Posted on

ACF: Indexing ACF fields for taxonomy terms

Advanced Custom Fields makes it possible to add custom fields to taxonomy terms. You can only do this with Relevanssi Premium because the free version of Relevanssi can’t index taxonomy terms.

Creating a category field in ACF
Here’s how you would create a category field in ACF.

Suppose you set Relevanssi to index custom fields. Relevanssi won’t index these taxonomy custom fields because taxonomy terms aren’t generally allowed to have custom fields, and the default get_post_meta() function doesn’t work on taxonomy terms.

Getting the fields indexed requires a bit of custom programming. You need to add a function to the relevanssi_tax_term_additional_content filter hook that reads the field’s contents and adds that to the taxonomy term content. In most basic form, the function looks like this:

add_filter( 'relevanssi_tax_term_additional_content', 'rlv_term_fields', 10, 2 );
function rlv_term_fields( $content, $term ) {
    $post_id  = $term->taxonomy . '_' . $term->term_id;
    $content .= ' ' . get_field( 'category_field', $post_id );
    return $content;
}

First, you have to generate the post ID for the get_field() function. For taxonomy terms, it’s in the format of taxonomy_termID. Then use get_field() to get the field content and add it to the filter’s content variable. With simple text fields, that’s all you need; repeat the get_field() for all fields you have to include.

With more complicated fields, this too gets more complicated, as you have to process the fields and format their contents into a clean string.

Once you add this function and rebuild the index, Relevanssi includes the taxonomy term fields in the index. They are not included in excerpts, though, requiring another function hooked to the relevanssi_pre_excerpt_content hook. However, it’s almost the same as our original function, so with minor changes, we can use the same function for both purposes:

add_filter( 'relevanssi_tax_term_additional_content', 'rlv_term_fields', 10, 2 );
add_filter( 'relevanssi_pre_excerpt_content', 'rlv_term_fields', 10, 2 );
function rlv_term_fields( $content, $term ) {
    if ( ! isset( $term->term_id ) ) {
      return $content;    // not a taxonomy term, skip
    }
    if ( isset( $term->term_id ) && ! isset( $term->taxonomy ) ) {
        // this is excerpt-building, where the taxonomy is in $term->post_type
        $term->taxonomy = $term->post_type;
    }
    $post_id  = $term->taxonomy . '_' . $term->term_id;
    $content .= get_field( 'category_field', $post_id );
    return $content;
}

2 comments ACF: Indexing ACF fields for taxonomy terms

    1. Eric, with taxonomy terms, Relevanssi does not index custom fields, so relevanssi_custom_fields_before_repeaters is out of the question – it just doesn’t run, as there’s no specific custom field indexing. So, you need to do it all manually. You need to create a function on relevanssi_tax_term_additional_content that reads the field content.

      What you need is a basic ACF Repeater field loop:

      $rows = get_field( 'repeater_field_name' );
      $field_content = '';
      foreach ( $rows as $row ) {
      $field_content .= ' ' . $row['subfield'];
      }

      and so on. You can also use the have_rows(), the_row(), get_subfield() method. See instructions here.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *