WP_query
-
Does the plugin also change the results of WP_query loops ran with the ‘s’ parameter?
-
If you’re using seamless mode, the plugin modifies the search loops by checking if the loop is the main query and is a search loop
https://github.com/ajaydsouza/better-search/blob/master/better-search.php#L1421
I am, so I assume the answer is yes. Than how do I make sure that I order query results by relevance? Does orderby=none do the job?
You should ideally only have a single search loop which is on the search results page i.e. “/?s=”
And, these are automatically ordered by relevance.
That I know, but I also have a custom wp_query under posts, that displays a list of related posts based on searching (with ‘s’ parameter) for a chosen keyword. So the questions are – will the plugin influence the results and if so can i sort them by relevance rather than date?
I don’t think it should because the custom query would not be classified as the main loop by WordPress.
That being said, if it does, then the results will automatically be sorted by relevance.
Well, default WP_Query behaviour is to orderby=date and order=DESC, so i thought i’d have to change that in order not to get results resorted chronologically. Was hoping orderby=none would do just that, but it seems that it’s not really documented anywhere.
Could you please post the existing query you are using and I could potentially assist there.
If you’re passing a specific set of posts, you could use orderby => “post__in”
$slug = str_replace('-','+',$post->post_name); $args = array('s' => $slug,'posts_per_page' => '5','orderby' => 'none'); // is the last one ok? $search_slug = new WP_Query( $args ); if ( $search_slug->have_posts() ) {while ( $search_slug->have_posts() ) {$search_slug->the_post(); include 'snippet.php'; } } wp_reset_postdata();Hi, you could try this:
$slug = str_replace('-','+',$post->post_name); $search_ids = arrary(); $matches = get_bsearch_matches( $slug, 0 ); // Fetch the search results for the search term stored in $search_query $searches = $matches[0]; // 0 index contains the search results always if ( $searches ) { $search_ids = wp_list_pluck( $searches, 'ID' ); } } $args = array( 'post__in' => $search_ids, 'order_by' => 'post__in', 'posts_per_page' => '5', ); $search_slug = new WP_Query( $args ); if ( $search_slug->have_posts() ) {while ( $search_slug->have_posts() ) {$search_slug->the_post(); include 'snippet.php'; } } wp_reset_postdata();
The topic ‘WP_query’ is closed to new replies.