Forum Replies Created
-
AuthorPosts
-
October 28, 2019 at 2:07 PM in reply to: Show Custom Post Type Data in place of ID in WP REST API #16725
Sharath P S
ParticipantHey 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 ๐
October 27, 2019 at 1:59 PM in reply to: Show Custom Post Type Data in place of ID in WP REST API #16716Sharath P S
ParticipantHey 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.
October 25, 2019 at 5:44 PM in reply to: Show Custom Post Type Data in place of ID in WP REST API #16706Sharath P S
ParticipantThanks 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.
Sharath P S
ParticipantI am experiencing the same right now, I cannot see any metabox data coming up at all. Please help.
I have two metaboxes, and none of them shows up fine on the REST API. I don't want to use the plugin since I'd be using the API in other website, not on wordpress one. Please help.
[ { "id": 236, "date": "2019-10-18T08:38:14", "date_gmt": "2019-10-18T08:38:14", "guid": { "rendered": "http://localhost:8888/wordpress/?post_type=trip&p=236" }, "modified": "2019-10-24T16:15:24", "modified_gmt": "2019-10-24T16:15:24", "slug": "hello-trip", "status": "publish", "type": "trip", "link": "http://localhost:8888/wordpress/trip/hello-trip/", "title": { "rendered": "Hello Trip" }, "content": { "rendered": "<p>This is the dummy trip content</p>\n", "protected": false }, "template": "", "_links": { "self": [ { "href": "http://localhost:8888/wordpress/wp-json/wp/v2/trip/236" } ], "collection": [ { "href": "http://localhost:8888/wordpress/wp-json/wp/v2/trip" } ], "about": [ { "href": "http://localhost:8888/wordpress/wp-json/wp/v2/types/trip" } ], "wp:attachment": [ { "href": "http://localhost:8888/wordpress/wp-json/wp/v2/media?parent=236" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ]You can see there are no metaboxes. Here is my code.
// For Image metabox registration function trip_image_meta_box( $meta_boxes ) { $prefix = 'prefix-'; $meta_boxes[] = array( 'id' => 'trip-image', 'title' => esc_html__( 'Assets', 'metabox-online-generator' ), 'context' => 'advanced', 'priority' => 'default', 'autosave' => 'false', 'post_types' => 'trip', 'fields' => array( array( 'id' => $prefix . 'trip-link', 'type' => 'text', 'name' => esc_html__( 'Sharable Link', 'metabox-online-generator' ), 'placeholder' => esc_html__( 'Please provide link here', 'metabox-online-generator' ), ), array( 'id' => $prefix . 'trip_image', 'type' => 'image_upload', 'name' => esc_html__( 'Image Uploads', 'metabox-online-generator' ), 'force_delete' => 'false', 'max_file_uploads' => '10', 'max_status' => true ) ) ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'trip_image_meta_box' ); //Trip Informations metabox function trip_information( $meta_boxes ){ $prefix = 'prefix-'; $meta_boxes[] = array( 'id' => 'trip-info', 'title' => esc_html__( 'Trip Information', 'metabox-online-generator' ), 'context' => 'advanced', 'priority' => 'default', 'autosave' => 'false', 'post_types' => 'trip', 'fields' => array( array( 'id' => $prefix . 'trip_lang', 'type' => 'text', 'name' => esc_html__( 'Language', 'metabox-online-generator' ) ), array( 'id' => $prefix . 'trip_duration', 'type' => 'text', 'name' => esc_html__( 'Duration', 'metabox-online-generator' ) ), array( 'id' => $prefix . 'trip_costing', 'type' => 'text', 'name' => esc_html__( 'Package Price', 'metabox-online-generator' ) ) ) ); return $meta_boxes; } add_filter('rwmb_meta_boxes', 'trip_information'); -
AuthorPosts