Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Ajay

    (@ajay)

    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

    Thread Starter luciandavidescu

    (@luciandavidescu)

    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?

    Plugin Author Ajay

    (@ajay)

    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.

    Thread Starter luciandavidescu

    (@luciandavidescu)

    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?

    Plugin Author Ajay

    (@ajay)

    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.

    Thread Starter luciandavidescu

    (@luciandavidescu)

    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.

    Plugin Author Ajay

    (@ajay)

    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”

    Thread Starter luciandavidescu

    (@luciandavidescu)

    $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();
    Plugin Author Ajay

    (@ajay)

    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();
Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘WP_query’ is closed to new replies.