Skip to main contentSkip to footer

Sometimes it may be necessary to restrict the Did you mean suggestions Relevanssi serves to just one post type. There’s no option for that, as by default the Relevanssi database the Did you mean suggestions use as a source (this only applies to Premium, that is) doesn’t have any information about the post types the words are related to.

However, it does have the post ID, which can be linked to a post type, and there’s a filter hook that lets us modify the query that fetches the words from the database. Then it’s just a question of formulating some extra MySQL code to restrict the words fetched to a particular post type.

Add this code to your theme functions.php to restrict the Did you mean searches (in Premium only) to the current post type restriction:

add_filter('relevanssi_get_words_query', 'rlv_dym_post_type');
function rlv_dym_post_type($q) {
	global $wpdb, $wp_query;
	if (isset($wp_query->query_vars['post_type'])) $post_type = $wp_query->query_vars['post_type'];
	if (empty($post_type)) $post_type = "any";
	if ($post_type != "any") {
		$addition = ' AS rr, ' . $wpdb->posts . ' AS pp WHERE rr.doc = pp.ID AND pp.post_type = "' . $post_type . '" GROUP';
		$q = str_replace('GROUP', $addition, $q);
	}
	return $q;
}

Your account

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

Search

Popular Resources

Indexing attachment file names
Relevanssi has been working nicely for the normal usecase. But how does one setup indexing of attachment files. When someone…
Debugging slow searches

Reasonable expectations for Relevanssi searching speeds are measured in seconds. Relevanssi does not generally return results in milliseconds. On the other hand, the searches shouldn’t take more than few seconds in most cases. Multisite searches are a different thing: even if the searching is reasonably fast, searching across many sites……should be close to the number of posts found. If there’s a big difference, that’s often a performance problem. If your site does 3000 queries every time you search, that’s going to take time even if those queries are fast. (See Performance issues here for avoiding common pitfalls.) Meta query…

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