Wordpress - Custom post types filtered by taxonomy using REST API v2
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
I installed the WP REST Filter plugin and then i'm able to filter post by category slug like:
http://www.example.com/wp-json/wp/v2/my_custom_posts?filter[my_custom_taxomony]=my_custom_taxonomy_slug
in your case:
http://www.example.com/wp-json/wp/v2/works?filter[work_type]=building
If you really need to use slug as url parameter you can add a custom filter, take a look to rest_{$this->post_type}_query hook
You can do something like that (post_type: work, taxonomy: work_type):
/**
* Filter work post type by work_type slug
*
* @param array $args
* @param WP_Rest_Rquest $request
* @return array $args
*/
function filter_rest_work_query( $args, $request ) {
$params = $request->get_params();
if(isset($params['work_type_slug'])){
$args['tax_query'] = array(
array(
'taxonomy' => 'work_type',
'field' => 'slug',
'terms' => explode(',', $params['work_type_slug'])
)
);
}
return $args;
}
// add the filter
add_filter( "rest_work_query", 'filter_rest_work_query', 10, 2 );
and then
https://example.com/wp-json/wp/v2/work?work_type_slug=slug01,slug02