• 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)
  • Mayuri

    (@mayuripatel)

    Possible Solutions:

    1. Remove 's' => $keyword and rely on tax_query:

    You could remove the content search ('s' => $keyword) and rely solely on the taxonomy query.

    2. Use the 'relation' => 'OR' for all queries, including 's':
    instead of having separate search and taxonomy filters, combine them under one meta_query using 'relation' => 'OR' to ensure that either the content or the taxonomy terms trigger a match.

    $args = array(
    'post_type' => 'work',
    'posts_per_page' => 20,
    'meta_query' => array(
    'relation' => 'OR',
    array(
    'key' => 'post_content',
    'value' => $keyword,
    'compare' => 'LIKE',
    ),
    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',
    ),
    ),
    );

    3. Check Taxonomy Registration:

    Ensure the taxonomies (work_category, work_style, work_material) are properly registered for the work post type, and that they are set to be publicly queryable:

    'public' => true,
    'show_in_rest' => true,
    'query_var' => true,
    Moderator bcworkz

    (@bcworkz)

    If you wish to search both title, content, OR taxonomies, you cannot rely solely upon tax_query because title and content occur in an entirely different table than taxonomies.

    When you combine 's' => search with 'tax_query' => args in WP_Query, the various fields for the title/content search are OR’d together. The various tax_query args can similarly be OR’d together. However, the search arg and tax_query arg are always AND’d so query results must match both a title/content match AND a tax_query match. In order to get OR logic between the args, you can either patch up the SQL query through the “posts_request” filter or implement your own custom SQL using global $wp_query object methods.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.