SearchWP

This Documentation is for SearchWP Version 3

Working with Advanced Custom Fields (ACF) Taxonomy Fields

By default Advanced Custom Fields stores Taxonomy field values as a serialized array of taxonomy term IDs in the database. ACF allows you to specify a return type (e.g. Term Object) in the settings, but because only the term IDs are stored in the database, SearchWP by default will index only those IDs.

Telling SearchWP to instead index what ACF works with is straightforward. Start by adding the following to your theme’s functions.php or a custom plugin:

<?php
// Tell SearchWP to index term names from ACF (Advanced Custom Fields) Taxonomy fields.
add_filter( 'searchwp_custom_fields', function( $meta_value, $meta_key, $the_post ) {
$acf_taxonomy_field_names = array( 'mytaxfield1', 'mytaxfield2' );
if ( ! in_array( $meta_key, $acf_taxonomy_field_names ) ) {
return $meta_value;
}
// Retrieve the value as per ACF's field settings.
$acf_field_object = get_field_object( $meta_key, $the_post->ID );
// We want to index the chosen terms names.
$meta_value = wp_list_pluck( $acf_field_object['value'], 'name' );
return $meta_value;
}, 20, 3 );
view raw functions.php hosted with ❤ by GitHub

Note: You will need to update line 5 to include all of the taxonomy field names (not labels) that you want to work with.

Once that’s done you can rebuild your index by clicking the Reset Index button on the Advanced tab of the SearchWP settings screen. Once rebuilt, you’ll be able to search by the actual values of your ACF Taxonomy fields!