Skip to content
Advertisement

Filter WP REST API by taxonomy value (value1, value2, value3 etc…)

I’m trying to filter my Custom post type taxonomy by values, but I haven’t had any success.

I wondered if anyone else knew how to you’d go about doing this, or maybe I’m taking the wrong approach here?

Thanks

Advertisement

Answer

You can pass tax_query in your post query like this:

$tax_query[] =  array(
'taxonomy' => 'product_cat',
'field' => 'tag_ID', // Filter by Texonomy field name tag_ID
'terms' => $termID, // your texonomy by which you want to filter
);
$args = array(
    'post_type'     => 'product',
    'post_status'   => 'publish',
    'tax_query'     => $tax_query,
);

$loop = new WP_Query($args);

Also you can filter by taxonomy slug:-

$tax_query[] =  array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $slugID

);

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement