I want to get rid of certain terms in my /wp-json/v2/categories
response, namely uncategorized
but i have no clue, how properly to respond with nothing. because right now, i still get a response item (in this case false
) or an empty value, but i`d rather remove the whole response
function so123_rest_prepare_category(WP_REST_Response $response, WP_Term $item, WP_REST_Request $request)
{
if (in_array($item->term_id, [1, 62])) {
$response = false;
}
return $response;
}
add_filter('rest_prepare_category', 'so123_rest_prepare_category', 10, 3);
i also tried unset($response)
and unset($response->data)
, but that leads to fatal error or Undefined variable $response
With rest_prepare_category
, you're in the wrong spot for this, I think - this is the hook for modifying a single term's data, and even if you return false, that won't make the entry in the array on the "upper level" of this disappear.
rest_category_query
is the more appropriate place to get this done. using that you can manipulate the query parameters - and specify terms ids you explicitly want to exclude
via the option of the same name.
function so75613342_rest_category_query(array $prepared_args, WP_REST_Request $request)
{
if ($request['context'] === 'view') {
$prepared_args['exclude'] = [1, 62];
}
return $prepared_args;
}
add_filter('rest_category_query', 'so75613342_rest_category_query', 10, 2);