Use tax_query to allow searching for results within custom taxonomies.
-
I want to register a REST API search route where the keywords can not only search for results through the title and content of posts but also find articles in different custom taxonomies based on the keywords. I added the following code to query the results, but articles in the custom taxonomies are not displayed in the results.
$keyword = sanitize_text_field($request['keyword']);
$args = array(
'post_type' => 'work',
'posts_per_page' => 20,
's' => $keyword,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'work_category',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
array(
'taxonomy' => 'work_style',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
array(
'taxonomy' => 'work_material',
'field' => 'name',
'terms' => $keyword,
'operator' => 'LIKE',
),
),
);
$query = new WP_Query($args);
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.