Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Shortcode in customfield soes not show up in results › Reply To: Shortcode in customfield soes not show up in results
April 2, 2019 at 10:19 am
#21952
Keymaster
Hi,
1. Because this is a custom code, the value can be inserted either before or after the whole content/title fields. You can define that by changing the $position and $field variables according to the comments after them.
2. This code only works with a single field. Try this variation, with this, enter the field names to the $custom_field variable, as a comma separated list:
add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 );
function asp_custom_field_to_results( $results ) {
$custom_field = 'pricemonth, availability'; // Enter the custom field name
$field = 'content'; // 'title' or 'content'
$position = 'before'; // 'before' or 'after'
$delimiter = ' '; // character between the field value and the field
// -- Do not change anything below --
$fields = explode(',', $custom_field);
foreach ($results as $k=>&$r) {
if ($r->content_type != 'pagepost') continue;
foreach ( $fields as $cfield ) {
$cfield = trim($cfield);
if ( $cfield == '' )
continue;
if ( function_exists('get_field') )
$meta_value = get_field( $r->id, $cfield, true ); // ACF support
else
$meta_value = get_post_meta( $r->id, $cfield, true );
$meta_value = do_shortcode($meta_value);
// Modify the post title to add the meta value
if ( !empty($meta_value) ) {
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;
}