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

Reply To: Adjust title of posts in search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Adjust title of posts in search results Reply To: Adjust title of posts in search results

#22091
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

Yes, that is possible as well via a custom code. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 ); 
function asp_custom_field_to_results( $results ) {
  $custom_field = 'trade_status'; 

  foreach ($results as $k=>&$r) {
    if ($r->content_type != 'pagepost') continue;
    if ( function_exists('get_field') )
        $trade_status  = get_field( $r->id, $custom_field, true ); // ACF support
    else
        $trade_status  = get_post_meta( $r->id, $custom_field, true );
    // Modify the post title to add the meta value
    if ( !empty($trade_status) ) {
      if ( in_array('30', $trade_status) ) {
        $html = '<h2 class="new">New</h2>';
      } else if ( in_array('20', $trade_status) ) {
        $html = '<h2 class="active">Active</h2>';
      } else if ( in_array('10', $trade_status) ) {
        $html = '<h2 class="closed">Closed</h2>';
      } else {
        $html = '';
      }
      $r->title .= $html;
    }
  }
 
  return $results;
}

I did not test this code, but it should be very close to a possible solution.