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

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

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #22083
    custompczonecustompczone
    Participant

    Greetings,

    I have a question about editing the title of search results.

    Currently, I’m using Advanced custom fields to change how the title of posts appear. Using the code below, an <h2> tag is added to the title of each post that is listed based on the array value.

     <?php $trade_status = get_field('trade_status'); ?>
    			  <?php if( in_array('30', $trade_status) ): ?>
                     <h2 class="new">New</h2>
                  <?php endif; ?>
                  <?php if( in_array('20', $trade_status) ): ?>
                     <h2 class="active">Active</h2>
                  <?php endif; ?>
                  <?php if( in_array('10', $trade_status) ): ?>
                     <h2 class="closed">Closed</h2>
                  <?php endif; ?>
    			</div>

    Is there any way I can make ajax pro do the same? That is, customize the titles of the posts being displayed based on an ACF value. I believe similar code would be applicable for this if it’s possible.

    Thanks a lot,
    Adam

    #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.

    #22106
    custompczonecustompczone
    Participant

    You cannot access this content.

    #22117
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Okay, that is not going to work that way. Instead of that plugin, I would suggest maybe this one instead. This does not require file editing, as you can add the custom code snippets via the plugin back-end. That should very likely do the trick.

    #22139
    custompczonecustompczone
    Participant

    You cannot access this content.

    #22141
    custompczonecustompczone
    Participant

    You cannot access this content.

    #22147
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    Thank you, I think there was a minor error within the code, and I also changed the output to display the custom HTML before the title. Please try this one instead.

    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( $custom_field, $r->id, 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 . $r->title;
        }
      }
     
      return $results;
    }
    #22153
    custompczonecustompczone
    Participant

    Thank you x1,000,000 it works perfectly.

    You are the bomb. I really appreciate you taking the time to sort this out with me. You truly went above and beyond. OMW to write an A+ review now (if I haven’t already).

    Have a great week.

    Adam

    #22164
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.