apply_filters( 'relevanssi_post_ok', boolean $post_ok, int $post_id )

Filters whether a post can be shown to the user or not.


(boolean) If true, the post can be included in the results. Default true.

(int) The post ID.

More information

In the process of searching, Relevanssi goes through each match it finds in the index. For each match, the weight is calculated, the match is passed through the relevanssi_match filter hook and then it passes through this filter hook. If this hook returns true – which is the default value – the match is then added to the list of matches found, but if the hook returns false, the match is discarded.

Relevanssi uses this filter hook to control user access to the posts. The index includes posts that shouldn’t be shown to all users: by default drafts and private posts are included, and those can’t be shown to users. So, for each match, Relevanssi checks if the user should be allowed to see the post, and discards the match if necessary.

Default behaviour

The default behaviour comes from the relevanssi_default_post_ok() function, which is hooked onto this hook with priority 9. Here’s what that function does:

  1. Relevanssi first checks the post status. If it’s not publish, the value is set to false.
  2. If the post is private, Relevanssi checks if the user has the capability required to read private posts in the post type (taken from $GLOBALS['wp_post_types'][<type>]->cap->read_private_posts). If the user has the capability or is the author of the post, set the value to true.
  3. If the post status is draft, pending or future and this is an admin search, set the value to true.

Post access plugins

Many plugins feature some kind of access control. Relevanssi has built-in support for Groups, MemberPress, Members, Paid Membership Pro, Restrict Content Pro, Simple Membership, User Access Manager, WP-Members, and WP JV Post Reading Groups.

Other plugins can be supported, as long as the plugin has some kind of method that will answer the question “is user X allowed to see post Y?”. The Relevanssi integration required is often very simple. Here’s the solution for MemberPress:

add_filter( 'relevanssi_post_ok', 'relevanssi_memberpress_compatibility', 10, 2 );

function relevanssi_memberpress_compatibility( $post_ok, $post_id ) {
	$post = get_post( $post_id );
	if ( MeprRule::is_locked( $post ) ) {
		$post_ok = false;

	return $post_ok;

If the MeprRule::is_locked( $post ) returns true, the post cannot be accessed by the current user, and Relevanssi won’t include it in the results.