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
Hi,
Well, the advanced description field is not going to work in this case, it does not support PHP code for security reasons.
The only possibility is via custom code via the theme functions.php file. However it is still possible, that the shortcode might not be registered when the ajax request is finished, although in that case I think it should return an empty string, and not the shortcode name.
One thing to note, that the do_shortcode(..) function requires the shortcode to be in brackets. Anothe rissue I noticed in the code, is that the get_the_ID() function will not work there, as the ajax reqults are out of the wordpress loop.
Try this custom code, although I don’t know if this is going to work in any way, as I don’t know the exact content of the custom fields:
add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 );
function asp_custom_field_to_results( $results ) {
$custom_field = 'pricemonth'; // Enter the custom field name
$field = 'content'; // 'title' or 'content'
$position = 'before'; // 'before' or 'after'
$delimiter = ' '; // character between the field value and the 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 = 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;
}