简体   繁体   English

如何修改wordpress搜索以查询分类术语和类别术语?

[英]How to amend wordpress search so it queries taxonomy terms and category terms?

I want to amend the search so if say I have taxonomy called "author" and add a term of "Ian Rankin" to a post, if I search for "Ian Rankin", I want that post to come up.我想修改搜索,所以如果说我有一个名为“作者”的分类法并在帖子中添加了“Ian Rankin”的术语,如果我搜索“Ian Rankin”,我希望该帖子出现。 I guess at the moment it only searches titles and content.我想目前它只搜索标题和内容。 How can I make it search terms too?我怎样才能让它成为搜索词?

You can alter the search query using filter hooks to join the taxonomy tables.您可以使用过滤器挂钩更改搜索查询以连接分类表。

eg to also search on the 'author' taxonomy例如,还可以搜索“作者”分类法

First join the taxonomy tables首先加入分类表

function tax_search_join( $join )
{
  global $wpdb;
  if( is_search() )
  {
    $join .= "
        INNER JOIN
          {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id
        INNER JOIN
          {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
        INNER JOIN
          {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
      ";
  }
  return $join;
}
add_filter('posts_join', 'tax_search_join');

then find the search term in the taxonomy 'author'然后在分类法“作者”中找到搜索词

function tax_search_where( $where )
{
  global $wpdb;
  if( is_search() )
  {
    // add the search term to the query
    $where .= " OR
    (
      {$wpdb->term_taxonomy}.taxonomy LIKE 'author'
      AND
      {$wpdb->terms}.name LIKE ('%".$wpdb->escape( get_query_var('s') )."%')
    ) ";
  }
  return $where;
}
add_filter('posts_where', 'tax_search_where');

and finally group the results by post id to avoid duplicate results because of the join最后按post id对结果进行分组,以避免由于连接而导致重复结果

function tax_search_groupby( $groupby )
{
  global $wpdb;
  if( is_search() )
  {
    $groupby = "{$wpdb->posts}.ID";
  }
  return $groupby;
}
add_filter('posts_groupby', 'tax_search_groupby');

It could also be done by using MySQL's EXISTS function with a subquery that joins term_relationships with terms .也可以通过使用 MySQL 的EXISTS函数和一个将term_relationshipsterm连接起来的子查询来完成 I have created the following snippet and put it into functions.php .我创建了以下代码段并将其放入functions.php 中 This applies to at least WordPress Version 5.3.1:这至少适用于 WordPress 5.3.1 版:

// Search titles and tags
function and_extend_search( $search, &$wp_query ) {

    global $wpdb;

    if ( empty( $search ))
        return $search;

    $terms = $wp_query->query_vars[ 's' ];
    $exploded = explode( ' ', $terms );
    if( $exploded === FALSE || count( $exploded ) == 0 )
        $exploded = array( 0 => $terms );

    $search = '';
    
    foreach( $exploded as $tag ) {
        $search .= " AND (
            ($wpdb->posts.post_title LIKE '%$tag%')

            OR EXISTS
            (
                SELECT 
                    *
                FROM 
                    $wpdb->term_relationships 
                LEFT JOIN 
                    $wpdb->terms 
                ON 
                    $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id
                WHERE
                        $wpdb->terms.name LIKE '%$tag%'
                    AND
                        $wpdb->term_relationships.object_id = $wpdb->posts.ID
            )
        )";
    }

    return $search;
}
add_filter('posts_search', 'and_extend_search', 500, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM