15

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.

18

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

  • Thank you, this almost solved my problem! I now get some posts from that specific post type but not all of them appear and also their data is not complete, for example the category is not listed and I also need the advanced custom fields to be listed (in WP REST API v1.2.3 I managed to make the ACF appear). Thanks for your help on this – Jeff Sep 8 '15 at 17:34
4

To show custom post types in version 2, you need to add 'show_in_rest' => true in the register_post_type function arguments, then your posts with that custom post type will be available at the endpoint: wp-json/wp/v2/your-custom-post-type.

Source: http://scottbolinger.com/custom-post-types-wp-api-v2/

0

You should use this:-

http://domain.com/wp-json/wp/v2/posts?job-type=your-post-type 

Hope it works :)

  • Thank you so much for your reply but that didn't work :( – Jeff Sep 4 '15 at 8:09
  • Please note if you set the query_var to false when registering the custom taxonomy, you’ll need to change the parameters to: wp-json/wp/v2/posts/?taxonomy=job-type&term=manager (its just an example) – dev Sep 4 '15 at 8:13
  • Thank you but it also didn't work. Could there be an issue with how I registered the custom post type? I have updated the question, If you could take a look at it I would be very grateful – Jeff Sep 4 '15 at 8:38
  • Yes just checked updated answer – dev Sep 4 '15 at 9:22
0

Ok here is my complete answer:-

function prefix_register_post_type()
{
  register_post_type(
    'prefix_portfolio',
    array(
      'labels'        => array(
        'name'               => __('Portfolio', 'text_domain'),
        'singular_name'      => __('Portfolio', 'text_domain'),
        'menu_name'          => __('Portfolio', 'text_domain'),
        'name_admin_bar'     => __('Portfolio Item', 'text_domain'),
        'all_items'          => __('All Items', 'text_domain'),
        'add_new'            => _x('Add New', 'prefix_portfolio', 'text_domain'),
        'add_new_item'       => __('Add New Item', 'text_domain'),
        'edit_item'          => __('Edit Item', 'text_domain'),
        'new_item'           => __('New Item', 'text_domain'),
        'view_item'          => __('View Item', 'text_domain'),
        'search_items'       => __('Search Items', 'text_domain'),
        'not_found'          => __('No items found.', 'text_domain'),
        'not_found_in_trash' => __('No items found in Trash.', 'text_domain'),
        'parent_item_colon'  => __('Parent Items:', 'text_domain'),
      ),
      'public'        => true,
      'menu_position' => 5,
      'supports'      => array(
        'title',
        'editor',
        'thumbnail',
        'excerpt',
        'custom-fields',
      ),
      'taxonomies'    => array(
        'prefix_portfolio_categories',
      ),
      'has_archive'   => true,
      'rewrite'       => array(
        'slug' => 'portfolio',
      ),
    )
  );
}

add_action('init', 'prefix_register_post_type');


function prefix_register_taxonomy()
{
  register_taxonomy(
    'prefix_portfolio_categories',
    array(
      'prefix_portfolio',
    ),
    array(
      'labels'            => array(
        'name'              => _x('Categories', 'prefix_portfolio', 'text_domain'),
        'singular_name'     => _x('Category', 'prefix_portfolio', 'text_domain'),
        'menu_name'         => __('Categories', 'text_domain'),
        'all_items'         => __('All Categories', 'text_domain'),
        'edit_item'         => __('Edit Category', 'text_domain'),
        'view_item'         => __('View Category', 'text_domain'),
        'update_item'       => __('Update Category', 'text_domain'),
        'add_new_item'      => __('Add New Category', 'text_domain'),
        'new_item_name'     => __('New Category Name', 'text_domain'),
        'parent_item'       => __('Parent Category', 'text_domain'),
        'parent_item_colon' => __('Parent Category:', 'text_domain'),
        'search_items'      => __('Search Categories', 'text_domain'),
      ),
      'show_admin_column' => true,
      'hierarchical'      => true,
      'rewrite'           => array(
        'slug' => 'portfolio/category',
      ),
    )
  );
}

add_action('init', 'prefix_register_taxonomy', 0);

You should also register taxonomy while registering custom post.

After this the request would be:

wp-json/wp/v2/posts/?taxonomy=prefix_portfolio_categories'&term=your-any-category

Hope this may help you :)

  • Thank you a million for your effort on this but unfortunately that didn't work either. I'm sure I'm pretty close but I don't get what the issue could be. Thank you again – Jeff Sep 4 '15 at 15:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.