WordPress-一些搜索结果未显示 [英] Wordpress - Some search results not showing up

查看:142
本文介绍了WordPress-一些搜索结果未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经拥有一个网站.有一个称为项目"的自定义帖子类型.

We have a website that we've taken over. There is a custom post type called "projects".

如果我们使用frontend上的学习"一词进行搜索,即使其中包含学习"一词,结果也不会显示标题为考菲尔德语法学校,总体规划和学习项目"的项目标题.但是,如果我们使用"Caulfield"一词进行搜索,它将出现在搜索结果中.

If we do a search using the term "learning" on the frontend, the results do not show a project with the title "Caulfield Grammar School, Masterplan & Learning Project" even though the term "learning" is in the title. Yet if we search using the term "Caulfield" it appears in the search results.

该网站正在运行 Relevanssi ,即使将其禁用,该搜索结果也不会出现.任何想法将不胜感激.

The site is running the Relevanssi and even with it disabled that search result does not appear. Any ideas would be greatly appreciated.

谢谢.

推荐答案

请尝试在functions.php中使用以下代码

Please try below code in functions.php

add_filter( 'pre_get_posts', 'mjt_project_search' );
function mjt_project_search( $query ) {

    if ( $query->is_search ) {
    $query->set( 'post_type', array( 'projects') );
    $query->set( 'post_status', array( 'publish') );
    }

    return $query;

}

/*custom texonomy */
function mjt_search_where($where){
  global $wpdb;
  if (is_search())
    $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')";
  return $where;
}

function mjt_search_join($join){
  global $wpdb;
  if (is_search())
    $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
  return $join;
}

function mjt_search_groupby($groupby){
  global $wpdb;

  // we need to group on post ID
  $groupby_id = "{$wpdb->posts}.ID";
  if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby;

  // groupby was empty, use ours
  if(!strlen(trim($groupby))) return $groupby_id;

  // wasn't empty, append ours
  return $groupby.", ".$groupby_id;
}

add_filter('posts_where','mjt_search_where');
add_filter('posts_join', 'mjt_search_join');
add_filter('posts_groupby', 'mjt_search_groupby');

这篇关于WordPress-一些搜索结果未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆