Posted on

Private posts in custom post types

Relevanssi has some problems when it comes to checking if it can show private posts to a user, if the post is in a custom post type.

Relevanssi doesn’t actually check the what the capability to read private posts is called, but instead assumes it’s read_private_slugs, where slug is the name of the post type.

That’s a problem, if the custom post type uses a different name for the capability. Sermon manager for WordPress, for example, uses a custom post type wpcf_sermon, but the capability is read_private_posts, not read_private_wpcf_sermons.

From Premium 1.13 and Relevanssi 3.3.6 onwards, you can do this to fix this:

add_filter('relevanssi_private_cap', 'private_sermon_cap');
function private_sermon_cap($cap) {
    if ($cap == 'read_private_wpfc_sermons') $cap = 'read_private_posts';
    return $cap;

In earlier versions, the fix is more complicated:

add_filter('relevanssi_post_ok', 'private_sermon_post_ok', 11, 2);
function private_sermon_post_ok($post_ok, $doc) {
	$status = relevanssi_get_post_status($doc);
	if ('private' == $status) {
		$type = relevanssi_get_post_type($doc);
		if ($type == 'wpfc_sermon') $type = "post";
		$cap = 'read_private_' . $type . 's';
		if (current_user_can($cap)) {
			$post_ok = true;
	return $post_ok;

This function will run after the default check, and if the post type is wpfc_sermon and the post is private, the function will check if the user is allowed to see the post.

4 comments Private posts in custom post types

  1. I have tried this code, but when I search I still see private posts in the search results. I have added the code via the Code Snippet plugin

    Is there an update to the above code?

    I have tried uninstalling/reinstalling the relevanssi plugin.

    I am using the Tooltypes plugins to create custom fields.

    1. Can you show me the code you’re using (put it in a Pastebin, for example)? What is the correct capability for reading your private post type? Without any code, Relevanssi tries to guess ’read_private_SLUGs’ where SLUG is the slug of your post type. If that doesn’t work, you need code that tells Relevanssi the correct capability.

      1. This is the code I am using.

        add_filter( ‘relevanssi_private_cap’, ‘tssupp_relevanssi_private_cap’ );
        function tssupp_relevanssi_private_cap( $cap ) {

        $cpt_slug = ‘job-application’; // Edit as required

        if ( $cap == ‘read_private_’ . $cpt_slug ) {
        $cap = ‘read_private_posts’;

        return $cap;

        1. The capability Relevanssi uses is ’read_private_’ . $cpt_slug . ’s’, the plural s must be in the end. That way your code should work, if read_private_posts is the correct capability.

Leave a Reply

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