• Resolved tonyp83

    (@tonyp83)


    Hi,

    This is a follow-up to this post, which solved the initial issue.

    So i’m currently using the below code to index all my the_field instances in ACF I want, which works fine.

    I have a builder in ACF, which has the_field of content_page_builder and inside has several the_sub_field which I can’t get to index. Is there a way to get these sub fields to index? I guess it will be slightly different in the foreach perhaps? (simply because it mentions $data = get_field( $field, $post->ID );.

    function my_post_attributes( array $attributes, WP_Post $post ) {
    
    	$fields = [
    		'short_introduction',
    		'practice_areas',
    		'qualifications',
    		'skills_experience',
    		'awards',
    		'memberships',
    		'hero_image',
    		'hero_text',
    		'content_page_builder', //the_field
    		'text_block', // the_sub_field - not indexing
    		'narrow_text_block', // the_sub_field - not indexing
    		'alternating_call_to_actions', // the_sub_field - not indexing
    	];
    
    	$ourdata = [];
    	foreach ( $fields as $field ) {
    		$data = get_field( $field, $post->ID );
    		if ( ! empty( $data ) ) {
    			$ourdata[] = $data;
    		}
    	}
    
        // Get the field value with the 'get_field' method and assign it to the attributes array.
    	$attributes['acf_fields'] = $ourdata;
    
        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(acf_fields)';
    
    	return $settings;
    }
    
    add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );
    

    Many thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I suspect you’d need to be using functions like the_sub_field() and potentially set things up with have_rows() etc, for that part of things.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    @tonyp83 did my notes about subfields and have_rows help out at all?

    Thread Starter tonyp83

    (@tonyp83)

    @tw2113 so I felt like I made some progress and have got in some instances Algolia to index content with my the_sub_field with my local version – but when I try and use on production (which has 51 pages already created) it deletes everything in the index so for wp_posts_page it shows 0 records. When I remove my function below and re-index it goes back to 51 pages, but no custom fields at all.

    So something seems to be breaking the index process it seems.

    function my_post_attributes_with_sub_field( array $attributes, WP_Post $post ) {
    
    	$fields = [
    		'hero_image',
    		'content_page_builder',
    	];
    
    	$ourdata = [];
    	foreach ( $fields as $field ) {
    		$datas = get_field( $field, $post->ID );
    		if ( ! empty( $datas ) ) {
    			foreach ($datas as $data) {
    				if ( ! empty( $data ) ) {
    					$ourdata[] = $data;
    				}
    			}
    		}
    	}
    
    	$attributes['acf_fields'] = $ourdata;
    
    	return $attributes;
    }
    
    add_filter( 'algolia_post_shared_attributes', 'my_post_attributes_with_sub_field', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'my_post_attributes_with_sub_field', 10, 2 );
    
    
    /**
     * @param array $settings
     *
     * @return array
     */
    
    function my_posts_index_settings( array $settings ) {
    
    	$settings['searchableAttributes'][] = 'unordered(acf_fields)';
    
    	return $settings;
    }
    
    add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );
    
    Thread Starter tonyp83

    (@tonyp83)

    I’ve noticed a specific field where a repeater has a repeater field inside of it seems to break the indexing process, so I’m trying to amend that.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Worth a shot, but perhaps without even worrying about getting things pushed to the index just yet, make sure that you’re seeing all of your expected data per post being attempted to index. Check the $attributes['acf_fields'] for everything if you can. Not sure if you’ve ever utilized something like XDEBUG but even just manually logging the variable to your error log could help verify things.

    Something like `error_log( implode(',',$attributes['acf_fields']); would drop in a comma separated message.

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

The topic ‘Indexing ACF Sub fields’ is closed to new replies.