Problem with custom searchable attributes

Hello community,

I try to make my custom fields searchable exactly like described here: https://community.algolia.com/wordpress/advanced-custom-fields.html. Sadly, without success. That’s why I wanted to ask, if somebody could confirm, that the method described in the guide works. I’m not using ACF, just get_post_meta(). The fields are pushed successfully to index and are there but I am not able to search in them. They are of string type.

Thank you in advance!

1 Like

Hi @aleksandr.epp,

Could you share with us all the custom code you implemented to make it work?

Looking forward to helping you,

Cheers,

1 Like

Hi rayrutjes,

ok, first I register my Controller Classes in the ‘init’ Hook. Then in on of the Controllers I define following functions and attach them to actions:

/**
 * Class AlgoliaSearchController
 * @package FindMe\Controller
 */
class AlgoliaSearchController extends BaseController
{
   /**
    * @param bool $shouldIndex
    * @param \WP_User $user
    *
    * @return bool
    */
   public function filterUser($shouldIndex, $user)
   {
       return false;
   }

   /**
    * @param bool $shouldIndex
    * @param \WP_Post $post
    *
    * @return bool
    */
   public function excludePostTypes($shouldIndex, $post)
   {
       // Add all post types you want to make searchable
       $allowedPostTypes = ['corporate', 'startup'];
       if (false === $shouldIndex) {
           return false;
       }

       if (\in_array($post->post_type, $allowedPostTypes, true)) {
           $publishProfile = \intval(\get_post_meta($post->ID, 'wpcf-publish-profile', true));
           if ($publishProfile === 1) {
               return true;
           }
           return false;
       }
   }

   /**
    * @param array $attributes
    * @param \WP_Post $post
    *
    * @return array
    */
   public function pushAttributesToIndex(array $attributes, \WP_Post $post)
   {
       $allowedPostTypes = ['corporate', 'startup'];

       if (\in_array($post->post_type, $allowedPostTypes)) {
           $attributes['website'] = \get_post_meta($post->ID, 'wpcf-website', true);
           $attributes['logoUrl'] = \get_post_meta($post->ID, 'wpcf-company-logo', true);
           $attributes['city'] = \get_post_meta($post->ID, 'wpcf-address-city', true);
           $attributes['backgroundImage'] = \get_post_meta($post->ID, 'wpcf-background-image', true);
           $hashTags = \get_post_meta($post->ID, 'wpcf-hashtag', false);
           if (\is_array($hashTags) && \sizeof($hashTags) > 0) {
               $attributes['hashtags'] = $hashTags;
               $attributes['hashtags_concat'] = \implode(' ', $hashTags);
           }
       }
       unset($attributes['post_author']);
       // Always return the value we are filtering.
       return $attributes;
   }

   /**
    * @param array $settings
    *
    * @return array
    */
   public function commonIndexSettings(array $settings)
   {
       unset($settings['attributesToIndex']);
       $settings['attributesToIndex'][] = 'unordered(post_title)';
       $settings['attributesToIndex'][] = 'unordered(content)';
       $settings['attributesToIndex'][] = 'unordered(hashtags)';
       $settings['attributesToIndex'][] = 'unordered(hashtags_concat)';
       $settings['attributesToIndex'][] = 'unordered(post_type_label)';

       $settings['attributesToSnippet'][] = 'hashtags_concat:50';
       // Always return the value we are filtering.
       return $settings;
   }

   /**
    * Register callbacks for actions and filters
    */
   public function registerHookCallbacks()
   {
       \add_filter('algolia_should_index_user', [$this, 'filterUser'], 10, 2);
       // Hook into Algolia to manipulate the post that should be indexed.
       \add_filter('algolia_should_index_searchable_post', [$this, 'excludePostTypes'], 10, 2);

       // push custom post fields to algolia index
       \add_filter('algolia_post_shared_attributes', [$this, 'pushAttributesToIndex'], 10, 2);
       \add_filter('algolia_post_corporate_shared_attributes', [$this, 'pushAttributesToIndex'], 10, 2);
       \add_filter('algolia_post_startup_shared_attributes', [$this, 'pushAttributesToIndex'], 10, 2);
       \add_filter('algolia_searchable_post_shared_attributes', [$this, 'pushAttributesToIndex'], 10, 2);

       // index custom post fields
       \add_filter('algolia_posts_index_settings', [$this, 'commonIndexSettings'], 10, 1);
       \add_filter('algolia_post_index_settings', [$this, 'commonIndexSettings'], 10, 1);
       \add_filter('algolia_posts_corporate_index_settings', [$this, 'commonIndexSettings'], 10, 1);
       \add_filter('algolia_searchable_post_index_settings', [$this, 'commonIndexSettings'], 10, 1);
   }
 }

Actually everything else but index settings works just fine.

I’m still not able to search in my custom attributes… Any idea? :frowning:

Ok, I solved the problem. The problem was, that I wasn’t able to push settings for searchable_posts and not for a specific post type. The documentation mentions algolia_posts_index_settings to achieve this and it didn’t work for me. In Algolia code I found the correct filter algolia_searchable_posts_index_settings. Now it works like a charm^^

1 Like