Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Show only when logged in Option
- This topic has 4 replies, 2 voices, and was last updated 4 years, 2 months ago by
seowerk.
-
AuthorPosts
-
March 30, 2022 at 11:24 am #37338
seowerk
ParticipantHey 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!
March 30, 2022 at 4:52 pm #37341Ernest Marcinko
KeymasterHi!
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; }April 1, 2022 at 12:10 pm #37365seowerk
ParticipantHello 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.
DominikApril 1, 2022 at 12:16 pm #37367Ernest Marcinko
KeymasterHi 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; }April 1, 2022 at 12:56 pm #37368seowerk
ParticipantAh ok, it worked with the normal CPT post. Taxonomy is still weird.
But for now, this is good for me.
Thank you!! -
AuthorPosts
- You must be logged in to reply to this topic.