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 3.3.7.1 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.

8 comments Excluding protected posts

  1. 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.

    thanks

  2. You wrote: “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.”
    Am I right to assume that a searcher on our public website can search, retrieve a document that is password protected but must first type in the password before they can go to the actual document? And is this the same for PDFs?

    1. Frances, correct. Relevanssi will find password protected posts for everybody, but to see the post content, the user must enter the password. I’m not sure how it works for PDFs, but I’d assume it’s the same.

  3. I am wanting to hide ‘private’ posts from search results if the user isn’t logged in. At the moment the ‘private’ posts are indexed, so when a user clicks on them the page hangs.

    1. Renae, Relevanssi shouldn’t show private posts at all to users that are not logged in. How are you making those posts private? Are you using the WordPress default tools, or some third-party plugin?

Leave a Reply

Your email address will not be published. Required fields are marked *