Skip to main contentSkip to footer

Restrict search by author from the search query

You can set Relevanssi to index the post author, which will then let you find posts by searching for the name of the author. But what if you want a stricter filter? Where searching for an author would only show posts by that one author and nothing else? That’s possible too.

Add this function to your site to implement stricter author filtering. Whenever a search is made, Relevanssi will fetch a list of authors on your site and then will compare the list of authors against the search query. If a match is made, Relevanssi will then add the author ID to the author parameter and remove the author name from the search query. The search will only match one author, the first one it comes across.

add_filter( 'relevanssi_modify_wp_query', 'rlv_author_filter' );
function rlv_author_filter( $query ) {
	$author_list = get_transient( 'rlv_author_filter_author_list' );
	if ( ! $author_list ) {
		$args        = array(
			'show_fullname' => false,
			'hide_empty'    => true,
			'echo'          => false,
			'style'         => '',
			'html'          => false,
			'exclude_admin' => false,
		);

		$author_list = wp_list_authors( $args );
		set_transient( 'rlv_author_filter_author_list', $author_list, WEEK_IN_SECONDS );
	}
	$authors = explode( ', ', $author_list );

	foreach ( $authors as $author ) {
		if ( false !== mb_stristr( $query->query_vars['s'], $author ) ) {
			$args= array(
				'search'        => $author,
				'search_fields' => array( 'display_name' )
			);
			$user_query = new WP_User_Query( $args );
			$results    = $user_query->get_results();
			if ( ! empty( $results) ) {
				$query->query_vars['s'] = str_ireplace( $author, '', $query->query_vars['s'] );
				$query->query_vars['author'] = $results[0]->ID;
				break;
			}
		}
	}

	return $query;
}

There’s a small lesson in the power of caching here as well. Notice the $author_list is taken from a transient, and only if it doesn’t exist, it’s generated with wp_list_authors(). The transient is stored for a week – because in most cases the list of authors is fairly stable – so the function needs to be called just once per week.

Why is this? I noticed in my tests that adding that one function call – just a single function – actually generated 200 database queries. A search without the user filter generated ~50 queries, a search with the filter did ~250 queries. Your mileage may vary, depending on the number of authors on your site for example, but it’s clear that is potentially a very expensive function. Since it produces output that’s stable and easy to cache, it’s a very good example of how you can optimize and cache using the transients.

If you have a very stable list of authors, it’s not a bad idea to remove the whole affair with wp_list_authors() and the transients, and instead just list all the authors you want to include directly as the $authors array.

Your account

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

Search

Popular Resources

Boosting shorter posts with higher keyword density

…15 search term appearances might well be a better match for the search than a 2000-word post with 20 search term appearances, as the density is much higher in the shorter post. One way to make Relevanssi give a boost to shorter posts is the add a consideration for the…By default, Relevanssi tends to prefer longer posts. The default TF × IDF weights Relevanssi uses simply count the term frequency, ie. how many times a word appears in the post. That prefers longer posts as they usually have the search term appear more often. However, a 500-word post with…

WordCamp Nordic 2019
Relevanssi Premium is a proud sponsor of the inaugural WordCamp Nordic. There have been several WordCamps in Nordic countries, but…
Index custom field contents
Custom fields (also known as post meta or meta fields) are a major part of WordPress the default search won’t…

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