Posted on

Excluding protected posts

A site I’m working with has a handful of “protected” posts (password required to view). For various reasons, we don’t want to show these in search results. While there is no excerpt shown, I’d prefer for people to not even know they exist.

Relevanssi sees protected posts as public posts (because their post status is “publish”, not “private” like with private posts). That’s what they are, as in general users will be able to see protected posts. Just not their content, unless they know the password. Relevanssi does protect the content and won’t show parts of it in excerpts.

If you want to deindex protected posts completely, just add this code to the theme functions.php:

add_filter('relevanssi_do_not_index', 'rlv_exclude_protected', 10, 2);
function rlv_exclude_protected($exclude, $post_id) {
	$post = get_post($post_id);
	if (!empty($post->post_password)) $exclude = true;
	return $exclude;

This will prevent Relevanssi from indexing posts that have a password. Because of the way Relevanssi is built, versions earlier and including 1.13 in Premium and in free will not deindex public posts after they are protected. To get the posts out of the index, you’ll have to rebuild the whole index. Later versions will remove the posts from index if a password is added.

  • Daniel Howell

    Is it possible to use this to filter out virtual products?

    We have ticekts on our website and the events and the virtual ticket product both show up. I want to remove virtual products from showing up in search results.


    • Are the tickets a post type? If they are, then the best way to exclude them is to simply uncheck the post type from Relevanssi indexing settings.

      • Daniel Howell

        The events are a post type. But, tickets are products (virtual).

        • So they’re products, but you have other products you want to include in the index? Ok, in that case this filter is the way to go.