• Resolved mikeywil

    (@mikeywil)


    Great plugin! Thanks.

    I’m using custom fields and have successfully indexed them and my search results are working with an excellent quality of ranking. I’m displaying the custom fields in the excerpt (using the plugin’s option).

    My custom fields are for an index of articles from journals. So, each article has fields for ‘author’, Journal’, ‘page’, ‘volume’, and ‘date’ (along with other fields). On the single custom page I can display a label next to these so it looks like:

    Author: Smith, John
    Journal: KJ373
    Page: 7
    Volume: 92
    Date: May-2018

    In the excerpt on the search results it looks like this:

    Smith, John 92 7 KJ373 May-2018…

    Is there a way I can make the excerpt results look like this:

    Smith, John Vol:92 Journal:KJ373 Page:7 May-2018…

    (the change of order is a nice-to-have).

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Relevanssi is doing stupid automatic work there. If you want nice formatting, you need to build the excerpts yourself in the search results template. That way you can control the formatting and get the precise results you want.

    Another approach is to disable the automatic custom field feature in the Relevanssi excerpt settings, and then add a relevanssi_excerpt filter hook that adds the custom fields:

    add_filter( 'relevanssi_excerpt', 'rlv_add_fields', 10, 2 );
    function rlv_add_fields( $excerpt, $post_id ) {
      $author  = get_post_meta( $post_id, 'author', true );
      $journal = get_post_meta( $post_id, 'journal', true );
      $page    = get_post_meta( $post_id, 'page', true );
      $volume  = get_post_meta( $post_id, 'volume', true );
      $date    = get_post_meta( $post_id, 'date', true );
    
      $fields  = "$author Vol: $volume Journal: $journal Page: $page $date";
      $excerpt = $fields . ' ' . $excerpt;
    
      return $excerpt;
    }

    Something like that.

    Thread Starter mikeywil

    (@mikeywil)

    Thanks for quick response – that looks really useful.

    For my site I need the search to be able to return results for a range of pages and various different custom post types. I presume I can add a check to that code so that it only fires for specific custom post types?

    Plugin Author Mikko Saari

    (@msaari)

    Yes. Just check for the post type in the function:

    add_filter( 'relevanssi_excerpt', 'rlv_add_fields', 10, 2 );
    function rlv_add_fields( $excerpt, $post_id ) {
      if ( 'cpt' === relevanssi_get_post_type( $post_id ) ) {
        $author  = get_post_meta( $post_id, 'author', true );
        $journal = get_post_meta( $post_id, 'journal', true );
        $page    = get_post_meta( $post_id, 'page', true );
        $volume  = get_post_meta( $post_id, 'volume', true );
        $date    = get_post_meta( $post_id, 'date', true );
    
        $fields  = "$author Vol: $volume Journal: $journal Page: $page $date";
        $excerpt = $fields . ' ' . $excerpt;
      }
    
      return $excerpt;
    }

    and so on.

    Thread Starter mikeywil

    (@mikeywil)

    Fantastic stuff. Works really well.

    Many thanks.

    (p.s. I will be asking a pre-sales question for the pro version via your contact form shortly)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can we add names or labels to custom fields used in excerpts’ is closed to new replies.