Apart from posts and pages there are profiles on my site. Does exist some way how could I push them to the top of the search results? Currently, results are ordered at first by keyword and then by date. Problem is, the posts (about persons; e.g. announcements) are almost always newer than profiles. It is logical: profile was added only once, posts are posted continuously. So profile may be in the top 10, but also not. If there exists ten newer posts about person whose name is searched, person’s profile will be too deep in the list (common case). It is wrong. If I search the person’s name, profile is always the most relevant content, so it always should be the first result.
-
Hmm... I am not sure the grouping results by type is exactly what I need. I like sorting by relevancy. Ok, grouped results display the profile as the first result, but sorting by type wouldn’t be the best for other results. E.g. some posts may be more relevant than pages that would be above them.– Tereza VanackáCommented Oct 9, 2014 at 13:05
2 Answers
Since WP 3.7 there is a filter "posts_search_orderby"
that allow to set the ordering for search.
To be sure that filter works as expected set "orderby"
to "relevance"
.
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_search() ) {
$query->set( 'orderby', 'relevance' );
}
} );
and then, assuming your profiles CPT slug is ""profiles":
add_filter( 'posts_search_orderby', function( $search_orderby ) {
global $wpdb;
return "{$wpdb->posts}.post_type LIKE 'profiles' DESC, {$search_orderby}";
});
In this way as usually, will be shown all posts that have the searched keywords in post title or post content, but ordering in this way:
- posts with profiles CPT, no matter if searched keywords are in title or in content, ordered by descending date
- posts with searched keywords in title, no matter post type, ordered by descending date
- posts with searched keywords in content, no matter post type, ordered by descending date
-
It sounds promising. Unfortunately, I have a blank screen after inserting your code into my function.php. It seems the problematic is the second one. Use I wrong slug? I have tried "profile" and "profiles" too. Commented Oct 9, 2014 at 17:15
-
1There was an error on first snippet. Fixed that. Be sure you have a 5.3+ version of PHP. @TerezaVanacká– gmazzapCommented Oct 9, 2014 at 17:23
-
I've got it! Problem was the adding action. Solution was adding just filter. Thanks so much :-) Commented Oct 9, 2014 at 17:24
-
@TerezaVanacká yes, there was a problem there, but as said, now is fixed.– gmazzapCommented Oct 9, 2014 at 17:29
WordPress allows you to filter the results of a search. By default, it search on posts and pages, but you can add your customs posts types:
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('course','post','page'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
The code above will filter the search results, giving you posts of types: post,page,course (for example).
Hope that this helps to you.
-
2As far as I remember default behavior is to include all posts types, supporting search.– RarstCommented Oct 9, 2014 at 12:54