Skip to main contentSkip to footer

There was an interesting support question:

I have website with logged-in users as well as non-logged-in users.

The logged-in user should be able to search through everything on the website, but the non-logged-in users should only be able to search the user profiles.

Is it at all possible to differentiate the searches like this?

The answer is, of course, yes, and it’s really quite simple, too. There’s no need to build two separate searches on the site. Every post that Relevanssi shows to users passes through the relevanssi_post_ok filter which determines whether the current user is allowed to see the post. This filter can be used to build this functionality. The code looks like this:

add_filter( 'relevanssi_post_ok', 'rlv_limit_users', 11, 2 );
function rlv_limit_users( $show, $post_id ) {
    if ( ! is_user_logged_in() ) {
        $post = relevanssi_premium_get_post( $post_id, get_current_blog_id() );
        if ( 'user' !== $post->post_type ) {
            $show = false;
        }
    }
    return $show;
}

Add this to your site. If the user is logged in (is_user_logged_in() returns true), nothing is done, and the posts are shown to the user as usual. If the user is not logged in, Relevanssi then fetches the post object using relevanssi_premium_get_post() (this is necessary instead of regular get_post(), because user profiles are involved) and if the post type is not set to user, the function returns false and the post is not shown to the user.

The code above requires Relevanssi Premium (and only makes sense with it, because you can’t search user profiles without Premium). If you want to apply this to other post types, this version is compatible with the free Relevanssi:

add_filter( 'relevanssi_post_ok', 'rlv_limit_post_types', 11, 2 );
function rlv_limit_post_types( $show, $post_id ) {
    if ( ! is_user_logged_in() ) {
        $post = get_post( $post_id );
        if ( 'your_custom_post_type' !== $post->post_type ) {
            $show = false;
        }
    }
    return $show;
}

Your account

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

Search

Popular Resources

Ultimate FAQs
Ultimate FAQs is a FAQ plugin that can create FAQs. It has a search, and enabling Relevanssi may break that…
Indexing trashed posts
By default, Relevanssi does not index trashed posts. If you want to enable searching for trashed posts in the WordPress…
ACF: Indexing relationship content

…field. The name of this field is the name // of your taxonomy connection field with an underscore prefix. $taxonomy_info_field_name = get_post_meta( $post->ID, ‘_taxonomy_connection’, true ); // Fetching the name of the taxonomy from the information field. global $wpdb; $taxonomy_field_info = $wpdb->get_var( $wpdb->prepare( “SELECT meta_value FROM $wpdb->postmeta WHERE meta_key =……add_filter( ‘relevanssi_content_to_index’, ‘rlv_relationship_content’, 10, 2 ); function rlv_relationship_content( $content, $post ) { global $wpdb; $relationships = unserialize( $wpdb->get_var( “SELECT meta_value FROM $wpdb->postmeta WHERE meta_key LIKE ‘sidebar_groups_%_people'” ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions foreach ( $relationships as $related_post_id ) { $content .= ‘ ‘ . get_the_title( $related_post_id ); } return $content; }……the post data by the relationship field. $relationships = get_post_meta( $post->ID, ‘relationship_field’, true ); if ( ! is_array( $relationships ) ) { $relationships = array( $relationships ); } foreach ( $relationships as $related_post ) { $content .= ‘ ‘ . get_the_title( $related_post ); } // Fetching the user data. $users…

Related Posts:

Comment Section:

3 Comments. Leave new

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