Custom Fields
Re-Envisioned
Support MB REST API Show Custom Post Type Data in place of ID in WP REST API

This topic contains 6 replies, has 2 voices, and was last updated by  Anh Tran 2 weeks, 3 days ago.

  • Creator
    Topic
  • #16686

    Sharath P S
    Participant

    I am using Metabox.io, and I have a certain Custom Post Types. Right now I am using a Destination CPT, which has a field type -> post. Now I am rendering the other CPT through that. Everything works fine, except the REST API response. It only shows the CPT ID attached to the Destination CPT.

    I want to show the Object Data in the Destination CPT of another post type which is embedded in it.

    JSON OUTPUT

    "meta_box": {
       "prefix-destination-story-field": ["535"], 
       "prefix-destination-gallery-field": ["269"],
       "prefix-destination-video-gallery-field": ["280"],
       "prefix-destination-trip-field": [ "236" ]
    }
    

    This is the link for Field Type Post: Metabox Post Type Field

    I need to know how should be able to populate an object inside the array of metabox-id?

    Any help would be appreciated. Let me know if I can produce any thing else to give you a clear insight of what I am asking for.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #16700

    Anh Tran
    Keymaster

    Hi Sharath,

    The values are returned via function rwmb_get_value and it has a filter with the same name rwmb_get_value for you to modify the values.

    So, I think you can do something like:

    add_filter( 'rwmb_get_value', function( $value, $field, $args, $post_id ) {
        // Change value only for REST API.
        if ( ! defined( 'REST_REQUEST' ) ) {
            return $value;
        }
    
        if ( 'your_field_id' !== $field['id'] ) {
            return $value;
        }
    
        // Modify your value here.
        return $value;
    }, 10, 4 );
    #16706

    Sharath P S
    Participant

    Thanks for the reply Anh, but did not work out for me.

    So here is the code in my Destination CPT, I have tried getting the data from the field_id: prefix-destination-story-field, as you can get it from my question too.

    But the API result was same as before. Here is what I’ve tried, I did not see any other object either.

    
    //for object info filter
    add_filter( 'rwmb_get_value', function( $value, $field, $args, $post_id ) {
        // Change value only for REST API.
        if ( ! defined( 'REST_REQUEST' ) ) {
            return $value;
        }
    
        if ( 'prefix-destination-story-field' !== $field['id'] ) {
            return $value;
        }
    
        // Modify your value here.
        return $value;
    }, 10, 4 );
    

    JSON RESPONSE

    
    "meta_box": {
      "prefix-destination-story-field": ["535"],
      "prefix-destination-gallery-field": ["269"],
      "prefix-destination-video-gallery-field": ["280"],
      "prefix-destination-trip-field": ["236"]
    }
    

    If you want I will show you the full code of that CPT.

    #16716

    Sharath P S
    Participant

    Hey Anh, I am still waiting for your reply. Please help me on this, cos I need to work on this. However, I have been looking for this all around but didn’t get any help on this. Please look into this and help me. Will wait for your response at the earliest.

    #16718

    Anh Tran
    Keymaster

    Hey Sharath,

    Please don’t just copy and paste my code. I put a comment “Modify your value here”, which means you should get the post details from the post ID ($value) there. Something like this:

    add_filter( 'rwmb_get_value', function( $value, $field, $args, $post_id ) {
        // Change value only for REST API.
        if ( ! defined( 'REST_REQUEST' ) ) {
            return $value;
        }
    
        if ( 'p' !== $field['id'] ) {
            return $value;
        }
    
        $value = get_post( $post_id, ARRAY_A );
        return $value;
    }, 10, 4 );
    #16725

    Sharath P S
    Participant

    Hey Anh, thanks for the help. It was indeed helpful, although, there is one catch. That is I cannot see the meta_box response inside the REST API response of prefix-destination-story-field. Could you please tell me how can I add/show the <string>meta_box of the Custom Post Type Story inside the Destination CPT in the response. Here is the response, with the code.

    Code:

    
    add_filter( 'rwmb_get_value', function( $value, $field, $args, $post_id ) {
        // Change value only for REST API.
        if ( ! defined( 'REST_REQUEST' ) ) {
            return $value;
        }
    
        if ( 'prefix-destination-story-field' !== $field['id'] ) {
            return $value;
        }
        
        $arr = [];
        $ids = get_post_meta($post_id, 'prefix-destination-story-field', false);
        //$value = get_post( $id, ARRAY_A);
        foreach ($ids as $id){
            array_push($arr, get_post($id, ARRAY_A));
        }
        return $arr;
        
    }, 10, 4 );
    

    API RESONSE:

    
    "meta_box": {
       "prefix-destination-story-field": [
         {
            "ID": 535,
            "post_author": "1",
            "post_date": "2019-10-24 12:42:39",
            "post_date_gmt": "2019-10-24 12:42:39",
            "post_content": "This is demo story content",
            "post_title": "Hello Story",
            "post_excerpt": "",
            "post_status": "publish",
            "comment_status": "closed",
            "ping_status": "closed",
            "post_password": "",
            "post_name": "hello-story",
            "to_ping": "",
            "pinged": "",
            "post_modified": "2019-10-24 16:39:25",
            "post_modified_gmt": "2019-10-24 16:39:25",
            "post_content_filtered": "",
            "post_parent": 0,
            "guid": "",
            "menu_order": 0,
            "post_type": "story",
            "post_mime_type": "",
            "comment_count": "0",
            "filter": "raw",
            "ancestors": [],
            "page_template": "",
            "post_category": [],
            "tags_input": []
         }
      ]
    }
    

    As you can clearly see in the response, that the meta_box is not shown. However, in this prefix-destination-story-field, there are meta-boxes linked with it. Thanks. Will appreciate this last help of yours. Thanks 🙂

    #16734

    Anh Tran
    Keymaster

    As this is a custom code to fetch post data, there won’t be a field meta_box in the post details. You can add a specific meta value if you want, like this:

    $arr = [];
    $ids = get_post_meta($post_id, 'prefix-destination-story-field', false);
    foreach ($ids as $id){
        $value = get_post($id, ARRAY_A);
        
        // THIS
        $value['field_id1'] = rwmb_meta( 'field_id1', '', $id );
        $value['field_id2'] = rwmb_meta( 'field_id2', '', $id );
    
        array_push($arr, $value);
    }
Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.