Skip to main contentSkip to footer

If you want to have sticky posts first in results when they match the search query, just add this code to your theme functions.php:

add_filter( 'relevanssi_hits_filter', 'rlv_sticky_first' );
function rlv_sticky_first( $hits ) {
	$sticky          = array();
	$nonsticky       = array();
	$sticky_post_ids = get_option( 'sticky_posts' );
	foreach( $hits[0] as $hit ) {
		if ( in_array( $hit->ID, $sticky_post_ids ) ) {
			$sticky[] = $hit;
		} else {
			$nonsticky[] = $hit;
		}
	}
	$hits[0] = array_merge( $sticky, $nonsticky );
	return $hits;
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Ajax Load More
Ajax Load More is an infinite scrolling plugin for WordPress. It has a Relevanssi extension, but with the current versions…
How to display relevancy score
Relevanssi stores the relevance score it uses to sort results in the $post variable. Just add something like <?php echo…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed