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.

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

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

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.