• Resolved deepmessage

    (@deepmessage)


    Hello,

    Is it possible to hide pages from the search results if the user is not logged in or does not have the required role?

    Kind regards
    Alex

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Omar ElHawary

    (@omarelhawary)

    Hi @deepmessage,

    Thanks for reaching out Members Support Team!

    Please copy/paste PHP snippet below WP Code plugin or theme functions.php and let me know if it helps.

    function exclude_protected_from_search($query) {
    if ($query->is_search && !is_admin()) {
    $meta_query = array(
    array(
    'key' => '_members_access_role',
    'compare' => 'NOT EXISTS',
    ),
    );
    $query->set('meta_query', $meta_query);
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_protected_from_search');

    Regards,

    Thread Starter deepmessage

    (@deepmessage)

    Hi @omarelhawary,

    Unfortunately, the posts are now also hidden if you have the required role.

    Kind regards Alex

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.