I have the following situation:

I created a custom Post Type names Works. And also attached to it custom taxonomy named Work Types

Here is the code

function rk_work_post_type(){
$labels = array(
    'name'               => 'Work',
    'singular_name'      => 'Work',
    'add_new'            => 'Add Work',
    'all_items'          => 'All Works',
    'add_new_item'       => 'Add Work',
    'edit_item'          => 'Edit Work',
    'new_item'           => 'New Work',
    'view_item'          => 'View Work',
    'search_item'        => 'Search Work',
    'not_found'          => 'No items found',
    'not_found_in_trash' => 'No items found in trash',
    'parent_item_colon'  => 'Parent Item'
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'publicly_queryable' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail',
        'revisions',
        'comments',
    ),
    // 'taxonomies' => array('category', 'post_tag'),
    'menu_icon' => 'dashicons-hammer',
    'menu_position' => 5,
    'exclude_from_search' => false,
    'show_in_rest'       => true,
    'rest_base'          => 'works',
    'rest_controller_class' => 'WP_REST_Posts_Controller',      
);
register_post_type('work',$args);
}

add_action('init','rk_work_post_type');


function rk_work_taxonomies() {
//add new taxonomy hierarchical
$labels = array(
    'name' => 'Work Types',
    'singular_name' => 'Work type',
    'search_items' => 'Search Types',
    'all_items' => 'All Work Types',
    'parent_item' => 'Parent Type',
    'parent_item_colon' => 'Parent Type:',
    'edit_item' => 'Edit Work Type',
    'update_item' => 'Update Work Type',
    'add_new_item' => 'Add New Work Type',
    'new_item_name' => 'New Work Type Name',
    'menu_name' => 'Work Types'
);

$args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'work_type' ),
    'show_in_rest'       => true,
    'rest_base'          => 'work_type',
    'rest_controller_class' => 'WP_REST_Terms_Controller',      
);

register_taxonomy('work_type', array('work'), $args);
}
add_action( 'init' , 'rk_work_taxonomies' );

The issue that i have is that i cant find a way to get the Works filtered by Work Type using REST API v2

I read that after some wordpress update filter query was removed from REST API. So what is the proper way to do this now? Can you provide some example please?

Thank you in advance!

Ok, the solution was:

example.com/wp-json/wp/v2/works?work_type=10

It views the work_types as id's. You can view the id's in:

example.com/wp-json/wp/v2/works
  • to retrive by slug just add _slug at the end of category slug. work_type_slug={slug} – user254153 Sep 1 at 6:13
  • Adding _slug to the end of the param doesn't work for me. I'm using WordPress 4.9.8, REST API v2. Using the id works great though. If you want to get the single term from the REST API based on the slug, then you use slug={term_slug} eg: example.com/wp-json/wp/v2/works?slug=work-type-slug – Tim Oct 1 at 13:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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