Hi ! If I didn't use ACF to create my custom post and field how can take my meta data ? I mean that the function "get_field" is created by ACF but when I use your code I have this message "Uncaught Error: Call to undefined function get_field() " Another function exist to call custom fields from my database ? Thanks for the tuto.
Hello. Thank you for the video. I have an issue with custom post types. I have created product post type and wordpress api shows me json data in broweser for this post type. But when I try to post to API by specifing 'type':'product' post content goes to normal post type (not to custom post type products). How can I solve this?
great work, thank you!!! I'm beginner with php but all works great! we learned from this video how to get the info from database but i want to post the info too! please tell me how can i post it! thank you again.
Hmmm, I don't know... you would have to check WordPress docs for that. For example you could use wp_get_themes() developer.wordpress.org/reference/functions/wp_get_themes/ to get all available themes. Other stuff you can google yourself. However I'm not sure how many functions there are to expose Admin specific stuff. Are you trying to create a dashboard for WP that would be decoupled from the actual WP installation?
@@WatchandLearnTutorials I am going to build a separate web application here user can add his wordpress site url. After this user can see his site theme name,wp version,active plugins,which plugins has update notifications...
Hi! I have a custom field which is number field, but output of rest is like as "height": "35.48591", but i need to this format "height": 35.48591 without quotes. How can do this?
Well if you are using JavaSscript to get the data you can use parseInt or Number() functions, or even just add a + before your variable. If using PHP there is probably a function to convert string to a number, just Google it. So you need to get the data and then transform that string to a number OR you can probably do it on the endpoint and send the number right away.
I am recreating what you have done above but instead for users, I have it working for the standard wordpress user fields but when I try to add the acf user fields to the end point I keep getting a null value. Also is there a way that I can allow this end point to be read and updated too? Thanks
Ok I figured out that I need to add user_ before the id to make it work for the user custom fields but my question still stands about allowing user data to be updated
@@jozayw2592 Glad you figured it out. For updating the users or any other type of data you would need a post route for that, for users it probably already exists. Check out this video (ruclips.net/video/_w4Ok-lI48g/видео.html) where I add posts from frontend, probably the same logic could be applied to users.
WooCommerce has it's own REST API, so you should probably use that: docs.woocommerce.com/document/woocommerce-rest-api/ ... the docs for available endpoints are here: woocommerce.github.io/woocommerce-rest-api-docs/
You can use WP Query for that instead of get posts. And send pagination along with the response. Same thing with ordering. Something like this: $args = [ 'posts_per_page' => 10, 'post_type' => 'products', 'paged' => 1, ]; $posts = new WP_Query($args); $data = []; $i = 0; $data['total_pages'] = $posts->max_num_pages; $data['total_posts'] = $posts->found_posts; foreach($posts->posts as $post) { $data['data'][$i]['id'] = $post->ID; $data['data'][$i]['title'] = $post->post_title; ... $i++; } return $data; This will send total_pages and total_posts with your response and you can create pagination on the frontend with that. Read about WP_Query: developer.wordpress.org/reference/classes/wp_query/#pagination-parameters or check out my series about it: ruclips.net/p/PLUBR53Dw-Ef8RvXF4iEU6UnhOlrcfIYn6 You can read about ordering in WP query here: developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters Also follow my channel if you want to be notified when my Next.js and WP series comes out (it will be a payed series) where I cover this example and many more when creating custom endpoints if you are interested.
Hi. Thanks for the informative video. How do I return an array of ids in this example? I have an array of topics with the values "topic": [1,2,3] when I view the standard rest api, but when I use the code $data[$i]['topic'] = $post->post_topic; my results are "topic": "" and not 1,2,3. Your assistance will be greatly appreciated. Thanks
@@hjbritz Then you should be using get_the_terms function to get the taxonomy for the post. Something like: $data[$i]['topic'] = get_the_terms( $post->ID, 'topic' ) , I think that something like this should work.
You would edit those in WP administration like usual. But if you want know how to add them to the API, read the Yoast docs, and you can use the same functions that you would use inside your theme to get specific data like OG tags, meta titles, descriptions etc. I really don't like Yoast and never heard of Rank Math so I can't give you exact code for that.
@@WatchandLearnTutorials I don’t know what to do but one thing i know is that it’s possible.. it is also possible to get the page id of all the pages? Like i input a url and it outputs it’s page id... Also, please help me with the yoast code, i am stuck since 6 days :(
@@mukeshwarsingh270 For Yoast It could be something like $data['meta_description'] = get_post_meta($post[0]->ID, '_yoast_wpseo_metadesc', true); This maybe gets the description of the post or page. But I haven't tested it, just did a quick Google search.
Hmmm, never tried Flutter but I have it on my TODO list. So maybe there will be a series about Flutter, don't know about WordPress though. We will see how it goes.
You can try something like this: www.coditty.com/code/enable-cors-on-wordpress-rest-api .... the problem is that your client and backend are on different domains so that is why you are getting an error.
@@kumardeepanshu8503 Well if you wanna host them on the same server then something like Digital Ocean or AWS. You could also leave it as is, but make them use same domain. They can be on different servers. WordPress on some shared hosting and Nextjs on Vercel. You can add custom domain to Vercel, but you will have to play around with DNS records and such.... but that is not really my field of expertise.
Well that is a bit tricky, because you will send only text. Now if you want to use shortcodes, you would have to create a pareser on your frontend that would go through that text and when it hits a shortcode it would have to do something. Now, the problem with that is that shortcodes usually come from a plugin. So when you hit shortcode you would have to find a way to tell your frontend what to do, and that could range from something simple to something very complicated, depending on what the plugin does. There is no one size fits all solution for this. Sorry.
This is really cool
Hi !
If I didn't use ACF to create my custom post and field how can take my meta data ?
I mean that the function "get_field" is created by ACF but when I use your code I have this message "Uncaught Error: Call to undefined function get_field() "
Another function exist to call custom fields from my database ? Thanks for the tuto.
Thank you it was really hepful
good film and plot. like
Thank You Soo much Sir
Hello. Thank you for the video. I have an issue with custom post types. I have created product post type and wordpress api shows me json data in broweser for this post type. But when I try to post to API by specifing 'type':'product' post content goes to normal post type (not to custom post type products). How can I solve this?
Try setting post type to be 'products'. Or check the slug of you CPT that should be the name used in 'type' property.
great work, thank you!!! I'm beginner with php but all works great!
we learned from this video how to get the info from database but i want to post the info too! please tell me how can i post it!
thank you again.
You can check out this video: ruclips.net/video/_w4Ok-lI48g/видео.html to get a general idea how to add data from frontend.
Is there a way to do a POST method to a custom field?
Did you figure it out Stephen? I've been spinning my wheels on this one as I'm currently stuck on trying to figure it out
@@dualizeox9884 Did you guys watch this video, maybe there is something in there that can help: ruclips.net/video/_w4Ok-lI48g/видео.html
@@WatchandLearnTutorials thanks but that's not relevant for
creating a custom endpoint with updatable acf fields :\
How to rest enable the custom fields using the plugin. Is there any plugin which can do this.
I don't know. Maybe, there is, but I don't know any.
How do you like the WP api vs using October CMS
I like October a bit better because you can create your APIs using using Laravel commands and ORM. Which is much nicer IMO.
Can we get theme name,active plugins ,which plugin needs update info outside of wordpress admin in a separate dashboard?
Hmmm, I don't know... you would have to check WordPress docs for that. For example you could use wp_get_themes() developer.wordpress.org/reference/functions/wp_get_themes/ to get all available themes. Other stuff you can google yourself. However I'm not sure how many functions there are to expose Admin specific stuff. Are you trying to create a dashboard for WP that would be decoupled from the actual WP installation?
@@WatchandLearnTutorials I am going to build a separate web application here user can add his wordpress site url. After this user can see his site theme name,wp version,active plugins,which plugins has update notifications...
@@SMARTHELP92 Ok, great. Good luck 😀
Thank you, you save me a lot of time. ; )
Glad I could help :)
Hi! I have a custom field which is number field, but output of rest is like as "height": "35.48591", but i need to this format "height": 35.48591 without quotes. How can do this?
Well if you are using JavaSscript to get the data you can use parseInt or Number() functions, or even just add a + before your variable. If using PHP there is probably a function to convert string to a number, just Google it. So you need to get the data and then transform that string to a number OR you can probably do it on the endpoint and send the number right away.
@@WatchandLearnTutorials Hey! Thank you so much!! Im using php and adding (float) before variable is solved my problem.
How would you do taxonomies?
Don't know of top of my head, but if you can display them through code the you can display them here. Check the ACF docs for that.
I am recreating what you have done above but instead for users, I have it working for the standard wordpress user fields but when I try to add the acf user fields to the end point I keep getting a null value. Also is there a way that I can allow this end point to be read and updated too? Thanks
Ok I figured out that I need to add user_ before the id to make it work for the user custom fields but my question still stands about allowing user data to be updated
@@jozayw2592 Glad you figured it out. For updating the users or any other type of data you would need a post route for that, for users it probably already exists. Check out this video (ruclips.net/video/_w4Ok-lI48g/видео.html) where I add posts from frontend, probably the same logic could be applied to users.
@@WatchandLearnTutorials thank you very much, love the content!
How would you get the endpoint to show a woocommerce order with some order details?
WooCommerce has it's own REST API, so you should probably use that: docs.woocommerce.com/document/woocommerce-rest-api/ ... the docs for available endpoints are here: woocommerce.github.io/woocommerce-rest-api-docs/
what about custom ordering & pagination for the products?
You can use WP Query for that instead of get posts. And send pagination along with the response. Same thing with ordering. Something like this:
$args = [
'posts_per_page' => 10,
'post_type' => 'products',
'paged' => 1,
];
$posts = new WP_Query($args);
$data = [];
$i = 0;
$data['total_pages'] = $posts->max_num_pages;
$data['total_posts'] = $posts->found_posts;
foreach($posts->posts as $post) {
$data['data'][$i]['id'] = $post->ID;
$data['data'][$i]['title'] = $post->post_title;
...
$i++;
}
return $data;
This will send total_pages and total_posts with your response and you can create pagination on the frontend with that. Read about WP_Query: developer.wordpress.org/reference/classes/wp_query/#pagination-parameters or check out my series about it: ruclips.net/p/PLUBR53Dw-Ef8RvXF4iEU6UnhOlrcfIYn6
You can read about ordering in WP query here: developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
Also follow my channel if you want to be notified when my Next.js and WP series comes out (it will be a payed series) where I cover this example and many more when creating custom endpoints if you are interested.
Hi. Thanks for the informative video. How do I return an array of ids in this example? I have an array of topics with the values "topic": [1,2,3] when I view the standard rest api, but when I use the code $data[$i]['topic'] = $post->post_topic; my results are "topic": "" and not 1,2,3. Your assistance will be greatly appreciated. Thanks
Are you sure its $post->post_topic? maybe it should be just $post->topic. What is a topic? Is that a custom taxonomy, or a custom field?
@@WatchandLearnTutorials topic is a custom taxonomy
@@hjbritz Then you should be using get_the_terms function to get the taxonomy for the post. Something like: $data[$i]['topic'] = get_the_terms( $post->ID, 'topic' ) , I think that something like this should work.
@@WatchandLearnTutorials great stuff... Thanks. This works perfectly.
@@hjbritz 👍
thx my friend
You are welcome :)
Is there any way to edit the meta title and description of Yoast or Rank Math?
You would edit those in WP administration like usual. But if you want know how to add them to the API, read the Yoast docs, and you can use the same functions that you would use inside your theme to get specific data like OG tags, meta titles, descriptions etc. I really don't like Yoast and never heard of Rank Math so I can't give you exact code for that.
@@WatchandLearnTutorials I don’t know what to do but one thing i know is that it’s possible.. it is also possible to get the page id of all the pages? Like i input a url and it outputs it’s page id... Also, please help me with the yoast code, i am stuck since 6 days :(
@@mukeshwarsingh270 For Yoast It could be something like $data['meta_description'] = get_post_meta($post[0]->ID, '_yoast_wpseo_metadesc', true); This maybe gets the description of the post or page. But I haven't tested it, just did a quick Google search.
@@WatchandLearnTutorials Umm, the Yoast Rest API plugin worked well for me! :) Thanks for the help BTW :)
@@mukeshwarsingh270 Nice 😀
Can you make a Wordpress+Flutter app series? Maybe on Udemy.
Hmmm, never tried Flutter but I have it on my TODO list. So maybe there will be a series about Flutter, don't know about WordPress though. We will see how it goes.
Watch and Learn thank you!! 🙂🙂
bro i am getting cors error when deployed the website,
i am using nextjs on client and hosting it to vercel
You can try something like this: www.coditty.com/code/enable-cors-on-wordpress-rest-api .... the problem is that your client and backend are on different domains so that is why you are getting an error.
@@WatchandLearnTutorials thank you so much 🙏
One more question which hosting you recommend for hosting wordpress and next js ?
@@kumardeepanshu8503 Well if you wanna host them on the same server then something like Digital Ocean or AWS. You could also leave it as is, but make them use same domain. They can be on different servers. WordPress on some shared hosting and Nextjs on Vercel. You can add custom domain to Vercel, but you will have to play around with DNS records and such.... but that is not really my field of expertise.
Where is this custom-api.php file from?
What do you mean? I created it.
where i put custom-api.php?
In plugins. Check this video first, I explain how to create api plugin there: ruclips.net/video/C2twS9ArdCI/видео.html
Hello, how to render content as html which have shortcut code and all. any body help me pls?
Well that is a bit tricky, because you will send only text. Now if you want to use shortcodes, you would have to create a pareser on your frontend that would go through that text and when it hits a shortcode it would have to do something. Now, the problem with that is that shortcodes usually come from a plugin. So when you hit shortcode you would have to find a way to tell your frontend what to do, and that could range from something simple to something very complicated, depending on what the plugin does. There is no one size fits all solution for this. Sorry.
thank you for your reply.
@@babud8876 No problem.