WP REST API fetch posts from post type
Asked Answered
C

4

8

How can I get all the posts from a specific custom post type with WP REST API (either v1 or v2)? I'm very new to this and trying to understand how to do that.

I am currently using WP REST API v2 and managed to fetch a list of all the post types with this

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

and then managed to get the post type I'm interested in with

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

How do I get all the posts from that specific content type?

I have 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 the default posts and on my site there are only posts inside the custom post type I'm trying to retrieve).

Could there be an issue with how I registered the post 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.

Charitycharivari answered 4/9, 2015 at 8:53 Comment(0)
S
5

There is a really shot and easy way for v.2. All that you have to do is to include to your args array the following property: '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') )
     )
);
Subordinate answered 5/7, 2016 at 21:56 Comment(4)
Where do I add that? Thanks.Happ
@Happ in your functions.php file ;)Subordinate
I only want to include the ID of the post and that's all. Would the above work? I can open a new question if warranted. ThanksHapp
I have never tried with ID of the post, but I think that it should work :)Subordinate
M
3

To use v2 of the REST API plugin:

In the functions.php file of your theme, 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';
}

Now you should have the following endpoint to query from:

/wp-json/wp/v2/myCustomPostType

myCustomPostType being the custom post type that you registered. The "rest_base" does not have to match the name of your custom post type.

You will most likely want to add additional fields that are specific to your custom post type, such as post metadata or perhaps from the Advanced Custom Fields plugin. For those scenarios, you can include those properties by adding a snippet like this to your 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 );
Malina answered 25/1, 2016 at 15:30 Comment(0)
L
2

register_post_type('name of post type'...) not the 'add_new' name. Change the name of your post type to Magazine and checkout the result. Hope it helps.

Lockard answered 5/9, 2015 at 22:27 Comment(1)
Thank you, I made that change but unfortunately it didn't solve my issue. I reverted back to v1 of the REST API plugin and with domain.com/wp-json/posts?type=magazine I managed to retrieve the posts from that specific post type. Thank youCharitycharivari
C
2

By reverting back to v1 of the REST API plugin and with /wp-json/posts?type=name-of-post-type I managed to retrieve the posts from that specific post type.

Charitycharivari answered 7/9, 2015 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.