WP REST API retrieves posts from post type

How can I get all messages of a specific message type using the WP REST API (v1 or v2)? I am very new to this and trying to figure out how to do this.

I am currently using WP REST API v2 and managed to get a list of all message types using this

http://domain.com/wp-json/wp/v2/types 

and then I managed to get the type of message that interests me

 http://domain.com/wp-json/wp/v2/types/the-icons-update 

How do I get all messages from this particular type of content?

I tried with

 http://domain.com/wp-json/wp/v2/posts?filter[post_type]=the-icons-update 

But it returns an empty array (I suppose it returns messages by default, and on my site there are only messages inside the custom message type that I am trying to receive).

There may be a problem with the way I registered the message type?

 function custom_post_type() { $labels = array( 'name' => _x( 'The Icons Update', 'post type general name' ), 'singular_name' => _x( 'The Icons Update', 'post type singular name' ), 'add_new' => _x( 'Add Page', 'magazine' ), 'add_new_item' => __( 'Add New Page' ), 'edit_item' => __( 'Edit Page' ), 'new_item' => __( 'New Page' ), 'all_items' => __( 'All Pages' ), 'view_item' => __( 'View Page' ), 'search_items' => __( 'Search Pages' ), 'not_found' => __( 'No Page found' ), 'not_found_in_trash' => __( 'No Page found in the Trash' ), 'parent_item_colon' => '', 'menu_icon' => '', 'menu_name' => 'The Icons Update' ); $args = array( 'labels' => $labels, 'description' => 'Holds our projects and project specific data', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ), 'has_archive' => true, 'taxonomies' => array('post_tag', 'category'), 'hierarchical' => false, 'query_var' => true, 'queryable' => true, 'searchable' => true, 'rewrite' => array( 'slug' => 'the-icons-update' ) ); register_post_type( 'magazine', $args ); flush_rewrite_rules(); } add_action( 'init', 'custom_post_type' ); 

Any help with this is really appreciated.

+8
php wordpress json-api custom-post-type wordpress-rest-api
source share
4 answers

For v.2 there is a really simple and easy way. All you have to do is include the following property in the args array: 'show_in_rest' => true

Example:

 register_post_type( 'recipe', array( 'labels' => $labels, 'public' => true, 'menu_position' => 5, 'hierarchical' => false, 'supports' => $supports, 'show_in_rest' => true, 'taxonomies' => array('recipe-type', 'post_tag'), 'rewrite' => array( 'slug' => __('recipe', 'recipe') ) ) ); 
+5
source share

register_post_type ('name of post type' ...) is not the name of 'add_new'. Change the name of your message type to the log and check the result. Hope this helps.

+2
source share

Returning to v1 of the REST API plugin and /wp-json/posts?type=name-of-post-type , I managed to get messages from this particular message type.

+2
source share

To use the v2 REST API plugin:

In your theme's functions.php file, add the following to create a rest endpoint:

 add_action( 'init', 'add_myCustomPostType_endpoint'); function add_myCustomPostType_endpoint(){ global $wp_post_types; $wp_post_types['myCustomPostType']->show_in_rest = true; $wp_post_types['myCustomPostType']->rest_base = 'myCustomPostType'; $wp_post_types['myCustomPostType']->rest_controller_class = 'WP_REST_Posts_Controller'; } 

You should now have the following endpoint for the request:

 /wp-json/wp/v2/myCustomPostType 

myCustomPostType is the type of custom message that you have registered. "Rest_base" does not have to match the name of your custom message type.

Most likely, you will want to add additional fields specific to your custom message type, such as mail metadata or, possibly, from the Advanced Custom Fields plugin. For these scenarios, you can include these properties by adding a fragment similar to this to the functions.php file:

 function add_myCustomPostType_fields_url_to_myCustomPostType_request( $data, $post, $request ) { $_data = $data->data; $customImageProperty = get_field('customImageProperty'); $_data['customImageProperty'] = $customImageProperty['url']; $data->data = $_data; return $data; } add_filter( 'rest_prepare_myCustomPostType', 'add_myCustomPostType_fields_url_to_myCustomPostType_request', 10, 3 ); 
+2
source share

All Articles