• Resolved phoenixbelle1

    (@phoenixbelle1)


    I have restricted all the products etc to Trade Users but using the Search function makes them visible. Is there a way around this? Or do I need to remove Search. My client doesn’t want unregistered users to see the prices. Other than that it is working well.

Viewing 1 replies (of 1 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @phoenixbelle1

    The best way to hide posts or products on search results is to use pre_get_posts filter hook. Here is an example of the code that prevents protected regular posts from being displayed in search results:

    add_action( 'pre_get_posts', function ( $query ) {
      if ( ! is_admin() && $query->is_main_query() && 'blog' === $query->query['pagename'] ) {
        $posts = get_posts(array('post_type' => 'post', 'post_status' => 'publish', 'numberposts' => -1));
        if(empty($posts)) { return; }
        $protected_posts = array();
        foreach($posts as $post) {
         if(!members_can_current_user_view_post($post->ID)) {
           $protected_posts[] = $post->ID;
         }
        }
    
        if(empty($protected_posts)) { return; }
    
        $query->set( 'post__not_in', $protected_posts );
      }
    } );

    We don’t have any code to hide products on search result, so you will need to modify it.

    Best

Viewing 1 replies (of 1 total)
  • The topic ‘Search Results’ is closed to new replies.