• Resolved tonyp83

    (@tonyp83)


    Hi,

    I have lots of information on my site being displayed using ACF fields. I’ve followed the post here 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

    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

    (@tw2113)

    The BenchPresser

    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

    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

    (@tonyp83)

    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

    (@tonyp83)

    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 > Person

    I’m not sure if that will have any impact on what I’m trying to do.

    /**
     * @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

    (@tw2113)

    The BenchPresser

    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

    (@tonyp83)

    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 12 months ago by tonyp83.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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”.

    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

    (@tonyp83)

    Thanks @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:

    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.

    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 11 months, 3 weeks ago by tonyp83.
    • This reply was modified 11 months, 3 weeks ago by tonyp83. Reason: tidied code
    Thread Starter tonyp83

    (@tonyp83)

    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 11 months, 3 weeks ago by tonyp83.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Problems Indexing ACF Fields’ is closed to new replies.