Search Terms - Querying on either description__like OR name__like in the same Term Query?

by StephanieQ   Last Updated May 22, 2018 18:08 PM

In my theme's search.php I have a section set up to show terms that fit the search. I want the term to show up IF the $keyword appears in the term's title or its description. I have it set up to do just that but if feels clunky to me to have to do two separate queries then prune the results to make sure each term is only displayed once.

  $search_matters_args = array(
    'taxonomy'    => array('book', 'magazine'), // taxonomies to search
    'orderby'     => 'id',
    'order'       => 'ASC',
    'hide_empty'  => false,
    'fields'      => 'all',
    'number'      => 8,
    'name__like'  => $keyword,

  $name_search = new WP_Term_Query($search_matters_args);
  /*--- Query again on description ---*/
  $search_matters_args['name__like'] = ''; // Override for next query
  $search_matters_args['description__like'] = $keyword; // Override for next query
  $desc_search = new WP_Term_Query($search_matters_args);
  $books_and_magazines = array_merge($name_search->terms, $desc_search->terms);

  $filtered_topics = array();

  if (!empty($books_and_magazines) && !is_wp_error($books_and_magazines)) {
    $unique_ids = array();
    for ($i=0; $i < count($books_and_magazines); $i++) {
      $termID =  $books_and_magazines[$i]->term_id;
      if (in_array($termID, $unique_ids)) {
      } else {
        $unique_ids[] = $termID;
        $filtered_topics[] = $books_and_magazines[$i];
    } // End For loop
    $books_and_magazines = $filtered_topics;
  } // End if $books_and_magazines is not empty or WP Error

Is there a more efficient way to query based on both? Thank you!

Related Questions

Filter Terms by FacetWP

Updated February 03, 2018 20:08 PM

Why does wp_remove_object_terms not work?

Updated April 01, 2018 06:08 AM

Search Query for Multiple Terms In Same Taxonomy

Updated October 10, 2017 18:08 PM