问题
We have a website that we've taken over. There is a custom post type called "projects".
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.
The site is running the Relevanssi and even with it disabled that search result does not appear. Any ideas would be greatly appreciated.
Thanks in advance.
回答1:
That has kind of worked but there are other post types which it will need to show the results of so I changed the code to this:
add_filter( 'pre_get_posts', 'mjt_project_search' );
function mjt_project_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'projects', 'news', 'award', 'person', 'location', 'page') );
$query->set( 'post_status', array( 'publish') );
}
return $query;
}
That has worked but it isn't showing the 'Caulfield Grammar School, Masterplan & Learning Project' now.
回答2:
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');
来源:https://stackoverflow.com/questions/48534638/wordpress-some-search-results-not-showing-up