Custom URL for Results

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 5 months, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #45451
    adam-polyaire89
    adam-polyaire89
    Participant

    Hi, Can I use a custom URL for the results? I have AJAX search pro searching some custom post types. It works well. But I want to use a custom URL for some analytics for the search results. I have added this URL in an ACF field. Can AJAX use the custom URL for when the search result is clicked on?

    #45462
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Yes, but it requires a bit of a custom code snippet. In this knowledge base I explain how to do exactly what you are looking for. Let me know if you need any help 🙂

    Best,
    Ernest Marcinko

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


    #45470
    adam-polyaire89
    adam-polyaire89
    Participant

    thank you, is it possible to change this, code so that it only applies to 1 Ajax search, eg, just [wd_asp id=2]?

    // Remove the line below, if you don't want to affect the Live results list
    add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 );
    // Remove the line below, if you don't want to affect the results page
    add_filter( 'asp_noajax_results', 'asp_custom_link_results', 10, 4 );
    function asp_custom_link_results( $results, $search_id, $is_ajax, $args ) {
        // Replaces each result URL with this
        $replace_with = 'https://google.com';
    
        // --- DO NOT CHANGE ANYTHING BELOW ---
        foreach ($results as $k=>&$r) {
    		// Ajax results link
    		if ( isset($r->link) ) {
                $r->link = $replace_with;
            }
    		// Results page link
    		if ( isset($r->asp_guid) ) {
                $r->asp_guid = $replace_with;
            }
        }
    
        return $results;
    }
    #45472
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Yes, of course 🙂

    Here you go:

    // Remove the line below, if you don't want to affect the Live results list
    add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 );
    // Remove the line below, if you don't want to affect the results page
    add_filter( 'asp_noajax_results', 'asp_custom_link_results', 10, 4 );
    function asp_custom_link_results( $results, $search_id, $is_ajax, $args ) {
        // Replaces each result URL with this
        $replace_with = 'https://google.com';
    	$apply_to_search_ids = array(2); // Search IDs to apply the code to
    
        // --- DO NOT CHANGE ANYTHING BELOW ---
    	if ( in_array($search_id, $apply_to_search_ids) ) {
    		foreach ($results as $k=>&$r) {
    			// Ajax results link
    			if ( isset($r->link) ) {
    				$r->link = $replace_with;
    			}
    			// Results page link
    			if ( isset($r->asp_guid) ) {
    				$r->asp_guid = $replace_with;
    			}
    		}
    	}
    
        return $results;
    }
    Best,
    Ernest Marcinko

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


    #45487
    adam-polyaire89
    adam-polyaire89
    Participant

    Thank you, this worked perfectly. Great

    #45493
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.