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

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38651
    LHASLHAS
    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 3 years, 10 months ago by LHAS.
    #38664
    Ernest MarcinkoErnest 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;
    }
    #38676
    LHASLHAS
    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 MarcinkoErnest Marcinko
    Keymaster

    You are welcome 🙂 Let me know.

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