我想在我的
/wp-json/v2/categories
回复中去掉某些术语,即 uncategorized
但我不知道如何正确地回应 nothing。因为现在,我仍然得到一个响应项(在本例中为false
)或一个空值,但我宁愿删除整个响应
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);
我也尝试了
unset($response)
和unset($response->data)
,但这会导致致命错误或Undefined variable $response
对于
rest_prepare_category
,我认为你在错误的位置 - 这是修改单个术语数据的钩子,即使你返回 false,也不会在数组中输入“上层”的消失。
get_items
是完成这项工作更合适的地方;执行挂钩rest_category_query
,并使用它可以操纵查询参数 - 并通过同名选项指定您明确想要exclude
的术语ID。