Extend WordPress REST API :: Add Fields to Posts Endpoint

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ม.ค. 2025

ความคิดเห็น • 25

  • @TORMENTUMM
    @TORMENTUMM ปีที่แล้ว

    I want to extend the api but to make post and get requests for CRUD operations on the wordpress database. How could I do it?

    • @TechiePress
      @TechiePress  ปีที่แล้ว

      You can make CRUD functionality in the callback function that you pass to the endpoints. That will work just fine.
      If you need some crud examples, I do some stuff here th-cam.com/play/PLNqG1qGUllk0Ov_KPxypiZ-4ZzwW5pyPz.html

  • @terrycarson
    @terrycarson 6 หลายเดือนก่อน

    No Git info anymore?

  • @Gigabyteserviceofficial
    @Gigabyteserviceofficial 4 ปีที่แล้ว +1

    so it can handle all CRUD operations?

    • @TechiePress
      @TechiePress  4 ปีที่แล้ว

      Yes it can.

    • @Gigabyteserviceofficial
      @Gigabyteserviceofficial 4 ปีที่แล้ว

      @@TechiePress where we can define 'post_type' => 'our-custom-post-type' if we want to do samething for custom post type

    • @TechiePress
      @TechiePress  4 ปีที่แล้ว

      At the same line. Just change the post_type post to your custom post type. So you would have something like 'post_type' => 'custom_post_type_name'

    • @Gigabyteserviceofficial
      @Gigabyteserviceofficial 4 ปีที่แล้ว +1

      @@TechiePress THANKS am I right?
      register_rest_field(
      'jit_image', // my custom post type is jits_image
      'my_custom_field_for_custom_post_type',
      array(..........

    • @Gigabyteserviceofficial
      @Gigabyteserviceofficial 4 ปีที่แล้ว

      @@TechiePress
      let's supposed my call back function is jit_image() then how to add offset to custom post type in wordpress api
      register_rest_route('jit/v1', 'media', [
      'methods' => 'POST',
      'callback' => 'jit_images',
      'permission_callback' => function() {
      return current_user_can('edit_posts');
      }
      ]);
      function jit_images() {
      $args = [
      'numberposts' => 99999,
      'post_type' => 'jit_image'
      ];
      $posts = get_posts($args);
      $data = [];
      $i = 0;
      foreach($posts as $post) {

      $data[$i]['image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
      $data[$i]['image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
      $data[$i]['image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
      $i++;
      }
      return $data;


      I want to allow my client-side app to fetch all custom posts but by pagination meaning offset = 10 so in each request, they will get 10 post's data not all 1000 data otherwise I think Mysq will take lots of loads.
      Please also share how to send requests e.g in postman like what should I include into a header or in the body as JSON data to get new 10 posts on each request.

  • @kyraex4414
    @kyraex4414 2 ปีที่แล้ว

    Can this be written in code snippet?

    • @TechiePress
      @TechiePress  2 ปีที่แล้ว

      Yes. Just add the ice to your snippet handing plugin and you are good to go

  • @alexdabruh
    @alexdabruh ปีที่แล้ว

    This is super helpful, but can this way be use to trigger a wp_mail or php mail call?
    I embedded a react app for my client’s website with a form submission component that I will like to generate an email after each submission

  • @volkan254
    @volkan254 3 ปีที่แล้ว

    Hello, I really love this
    working on something and wanted to utilise the same approach to extend woocommerce's API, cureently I am stuck and was wondering if you can help me think it through.

    • @TechiePress
      @TechiePress  3 ปีที่แล้ว

      Reach out via Web@mediauganda.com with some details and we can work something out

  • @TestTest-hk9oh
    @TestTest-hk9oh 3 ปีที่แล้ว

    Your video is excellent. Right now we are going to use Wordpress as headless cms and angular for the front end my query is how we can display the post view, likes and comment on the angular front end which updated post view, likes and comment from the angular frontend on the Wordpress backend. Can you please help me and give me some idea how we can achieve this?

    • @TechiePress
      @TechiePress  3 ปีที่แล้ว

      You will need to create an endpoint that stores a meta field on each post. Then use angular to do the like button click and send the data over http to save in the post meta. Then you can call that meta field in the display on the front end headless.

  • @ltonchis1245
    @ltonchis1245 3 ปีที่แล้ว +1

    Great videos! Question: is it possible to Update meta field and then trigger code? 🤔😮😂

    • @TechiePress
      @TechiePress  3 ปีที่แล้ว

      Yeah, it’s possible. Just need to get the logic right. If you save it via the editor, use the save_post hook.

  • @jyotibarasker2792
    @jyotibarasker2792 3 ปีที่แล้ว

    Your video is excellent. Right now we are going to use Wordpress as headless cms and angular for the front end my query is how we can display the post view, likes and comment on the angular front end which updated post view, likes and comment from the angular frontend on the Wordpress backend. Can you please help me and give me some idea how we can achieve this?

    • @TechiePress
      @TechiePress  3 ปีที่แล้ว

      You will need to create an endpoint that stores a meta field on each post. Then use angular to do the like button click and send the data over http to save in the post meta. Then you can call that meta field in the display on the front end headless. Using something like WPGraphQL might be the best option