WP REST API fetch posts from post type

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.

4

Just add the next parmater into the function register_post_type, it can be before ‘menu_position’parameter. ‘show_in_rest’ => true

enter image description here

if you’re using a plugin to register your posttype you can use the next code:

add_action( 'init', 'add_anuncios_to_json_api', 30 );
function add_anuncios_to_json_api(){
    global $wp_post_types;
    $wp_post_types['anuncio']->show_in_rest = true;
}

after that, you’ll be able to list your posts from mydomain.com/wp-json/wp/v2/posttype_slug

in my case: mydomain.com/wp-json/wp/v2/anuncio

you can also register a new base using the next code:

add_action( 'init', 'add_anuncios_to_json_api', 30 );
function add_anuncios_to_json_api(){
    global $wp_post_types;
    $wp_post_types['anuncio']->show_in_rest = true;
    $wp_post_types['anuncio']->rest_base="clasi";
    $wp_post_types['anuncio']->rest_controller_class="WP_REST_Posts_Controller";
}

just replace anuncio for your post type slug and ‘clasi’ will be your route. mydomain.com/wp-json/wp/v2/clasi

Leave a Comment