Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Results suggestions – how to add more than post tile in the results › Reply To: Results suggestions – how to add more than post tile in the results
Hi,
Thank you for your kind words!
Well, if the state_code exists on both post types, then it will display unfortunately for both, there is no conditional logic to that in the options.
However, this might be better to solve programmatically instead of using the advanced title/content fields, as if you change your mind or want to make it more complicated it will get worse.
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
add_filter( 'asp_results', 'asp_custom_link_meta_results', 10, 4 );
function asp_custom_link_meta_results( $results, $search_id, $is_ajax, $args ) {
// Change this variable to whatever meta key you are using
$meta_key = 'state_code';
$post_type = 'cities';
// --- no changes below
foreach ($results as $k=>&$r) {
if ( isset($r->post_type) && $r->post_type == $post_type ) {
if ( function_exists('get_field') )
$state_code = get_field( $meta_key, $r->id, true ); // ACF support
else
$state_code = get_post_meta( $r->id, $meta_key, true );
if ( !empty($state_code) ) {
$r->title .= " " . $state_code;
}
}
}
return $results;
}
I this code I assumed the post type name is cities and the meta field name is state_code. You may have to adjust that on lines 5-6 to make sure it is correct.
If you need help with the code let me know, I will add it for you.