WP REST API - Custom Post Types And Fields

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 68

  • @bradfranklin-j66co
    @bradfranklin-j66co Год назад +1

    This is really cool

  • @forlaps-agendaautomobile6012
    @forlaps-agendaautomobile6012 2 месяца назад

    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.

  • @amarmaharjan464
    @amarmaharjan464 3 года назад +1

    Thank you it was really hepful

  • @vladbelozertsev4084
    @vladbelozertsev4084 4 года назад +2

    good film and plot. like

  • @muhammadhassan6158
    @muhammadhassan6158 Год назад +1

    Thank You Soo much Sir

  • @JaehoonMe
    @JaehoonMe 3 года назад +1

    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.

  • @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: ruclips.net/video/_w4Ok-lI48g/видео.html to get a general idea how to add data from frontend.

  • @stephendeo1
    @stephendeo1 3 года назад +1

    Is there a way to do a POST method to a custom field?

    • @dualizeox9884
      @dualizeox9884 3 года назад

      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  3 года назад

      @@dualizeox9884 Did you guys watch this video, maybe there is something in there that can help: ruclips.net/video/_w4Ok-lI48g/видео.html

    • @dualizeox9884
      @dualizeox9884 3 года назад

      @@WatchandLearnTutorials thanks but that's not relevant for
      creating a custom endpoint with updatable acf fields :\

  • @adarshsoni5323
    @adarshsoni5323 9 месяцев назад

    How to rest enable the custom fields using the plugin. Is there any plugin which can do this.

  • @LetTheWritersWrite
    @LetTheWritersWrite 3 года назад

    How do you like the WP api vs using October CMS

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  3 года назад +1

      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 😀

  • @joseantonioandreu1462
    @joseantonioandreu1462 3 года назад +1

    Thank you, you save me a lot of time. ; )

  • @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 Год назад +1

      ​@@WatchandLearnTutorials Hey! Thank you so much!! Im using php and adding (float) before variable is solved my problem.

  • @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.

  • @jozayw2592
    @jozayw2592 3 года назад +1

    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 года назад +1

      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 года назад +1

      @@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.

    • @jozayw2592
      @jozayw2592 3 года назад

      @@WatchandLearnTutorials thank you very much, love the content!

  • @MarceloCoelho1
    @MarceloCoelho1 3 года назад

    How would you get the endpoint to show a woocommerce order with some order details?

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  3 года назад

      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/

  • @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: 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.

  • @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 года назад +1

      @@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 года назад +2

      @@WatchandLearnTutorials great stuff... Thanks. This works perfectly.

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  3 года назад +1

      @@hjbritz 👍

  • @trumpfbam2094
    @trumpfbam2094 4 года назад +1

    thx my friend

  • @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 года назад +1

      @@WatchandLearnTutorials Umm, the Yoast Rest API plugin worked well for me! :) Thanks for the help BTW :)

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  3 года назад

      @@mukeshwarsingh270 Nice 😀

  • @Mayanktaker
    @Mayanktaker 4 года назад

    Can you make a Wordpress+Flutter app series? Maybe on Udemy.

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  4 года назад +1

      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 4 года назад +1

      Watch and Learn thank you!! 🙂🙂

  • @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.

  • @knut.becker
    @knut.becker 3 года назад

    Where is this custom-api.php file from?

  • @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: ruclips.net/video/C2twS9ArdCI/видео.html

  • @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 года назад +1

      thank you for your reply.

    • @WatchandLearnTutorials
      @WatchandLearnTutorials  3 года назад

      @@babud8876 No problem.