Get search phrase at asp_cpt_results filter

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 Ernest Marcinko 5 days, 20 hours ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38651
    LHAS
    LHAS
    Participant

    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.

    • This topic was modified 1 week ago by LHAS .
    #38664
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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:

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38676
    LHAS
    LHAS
    Participant

    Hi Ernest,

    Thank you so much! Your sample code works well.
    If I have any more questions, I will ask again. Keep up good work!

    #38690
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are welcome 🙂 Let me know.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.