Manually promote featured content in search results

We use SearchWP, an advanced site-search plugin for WordPress, on every site we build that includes a search form (here’s why).

The relevancy-based search works great in most instances, but sometimes there may be a specific result you want to promote to the top for a specific search term. That’s where the Custom Results Order addon comes in.

The most common search term on my site is “gutenberg”, but my Developer’s Guide to Gutenberg didn’t appear as the first result. Now with the Custom Results Order extension, it will always appear as the first result for “gutenberg” searches.

Styling promoted results

I think it’s a good idea to style promoted search results. It helps illustrate to visitors that this is a promoted result, and it draws more attention to it, which was the point of promoting it in the first place.

You can use the searchwp_cro_get_promoted( $query, $engine ) function to get a list of Post IDs that are promoted. $query is the current search query, and $engine is the SearchWP engine being used (this will be ‘default’ unless you’re using a supplemental engine).

In my template partial responsible for search results, I build an array of default post classes, check if this post is promoted, and if it is I add a class of ‘promoted’. I then style this differently.

$classes = array( 'post-summary', 'search-result' );
if( function_exists( 'searchwp_cro_get_promoted' ) ) {
	$promoted = searchwp_cro_get_promoted( get_search_query(), 'default' );
	if( in_array( get_the_ID(), $promoted ) )
		$classes[] = 'promoted';
}

echo '<article class="' . join( ' ', $classes ) . '">';

SearchWP WordPress Development

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Leave A Reply