Trouble indexing custom fields from Wordpress to Algolia

Hi all. As the headline states, I’m having some trouble indexing custom fields from my Wordpress installation in Algolia. I’m all new to Algolia, but I’m making a PoC at my job for us to maybe switch to Algolia for all searching purposes.

Most of our Wordpress installations have a lot of custom fields (via Advanced Custom Fields Pro), so we don’t want to have to add every single field manually and hardcoded. I’m working on creating some “catch-all” code to automatically add all custom fields to Algolia. The fields are added fine to Algolia - that is, I can see them in my Algolia web interface under Indices. However, they are not indexed for searching.

This is my code:

add_filter( 'algolia_post_shared_attributes', 'my_post_attributes', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes', 10, 2 );

function my_post_attributes( array $attributes, WP_Post $post ) {

    $posttypes = get_post_types();

    foreach ($posttypes as $posttype) {
        if ( $posttype !== $post->post_type ) {
            continue;
        }

  	    $groups = acf_get_field_groups(array('post_type' => $posttype));
  	    foreach ($groups as $group) {
  	        $fields = acf_get_fields($group['key']);
  		foreach ($fields as $field) {
                if ($field['name'] !== '') {
                   $attributes[$field['name']] = get_field( $field['name'], $post->ID );
                }
  	        }
  	     }
      }

      return $attributes;
  }

  // add_filter( 'algolia_posts_member_index_settings', 'my_posts_index_settings' );
  add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );

function my_posts_index_settings( array $settings ) {

    // error_log(print_r($settings, TRUE));
    foreach ($posttypes as $posttype) {
      if ( $posttype !== $post->post_type ) {
         continue;
      }

      $groups = acf_get_field_groups(array('post_type' => $posttype));
      foreach ($groups as $group) {
         $fields = acf_get_fields($group['key']);
         foreach ($fields as $field) {
           if ($field['name'] !== '') {
            $settings['attributesToIndex'][] = 'unordered(' . $field['name'] . ')';
          }
        }
       }

       // $settings['attributesToIndex'][] = 'unordered(member_approach)';

    }
    return $settings;
  }

But as mentioned, even though the fields are visible in Algolia, I’m not able to search in them. I’ve also tried the very basic method described here of just adding one field manually (as you can see in the commented out code, but that does not work either): https://community.algolia.com/wordpress/advanced-custom-fields.html

Does anyone have experience with succesfully indexing the custom fields?

Thanks!

Hi @developer2,

From this statement below it sounds like you are successful in indexing the attributes in Algolia (i.e., you have pushed the data and they exist in your records) - is that correct?:

The fields are added fine to Algolia - that is, I can see them in my Algolia web interface under Indices.

If I understand correctly, now the issue is how can you make those attributes searchable.

In order to do this, have you tried updating the default settings of the index by using the filter algolia_searchable_posts_index_settings (see this discussion)?

You can take inspiration from this example and similarly add your custom attributes to the searchableAttributes parameter:

<?php

function custom_fields_searchable_attributes( array $settings ) {
    $settings['searchableAttributes'][] = 'my_custom_field';

    return $settings;
}
add_filter( 'algolia_searchable_posts_index_settings', 'custom_fields_searchable_attributes' );

Hope this helps

Hi @ajay.david

Yes, you are correct :slight_smile: The fields are indexed, but not searchable.

The filter algolia_searchable_posts_index_settings did indeed do the trick with some other minor alterations. Thanks! Now I have an issue with too many searchable attributes though, and an error in Instantsearch.js, but the initial problem is solved, so thank you for your help!

1 Like