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

Reply To: Custom URL link for results quit working for a Search Instance

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Custom URL link for results quit working for a Search Instance Reply To: Custom URL link for results quit working for a Search Instance

#55960
Ernest MarcinkoErnest Marcinko
Keymaster

Hi!

I assume you had a custom code in place to change the URLs, but I couldn’t find it anywhere.

I have added the Code Snippets plugin and added the following custom code to fetch the news_url custom field:

add_filter( 'asp_results', function ( $results, $search_id, $is_ajax, $args ) {
	// Change this variable to whatever meta key you are using
	$meta_key = 'news_url';

	// --- DO NOT CHANGE ANYTHING BELOW ---
	foreach ($results as $k=>$r) {
		if ( function_exists('get_field') ) {
			$new_url = get_field( $meta_key, $r->id, true ); // ACF support
		} else {
			$new_url = get_post_meta( $r->id, $meta_key, true );
		}
		
		// Change only, if the meta is specified
		if ( !empty($new_url) ) {
			// Ajax results link
			if ( isset($r->link) ) {
				$r->link = $new_url;
			}
		}
	}

	return $results;
}, 10, 4 );