Skip to main contentSkip to footer

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;
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Co-authors Plus
Co-Authors Plus is a neat plugin that makes it possible to have multiple authors for one post in WordPress. This…
Avada
Avada is one of the most popular WordPress themes on the market. All post types are not found Avada has…

Related Posts:

Comment Section:

2 Comments. Leave new

  • Eric Kazda
    May 5, 2022 8:52 pm

    How would I go about adding Repeater fields to this setup? I’m assuming it uses some principle discussed at https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_custom_fields_before_repeaters/, but I’m not sure how to implement this properly for taxonomies.

    Reply
    • 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.

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed