搜索词-在同一个词查询中查询Description__Like或Name__Like?

时间:2018-05-22 作者:StephanieQ

在我的主题中search.php 我设置了一个部分来显示适合搜索的术语。如果$keyword 出现在术语的标题或描述中。我已经设置好这样做了,但是如果我觉得必须进行两个单独的查询很笨拙,那么就删减结果,确保每个术语只显示一次。

  $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)) {
        continue;
      } 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
有没有一种更有效的方法可以基于这两者进行查询?非常感谢。

2 个回复
SO网友:harisrozak

我看不到WordPress可以通过名称和;描述因此,我组合了两个查询,如@StephanieQ,但可能更简单的方法是,在下面查看我的ajax响应:

public function ajax_project_terms_search() {
    $results = array();
    $search_query = sanitize_text_field($_GET[\'q\']);
    $withname = intval($_GET[\'withname\']);
    $search_keys = array(\'name__like\', \'description__like\');
    $exclude = array();

    foreach ($search_keys as $search_key) {
        $terms = get_terms( array(
            \'taxonomy\' => \'project\',
            \'hide_empty\' => false,
            \'number\' => 8,
            \'exclude\' => $exclude,
            $search_key => $search_query,
        ));

        // create results
        foreach ($terms as $term) {
            $exclude[] = $term->term_id;
            $results[] = array(
                \'id\' => isset($_GET[\'return_id\']) ? $term->term_id : $term->slug,
                \'text\' => $withname ? $term->name . \' - \' . $term->description : $term->name,
            );
        }
    }

    wp_send_json($results);
    wp_die();
}

SO网友:Rohit Ramani

我也搜索了这个,但没有找到解决方案,@harisrozak solution很好,但我还需要使用order by。

因此,我为其开发了自定义查询,如下所示。

function searchterm(){

global $wpdb;

$search_query = sanitize_text_field($_GET[\'q\']);

$query = "SELECT DISTINCT t.*, tt.* FROM wp_terms AS t  INNER JOIN wp_termmeta ON ( t.term_id = wp_termmeta.term_id ) INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN (\'category\') AND (t.name LIKE \'search_query%\' OR tt.description LIKE \'search_query%\') AND ( 
  wp_termmeta.meta_key = \'mata_key\'
) ORDER BY wp_termmeta.meta_value+0 DESC limit 10"

$the_query = $wpdb->get_results($query);

wp_send_json($the_query);
wp_die();

}
我希望有人能从中得到帮助。

结束

相关推荐

对类别使用WP_QUERY而不是GET_TERMS

我已通过此功能从Woocommerce获取所有父类别$terms = get_terms( array( \'taxonomy\' => \'product_cat\', \'hide_empty\' => false, \'parent\' => 0 ) ); 但我还没有做到这一点WP_Query. 现在我有两个问题:如何使用WQ\\u Query从WooCommerce获取类别列表?建议使用WP\\u Query