Posted on

ACF: Indexing File field title

I use the ACF field type File for a download section, but the displayed file title is not indexing. The format I return the ACF field data is “File Array (array)”.

For Relevanssi, it doesn’t matter which ACF return format you choose. Relevanssi doesn’t use get_field() but instead uses the plain get_post_meta(). That always returns the post ID for File fields. To index the file title, you need to explain what you want using the relevanssi_custom_field_value filter hook. If you wish to the file title, this does the trick:

add_filter( 'relevanssi_custom_field_value', 'rlv_file_name', 10, 2 );
function rlv_file_name( $values, $field ) {
  if ( 'download_section' === $field ) {
    $values = array_map(
      function( $attachment_id ) {
        return get_the_title( $attachment_id );
      },
      $values
    );
  }
  return $values;
}

Assuming the field name is download_section, this indexes the attachment title.

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 *