Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Get search phrase at asp_cpt_results filter
This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko 6 months, 1 week ago.
- AuthorPosts
- August 2, 2022 at 10:22 am #38651
Hey,
https://knowledgebase.ajaxsearchpro.com/hooks/filters/search-results/asp_cpt_results
I have a question about asp_cpt_results filter.
I am trying to get the s (Search Phrase) in $args in the function, but I can’t. How can I get the s (Search Phrase) in $args?
The following is a sample script from the above URL with $args[‘s’] added.
add_filter( 'asp_cpt_results', 'asp_cpt_result_filter', 10, 3 ); function asp_cpt_result_filter( $results, $search_id, $args ) { $link = 'https://www.google.com/'; // Link to use, when not logged in $link = $args['s']; // ADD // Parse through each result item foreach ($results as $k=>&$r) { /** * $r (stdClass object) { * 'id' -> Post or other result object (taxonomy term, user etc..) ID, * 'title' -> Result title * 'content' -> Result content * 'post_type' -> Result post type (if available) * 'content_type' -> Content type (pagepost, user, term, attachment etc..) * } **/ if ( !is_user_logged_in() ) $r->link = $link; } return $results; }
Thanks in advance.
August 3, 2022 at 2:49 pm #38664Hi,
I just checked, and there is an error in the core code with that specific filter. The wrong variable is passed, and so the args are not accessible. You can however use this filter instead:
Best,add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 ); function asp_custom_link_results( $results, $search_id, $is_ajax, $args ) { $link = $args['s']; // Parse through each result item foreach ($results as $k=>&$r) { // Only post type results if ( isset($r->post_type) ) { // Do your thing here } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
August 4, 2022 at 3:06 am #38676Hi Ernest,
Thank you so much! Your sample code works well.
If I have any more questions, I will ask again. Keep up good work!August 4, 2022 at 12:55 pm #38690You are welcome 🙂 Let me know.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.