Skip to main contentSkip to footer

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.

Your account

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

Search

Popular Resources

WooCommerce: Make Relevanssi notice stock changes

In some cases, WooCommerce product stock level changes don’t affect the search immediately. If the search is set up to exclude out-of-stock items, a product that sells out may still be included in the search until the next reindexing. This happens because changes in the stock status don’t trigger a…

Polylang attachment searching
If Polylang is in use and you haven’t enabled “Media translation“, your attachment files won’t have a language. That’s fine,…
Relevanssi indexes excluded posts

…use the Relevanssi admin search (Dashboard > Admin search). If you use the admin search and you’re still getting posts from the wrong post type, you can move on to the next step. If the admin search gets you the correct results but your theme front end search doesn’t, your…Sometimes it can happen the search results include posts from a post type that has been excluded from the index. When that happens, here are things you can check. Are you using Relevanssi? It’s possible your search results are not coming from Relevanssi. The quickest way to check is to……theme search is not using Relevanssi for some reason. Common causes for indexing the wrong post types The most common reason for index including the wrong post type is a shortcode or some other feature that displays posts and confuses Relevanssi. In the case that prompted this post, the search

Related Posts:

Comment Section:

14 Comments. Leave new

  • 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

    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)?

    Thanks,
    Mark

    Reply
    • 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.

      Reply
      • 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

        Reply
  • 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;
    }

    Reply
  • 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.

    Reply
    • 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.

      Reply
  • 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.

    Reply
    • 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?

      Reply
  • 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?

    Reply
    • 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.

      Reply
  • Daniel Howell
    March 20, 2016 11:44 pm

    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

    Reply

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