Knowledge Base

Changing results URL

in Ajax Search Pro Tips Tags: change urlcustom fieldfilterfuncionresultsurl

To change the results url, you have to access one of the filters by adding a custom code snippet to the functions.php file in your active theme directory.

Change URL to custom field value

Copy-paste this code to the functions.php file:

add_filter( 'asp_results', 'asp_change_url_to_cf', 1, 1 );
 
function asp_change_url_to_cf( $results ) {
  // Change this to the custom field name, which stores the URL
  $custom_field = "custom_field_name";

  foreach ($results as $k=>$v) {
    if ( $v->content_type == "pagepost" ) {
      $url = get_post_meta($v->id, $custom_field, true);
      // Modify the post link
      if ( !empty($url) )
        $results[$k]->link  = $url;
    }
  }
  return $results;
}

Change the $custom_field variable value to the custom field name, that stores the URL of the result.