Title: Problems Indexing ACF Fields
Last modified: March 28, 2024

---

# Problems Indexing ACF Fields

 *  Resolved [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/)
 * Hi,
 * I have lots of information on my site being displayed using ACF fields. I’ve 
   followed the [post here](https://webdevstudios.com/2021/02/09/wp-search-with-algolia/#indexing-custom-fields)
   but seem to be missing something, as even after indexing and pushing the changes
   they’re not being indexed/shown in search results.
 * They are being used on a Custom Post Type, which is showing results in my search,
   but **only** looking at their name and excerpt to check content, but not my ACF
   fields.
 * What I’ve been working with is below, but I’m guessing I’m applying it wrong 
   or not understanding something.
 * Any help appreciated – thanks
 *     ```wp-block-code
       function wds_algolia_custom_fields( array $attributes, WP_Post $post ) {
   
       	// Eligible post meta fields.
       	$fields = [
       		'short_bio',
       		'practice_areas',
       		'qualifications',
       		'experience',
       	];
   
       	// Loop over each field...
       	foreach ( $fields as $field ) {
   
       	// Standard WordPress Post Meta.
       	$data = get_post_meta( $post->ID, $field );
   
       	// Advanced Custom Fields.
       	$data = get_field( $field, $post->ID );
   
       	// Only index when a field has content.
       	if ( ! empty( $data )  ) {
       		$attributes[ $field ] = $data;
       	}
       }
   
       return $attributes;
   
       }
       add_filter( 'algolia_post_shared_attributes', 'wds_algolia_custom_fields', 10, 2 );
       add_filter( 'algolia_searchable_post_shared_attributes', 'wds_algolia_custom_fields', 10, 2 );
       ```
   

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17535435)
 * As long as the attributes are getting into your indexed objects, the above code
   is correct.
 * The next thing to check would be if the fields are being marked as searchable.
   This would be in the “Searchable Attributes” section of the index’s configuration.
 * Looks like the post you linked to originally doesn’t have that step mentioned.
   However we have the same topic documented over on our GitHub wiki at [https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Advanced-Custom-Fields#make-custom-fields-searchable](https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Advanced-Custom-Fields#make-custom-fields-searchable)
 * If you’re not managing configuration settings via code, then marking the properties
   searchable can be done through your algolia.com dashboard.
 *  Thread Starter [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17535723)
 * Thanks for the reply.
 * I’m trying to add that via the Configuration in my Algolia Dashboard and can 
   see the options to add, but the options it gives me when I click “Add a Searchable
   Attribute” don’t seem to have any options relating to what I’ve done, so I’m 
   guessing I’ve missed a step.
 *  Thread Starter [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17535781)
 * I’ve updated what I’ve tried and this is now in my `functions.php`. I’m just 
   trying to get the ACF field ‘short_introduction’ as the first field I want to
   index to work, which is only on the custom_post_type “People”.
 * The way I’ve created my Fields is set a location rule in ACF, which is:
   **Show
   this field group if**Post Type > is equal to > PersonI’m not sure if that will
   have any impact on what I’m trying to do.
 *     ```wp-block-code
       /**
        * @param array   $attributes
        * @param WP_Post $post
        *
        * @return array
        */
   
       function my_post_attributes( array $attributes, WP_Post $post ) {
       	if ( 'people' !== $post->post_type ) {
       		// We only want to add an attribute for the 'speaker' post type.
       		// Here the post isn't a 'speaker', so we return the attributes unaltered.
       		return $attributes;
       	}
   
       	// Get the field value with the 'get_field' method and assign it to the attributes array.
       	// @see https://www.advancedcustomfields.com/resources/get_field/
       	$attributes['short_introduction'] = get_field( 'short_introduction', $post->ID );
   
       	// Always return the value we are filtering.
       	return $attributes;
       }
   
       add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
       add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );
   
   
       /**
        * @param array $settings
        *
        * @return array
        */
   
       function my_posts_index_settings( array $settings ) {
   
       	$settings['searchableAttributes'][] = 'unordered(short_introduction)';
   
       	return $settings;
       }
   
       add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );
       ```
   
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17535835)
 * When using the dashboard UI, when you click the “Add a Searchable Attribute” 
   button, you can start typing and it’ll autosuggest potential properties available.
   If you have the custom fields properly indexed as attributes, they should be 
   suggested with enough typing.
 * Looking at your functions.php code, it looks like we could use some updating 
   of our wiki page because it’s using post-type specific filter hooks. For example:`
   algolia_posts_speaker_index_settings`. which would be for autocomplete+speaker
   post type.
 * If you want this just for your `people` post type + autocomplete, I’d recommend
   switching that to `algolia_posts_people_index_settings` . If you’re also using
   Search/Instantsearch, I’d also use `algolia_searchable_posts_index_settings`
 * Also, you’d need to click the “push settings” button for these indexes, because
   simply saving the `my_posts_index_settings` function to your functions.php file
   won’t automatically make the changes into Algolia.
 *  Thread Starter [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17535877)
 * EDIT: Not sure what I’ve done and changed, but it seems to be working now. I 
   can search certain words in the field “Short Introduction” and they’re showing
   in results!
   Thank you for your help and showing that update from speaker -> people.
   That must have been the fix. One question I have is, is it possible to list out
   numerous fields to search in this way, like an example where I can add 8 different
   fields all on the People post type. So essentially `$attributes['short_introduction']
   = get_field( 'short_introduction', $post->ID );` in an array?
 * ~I feel everything is right with my functions file, and I’ve been pushing settings
   anytime I change anything, so I’m guessing this part is where I’m making a mistake.
   The amount of fields I can see in Searchable Attributes is always staying the
   same.~
 * > If you have the custom fields properly indexed as attributes, they should be
   > suggested with enough typing.
 * ~I’m wondering whether I’ve missed something obvious.~
    -  This reply was modified 2 years, 2 months ago by [tonyp83](https://wordpress.org/support/users/tonyp83/).
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17536009)
 * > One question I have is, is it possible to list out numerous fields to search
   > in this way, like an example where I can add 8 different fields all on the 
   > People post type.
 * Yes. I assume you mean something like below, where all the fields are pushed 
   into an array, and then that one array is stored in an attribute. In this case,“
   short_introduction”.
 *     ```wp-block-code
       function wds_algolia_custom_fields_array( array $attributes, WP_Post $post ) {
   
       	// Eligible post meta fields.
       	$fields = [
       		'short_bio',
       		'practice_areas',
       		'qualifications',
       		'experience',
       	];
   
       	$ourdata = []
       	foreach ( $fields as $field ) {
       		$data = get_field( $field, $post->ID );
       		if ( ! empty( $data ) ) {
       			$ourdata[] = $data;
       		}
       	}
   
       	$attributes['short_introduction'] = $ourdata[]
   
       	return $attributes;
       }
       add_filter( 'algolia_post_shared_attributes', 'wds_algolia_custom_fields_array', 10, 2 );
       add_filter( 'algolia_searchable_post_shared_attributes', 'wds_algolia_custom_fields_array', 10, 2 );
       ```
   
 *  Thread Starter [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17554142)
 * Thanks [@tw2113](https://wordpress.org/support/users/tw2113/),
 * I’ve updated my code to include all the fields (not just the field ‘short_introduction’)
   and it doesn’t seem to work.
 * I’ve updated my code based on your suggestion to the below but it isn’t working.
   This is:
 *     ```wp-block-code
       function my_post_attributes( array $attributes, WP_Post $post ) {
           if ( 'people' !== $post->post_type ) {
               return $attributes;
           }
   
           $attributes['short_introduction'] = get_field( 'short_introduction', $post->ID );
   
           return $attributes;
       }
       ```
   
 * Which is indexing `short_introduction` only. However, I want to index several
   fields and not just that.
   I’ve tried the below based on your suggestion but it
   doesn’t seem to work. I feel I’m maybe merging two methods.
 *     ```wp-block-code
       function my_post_attributes( array $attributes, WP_Post $post ) {
           if ( 'people' !== $post->post_type ) {
       		$fields = [
       			'short_introduction',
       			'skills_experience',
       			'awards',
       			'memberships',
       			'practice_areas',
       			'qualifications',
       			'experience',
       		];
   
       		$ourdata = [];
       		foreach ( $fields as $field ) {
       			$data = get_field( $field, $post->ID );
       			if ( ! empty( $data ) ) {
       				$ourdata[] = $data;
       			}
       		}
           }
   
           $attributes['people_fields'] = $ourdata;
   
           return $attributes;
       }
   
   
       /**
        * @param array $settings
        *
        * @return array
        */
   
       function my_posts_index_settings( array $settings ) {
   
       	$settings['searchableAttributes'][] = 'unordered(people_fields)';
   
       	return $settings;
       }
   
       add_filter( 'algolia_posts_people_index_settings', 'my_posts_index_settings' );
       ```
   
    -  This reply was modified 2 years, 2 months ago by [tonyp83](https://wordpress.org/support/users/tonyp83/).
    -  This reply was modified 2 years, 2 months ago by [tonyp83](https://wordpress.org/support/users/tonyp83/).
      Reason: tidied code
 *  Thread Starter [tonyp83](https://wordpress.org/support/users/tonyp83/)
 * (@tonyp83)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17554657)
 * I’ve tweaked this slightly and have it working now, and it’s picking up all my
   fields on the people page.
   Thanks for you help Michael 🙂
    -  This reply was modified 2 years, 2 months ago by [tonyp83](https://wordpress.org/support/users/tonyp83/).
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17554876)
 * Welcome

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Problems Indexing ACF Fields’ is closed to new replies.

 * ![](https://ps.w.org/wp-search-with-algolia/assets/icon-256x256.png?rev=2894668)
 * [WP Search with Algolia](https://wordpress.org/plugins/wp-search-with-algolia/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-search-with-algolia/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-search-with-algolia/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-search-with-algolia/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-search-with-algolia/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-search-with-algolia/reviews/)

## Tags

 * [ACF](https://wordpress.org/support/topic-tag/acf/)
 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * 9 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [2 years, 2 months ago](https://wordpress.org/support/topic/problems-indexing-acf-fields/#post-17554876)
 * Status: resolved