Custom post types filtered by taxonomy using REST API v2

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!

4 s
4

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

Leave a Comment