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 ) . '">';

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk

Comments are closed. Continue the conversation with me on Twitter: @billerickson