You could try using the rest_post_query
action to add exclusion filters for categories and tags. Try something like the below.
This adds ['filter']['exclude_categories']
and ['filter']['exclude_tags']
as options to use in the WP REST API. Each filter accepts multiple categories/tags seperated by a comma.
add_action( 'rest_post_query', 'your_exclude_query', 10, 2 );
function your_exclude_query( $args, $request ) {
// Exclude Query
if ( isset($request['filter']['exclude_categories']) || isset($request['filter']['exclude_tags']) ) {
// If the exclude categories filter is set
if ( isset($request['filter']['exclude_categories']) ) {
$categories = explode( ',', $request['filter']['exclude_categories'] ); // NOTE: Assumes comma separated taxonomies
array_push( $pre_tax_query, array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => array( $categories ),
'operator' => 'NOT IN'
));
}
// If the exclude tags filter is set
if ( isset($request['filter']['exclude_tags']) ) {
$tags = explode( ',', $request['filter']['exclude_tags'] ); // NOTE: Assumes comma separated taxonomies
array_push( $pre_tax_query, array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => array( $tags ),
'operator' => 'NOT IN'
));
}
$tax_query = array(
'relation' => 'AND',
$pre_tax_query
);
$args[ 'tax_query' ] = $tax_query;
}
return $args;
} // end your_exclude_query