WP REST API - Custom Post Types And Fields

  Рет қаралды 29,776

Watch and Learn

Watch and Learn

Күн бұрын

In this video we are going to create WP REST API custom endpoint which will also show custom fields made with ACF.
You guys have been asking a lot of questions on this previous WP REST API video (bit.ly/33nMv5e) so I decided to make a few more videos about WP REST API, not just because you asked but also because WP #REST #API is pretty cool and can help a lot if you are developing decoupled themes where #WordPress is just used as a backend.
Code used in this video
bit.ly/2JhP9Q8
You can support my work on Patreon
/ watchlearn
Follow me on Social Media
Github: github.com/ivandoric
Twitter: / ivan_doric
Instagram: / watchlearntuts
Facebook: / watchlearntutorials

Пікірлер: 67
@bradfranklin-j66co
@bradfranklin-j66co Жыл бұрын
This is really cool
@amarmaharjan464
@amarmaharjan464 3 жыл бұрын
Thank you it was really hepful
@joseantonioandreu1462
@joseantonioandreu1462 3 жыл бұрын
Thank you, you save me a lot of time. ; )
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
Glad I could help :)
@vladbelozertsev4084
@vladbelozertsev4084 3 жыл бұрын
good film and plot. like
@muhammadhassan6158
@muhammadhassan6158 Жыл бұрын
Thank You Soo much Sir
@trumpfbam2094
@trumpfbam2094 3 жыл бұрын
thx my friend
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
You are welcome :)
@JaehoonMe
@JaehoonMe 3 жыл бұрын
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?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
Try setting post type to be 'products'. Or check the slug of you CPT that should be the name used in 'type' property.
@jozayw2592
@jozayw2592 3 жыл бұрын
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
@jozayw2592
@jozayw2592 3 жыл бұрын
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
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@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 (kzbin.info/www/bejne/laiXgJ5joa5nbsk) where I add posts from frontend, probably the same logic could be applied to users.
@jozayw2592
@jozayw2592 3 жыл бұрын
@@WatchandLearnTutorials thank you very much, love the content!
@LoL-lg9dp
@LoL-lg9dp 3 жыл бұрын
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.
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
You can check out this video: kzbin.info/www/bejne/laiXgJ5joa5nbsk to get a general idea how to add data from frontend.
@MarceloCoelho1
@MarceloCoelho1 2 жыл бұрын
How would you get the endpoint to show a woocommerce order with some order details?
@WatchandLearnTutorials
@WatchandLearnTutorials 2 жыл бұрын
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/
@LetTheWritersWrite
@LetTheWritersWrite 3 жыл бұрын
How do you like the WP api vs using October CMS
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
I like October a bit better because you can create your APIs using using Laravel commands and ORM. Which is much nicer IMO.
@SMARTHELP92
@SMARTHELP92 3 жыл бұрын
Can we get theme name,active plugins ,which plugin needs update info outside of wordpress admin in a separate dashboard?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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?
@SMARTHELP92
@SMARTHELP92 3 жыл бұрын
@@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...
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@SMARTHELP92 Ok, great. Good luck 😀
@koprualt8569
@koprualt8569 Жыл бұрын
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?
@WatchandLearnTutorials
@WatchandLearnTutorials Жыл бұрын
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.
@koprualt8569
@koprualt8569 Жыл бұрын
​@@WatchandLearnTutorials Hey! Thank you so much!! Im using php and adding (float) before variable is solved my problem.
@hjbritz
@hjbritz 3 жыл бұрын
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
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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?
@hjbritz
@hjbritz 3 жыл бұрын
@@WatchandLearnTutorials topic is a custom taxonomy
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@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.
@hjbritz
@hjbritz 3 жыл бұрын
@@WatchandLearnTutorials great stuff... Thanks. This works perfectly.
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@hjbritz 👍
@jfudman
@jfudman 3 жыл бұрын
what about custom ordering & pagination for the products?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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: kzbin.info/aero/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.
@adarshsoni5323
@adarshsoni5323 5 ай бұрын
How to rest enable the custom fields using the plugin. Is there any plugin which can do this.
@WatchandLearnTutorials
@WatchandLearnTutorials 4 ай бұрын
I don't know. Maybe, there is, but I don't know any.
@Lucky5111
@Lucky5111 2 жыл бұрын
How would you do taxonomies?
@WatchandLearnTutorials
@WatchandLearnTutorials 2 жыл бұрын
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.
@mukeshwarsingh270
@mukeshwarsingh270 3 жыл бұрын
Is there any way to edit the meta title and description of Yoast or Rank Math?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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.
@mukeshwarsingh270
@mukeshwarsingh270 3 жыл бұрын
@@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 :(
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@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.
@mukeshwarsingh270
@mukeshwarsingh270 3 жыл бұрын
@@WatchandLearnTutorials Umm, the Yoast Rest API plugin worked well for me! :) Thanks for the help BTW :)
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@mukeshwarsingh270 Nice 😀
@legomaster812
@legomaster812 3 жыл бұрын
So Nice 💘💘💘💘💘💘
@kumardeepanshu8503
@kumardeepanshu8503 2 жыл бұрын
bro i am getting cors error when deployed the website, i am using nextjs on client and hosting it to vercel
@WatchandLearnTutorials
@WatchandLearnTutorials 2 жыл бұрын
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
@kumardeepanshu8503 2 жыл бұрын
@@WatchandLearnTutorials thank you so much 🙏 One more question which hosting you recommend for hosting wordpress and next js ?
@WatchandLearnTutorials
@WatchandLearnTutorials 2 жыл бұрын
@@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.
@stephendeo1
@stephendeo1 2 жыл бұрын
Is there a way to do a POST method to a custom field?
@dualizeox9884
@dualizeox9884 2 жыл бұрын
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
@WatchandLearnTutorials
@WatchandLearnTutorials 2 жыл бұрын
@@dualizeox9884 Did you guys watch this video, maybe there is something in there that can help: kzbin.info/www/bejne/laiXgJ5joa5nbsk
@dualizeox9884
@dualizeox9884 2 жыл бұрын
@@WatchandLearnTutorials thanks but that's not relevant for creating a custom endpoint with updatable acf fields :\
@Mayanktaker
@Mayanktaker 3 жыл бұрын
Can you make a Wordpress+Flutter app series? Maybe on Udemy.
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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.
@Mayanktaker
@Mayanktaker 3 жыл бұрын
Watch and Learn thank you!! 🙂🙂
@knut.becker
@knut.becker 3 жыл бұрын
Where is this custom-api.php file from?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
What do you mean? I created it.
@faizalfahmi1485
@faizalfahmi1485 3 жыл бұрын
where i put custom-api.php?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
In plugins. Check this video first, I explain how to create api plugin there: kzbin.info/www/bejne/eWPXqIZvdteXeas
@babujune86
@babujune86 3 жыл бұрын
Hello, how to render content as html which have shortcut code and all. any body help me pls?
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
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.
@babud8876
@babud8876 3 жыл бұрын
thank you for your reply.
@WatchandLearnTutorials
@WatchandLearnTutorials 3 жыл бұрын
@@babud8876 No problem.
WP REST API - Add Posts From Frontend
13:10
Watch and Learn
Рет қаралды 20 М.
WP REST API - Custom Endpoints
14:00
Watch and Learn
Рет қаралды 79 М.
MISS CIRCLE STUDENTS BULLY ME!
00:12
Andreas Eskander
Рет қаралды 16 МЛН
Theming With WP REST API - Part 14 - Adding Custom Fields
9:18
Watch and Learn
Рет қаралды 8 М.
Exploring The Wordpress REST API & React Integration
51:32
Traversy Media
Рет қаралды 200 М.
WordPress   REST API Basics
5:53
Chris Miller
Рет қаралды 76 М.
How to Add New Custom Field in REST API | WordPress
15:51
mrknowitall
Рет қаралды 7 М.
WordPress REST API Authentication: Application Passwords
14:34
LearnWebCode
Рет қаралды 52 М.
Let's Checkout... #Payload #CMS
10:19
Watch and Learn
Рет қаралды 14 М.
Custom CRUD with WordPress REST API - Pricode
41:46
Pricode
Рет қаралды 9 М.
Send Emails With #Directus #Flows
6:56
Watch and Learn
Рет қаралды 5 М.