How to change the results URL to something else?

Replacing with a static URL

1
// Remove the line below, if you don't want to affect the Live results list
2
add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 );
3
// Remove the line below, if you don't want to affect the results page
4
add_filter( 'asp_noajax_results', 'asp_custom_link_results', 10, 4 );
5
function asp_custom_link_results( $results, $search_id, $is_ajax, $args ) {
6
// Replaces each result URL with this
7
$replace_with = 'https://google.com';
8
9
// --- DO NOT CHANGE ANYTHING BELOW ---
10
foreach ($results as $k=>&$r) {
11
// Ajax results link
12
if ( isset($r->link) ) {
13
$r->link = $replace_with;
14
}
15
// Results page link
16
if ( isset($r->asp_guid) ) {
17
$r->asp_guid = $replace_with;
18
}
19
}
20
21
return $results;
22
}
Copied!

Replacing multiple different URLs within the results

Enter the values to the $replace variable on line 7. On each line, the left side is the value to replace, with the value on the right side.
1
// Remove the line below, if you don't want to affect the Live results list
2
add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 );
3
// Remove the line below, if you don't want to affect the results page
4
add_filter( 'asp_noajax_results', 'asp_custom_link_results', 10, 4 );
5
function asp_custom_link_results( $results, $search_id, $is_ajax, $args ) {
6
// Array of original -> replacement links
7
$replace = array(
8
'websiteurl1.com' => 'google.com',
9
'websiteurl2.com' => 'wikipedia.com',
10
'websiteurl3.com' => 'bing.com'
11
);
12
13
// --- DO NOT CHANGE ANYTHING BELOW ---
14
foreach ($results as $k=>&$r) {
15
$link = isset($r->link) ? $r->link : (isset($r->asp_guid) ? $r->asp_guid : '');
16
if ( $link == '' )
17
continue;
18
$link = str_replace(array_keys($replace), array_values($replace), $link);
19
20
// Ajax results link
21
if ( isset($r->link) ) {
22
$r->link = $link;
23
}
24
// Results page link
25
if ( isset($r->asp_guid) ) {
26
$r->asp_guid = $link;
27
}
28
}
29
30
return $results;
31
}
Copied!

Parsing the url from post meta

1
// Remove the line below, if you don't want to affect the Live results list
2
add_filter( 'asp_results', 'asp_custom_link_meta_results', 10, 4 );
3
// Remove the line below, if you don't want to affect the results page
4
add_filter( 'asp_noajax_results', 'asp_custom_link_meta_results', 10, 4 );
5
function asp_custom_link_meta_results( $results, $search_id, $is_ajax, $args ) {
6
// Change this variable to whatever meta key you are using
7
$meta_key = 'custom_url';
8
9
// --- DO NOT CHANGE ANYTHING BELOW ---
10
foreach ($results as $k=>&$r) {
11
if ( function_exists('get_field') )
12
$new_url = get_field( $meta_key, $r->id, true ); // ACF support
13
else
14
$new_url = get_post_meta( $r->id, $meta_key, true );
15
16
// Change only, if the meta is specified
17
if ( !empty($new_url) ) {
18
// Ajax results link
19
if ( isset($r->link) ) {
20
$r->link = $new_url;
21
}
22
// Results page link
23
if ( isset($r->asp_guid) ) {
24
$r->asp_guid = $new_url;
25
}
26
}
27
}
28
29
return $results;
30
}
Copied!
Last modified 1yr ago