This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Show only when logged in Option

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Show only when logged in Option

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37338
    seowerkseowerk
    Participant

    Hey there,

    short question. When I create a membership area on my page with a plugin and I want only the logged in member to find some results but not the normal user…how can I do that in ajax search pro?

    Im looking for a “show only when logged in” option for the search results.

    Thank you!

    #37341
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    Well, there is no such option, but there is a programmatical way to exclude posts/pages/other cpt by their IDs, when the user is not logged in.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    You can add the unwanted post IDs to the $ids array variable.

    add_filter( 'asp_query_args', 'asp_exclude_only_post_ids', 10, 2 );
    function asp_exclude_only_post_ids( $args, $id ) {
      /**
       * Array of post IDs to exclude for non-logged in users
       */     
      $ids = array(1, 2, 3);
      
      // -- !! Do not change anything below this line !! --
      if ( !is_user_logged_in() ) {
    	$args['post_not_in'] = array_merge($args['post_not_in'], $ids);
      }
    
      return $args;
    }
    #37365
    seowerkseowerk
    Participant

    Hello Ernest,

    thank you for your quick answer. I tried it and added the IDs of the product categories in the $ids array.
    Then I recreated the index.

    Does this only work with posts or also with CPTs and custom Taxonomies?
    In the live search there are still those results when I am not logged in.
    Thanks for your help.
    Dominik

    #37367
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Dominik,

    This only works with custom post types. If you want to exclude taxonomy term results, use this variation instead:

    add_filter( 'asp_query_args', 'asp_exclude_only_post_ids', 10, 2 );
    function asp_exclude_only_post_ids( $args, $id ) {
      /**
       * Array of post IDs to exclude for non-logged in users
       */     
      $post_type_ids = array(1, 2, 3);
      
      /**
       * Array of term IDss to exclude for non-logged in users
       */     
      $tax_term_ids = array(1, 2, 3);
      
      // -- !! Do not change anything below this line !! --
      if ( !is_user_logged_in() ) {
    	$args['post_not_in'] = array_merge($args['post_not_in'], $post_type_ids);
    	$args['taxonomy_terms_exclude'] = array_merge($args['taxonomy_terms_exclude'], $tax_term_ids);
      }
    
      return $args;
    }
    #37368
    seowerkseowerk
    Participant

    Ah ok, it worked with the normal CPT post. Taxonomy is still weird.

    But for now, this is good for me.
    Thank you!!

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