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, add this code to your site:

add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_protected' );
function rlv_exclude_protected( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.post_password = ''";
	$restriction['reason'] .= ' Has a password';
	return $restriction;

Then just rebuild the index. This will prevent Relevanssi from indexing posts that have a password using the relevanssi_indexing_restriction filter hook.

If you want to keep the protected posts in the index but only show them to users who are logged in, you can use a filter function on relevanssi_post_ok like this:

add_filter( 'relevanssi_post_ok', 'rlv_search_exclude_protected', 10, 2 );
function rlv_search_exclude_protected( $allow, $post_id ) {
	$post = get_post( $post_id );
	if ( ! empty( $post->post_password ) ) {
		$allow = false;
		if ( is_user_logged_in() ) {
			$allow = true;
	return $allow;

Now protected posts are not shown to searchers who are not logged in.

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


  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?

  4. Can I exclude specific pages from Search? For example, I want to exclude the confirmation pages readers see after they subscribe to my mailing list.

    1. Stephanie, with Premium you can just check the “Exclude this from all searches” checkbox on the post edit page. In the free version, list the post ID in “Post exclusion” in the Searching tab.

  5. Thanks for this code. I needed to de-index WooCommerce product variations that belonged to a product that was not yet published. To remove those product variations I had to look at the posts parent status and exclude from there.

    add_filter(‘relevanssi_do_not_index’, ‘rlv_exclude_protected’, 10, 2);
    function rlv_exclude_protected( $exclude, $post_id ) {
    $post = get_post( $post_id );
    if( $post->post_type == ‘product_variation’ && get_post_status( $post->post_parent ) != ‘publish’ ) {
    $exclude = true;
    } else if ( $post->post_status != ‘publish’ ) {
    $exclude = true;
    return $exclude;

  6. Hi Mikko,

    I want to do similar, but not exactly the same.

    I want to exclude from search all content in specific e-courses (Learndash) on my site. So one client’s e-course doesn’t show up in search results for another different client.

    These aren’t protected posts (they’re all “public”) but users have to be signed in and allocated to the right e-course.

    I have [searchform ld_topic_category="xxx"] in place to search an entire e-course (where I set each post in that course to category ‘xxx’) and that works fine.

    But I guess I want a way of them NOT searching ld_topic_category=”xxx” in other search boxes on my site.

    Is there a way to add a negative term in the above shortcode, or exclude a learndash course from my main site search box (whilst still enabling the above code to work on the custom search box within that client’s e-course)?


    1. Mark, adding a category parameter like that is not a good solution in your case, because it’s super easy to circumvent by just removing the parameter from the URL or changing the value. It would be better – but slightly more difficult, sure – to limit the search results based on the access user has.

      So, what you need is a filter on the relevanssi_post_ok filter hook which is used to control who sees what. The filter function should check which e-courses the user is allowed to see and then limit the searches according to that. Here are some basic instructions, the exact details depend on how the permissions are handled in Learndash; I don’t know that so I can’t help you there. But the essence is that the relevanssi_post_ok will fire for each post found and you need to figure out if the current user is allowed to see the post or not and return true or false depending on that.

      1. Mikko, thanks so much for this. I’ll have a go and see where I get. It makes sense. Very impressed by the way with the plugin itself but also how you’re looking after your users. Stands out. Appreciated, Mark

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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