Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Distance Filters › Reply To: Distance Filters
June 12, 2020 at 2:26 pm
#27830
Keymaster
Thanks. The custom code was almost working perfectly, I adjusted it a bit to give the same output as the advanced title field. I removed the address_field from the advanced title field, as it is no longer needed there.
The final code is:
add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 );
function asp_custom_field_to_results( $results ) {
$custom_fields = 'address_field'; // Enter the custom field names, comma separated
$field = 'title'; // 'title' or 'content'
$position = 'after'; // 'before' or 'after'
$delimiter = ' '; // character between the field value and the field
$fields = explode(',', $custom_fields);
foreach ( $fields as $custom_field ) {
$custom_field = trim($custom_field);
foreach ($results as $k=>&$r) {
if ($r->content_type != 'pagepost') continue;
if ( function_exists('get_field') )
$meta_value = get_field( $r->id, $custom_field, true ); // ACF support
else
$meta_value = get_post_meta( $r->id, $custom_field, true );
$meta_value = maybe_unserialize($meta_value);
if ( is_array($meta_value) ) {
$meta_value = implode(', ', $meta_value);
}
// Modify the post title to add the meta value
if ( $meta_value != '' ) {
$meta_value = '<p>Location: ' . $meta_value . '</p>';
if ( $field == 'title' ) {
if ( $position == 'before' )
$r->title = $meta_value . $delimiter . $r->title;
else
$r->title .= $delimiter . $meta_value;
} else {
if ( $position == 'before' )
$r->content = $meta_value . $delimiter . $r->content;
else
$r->content .= $delimiter . $meta_value;
}
}
}
}
return $results;
}