• Resolved nguyenthixuan

    (@nguyenthixuan)


    Hi guys,

    Currently, I can add ACF fields to searchable for custom post type. However, I’m also using ACF fields for custom page templates ( post type = page ), most of page content is in ACF fields. I want to these ACF fields value to searchable for page, but it doesn’t work. My code as below. I’m not sure this correct way to do. Can you help me!

    add_filter( 'algolia_post_shared_attributes', 'cwy_algolia_page_attributes', 10, 2 );
    add_filter( 'algolia_searchable_post_shared_attributes', 'cwy_algolia_page_attributes', 10, 2 );
    
    function cwy_algolia_page_attributes( array $attributes, WP_Post $post ) {
    	$searchable_post_types = ['page'];
        if ( !in_array($post->post_type,$searchable_post_types) ) {
            return $attributes;
        }
    
        //Add acf fields to searchable for post type = page
        $attributes['aboutus_title'] = strip_tags(get_field( 'aboutus_title', $post->ID ));
        $attributes['aboutus_desc'] = strip_tags(get_field( 'aboutus_desc', $post->ID ));
        $attributes['aboutus_sec1_title'] = strip_tags(get_field( 'aboutus_sec1_title', $post->ID ));
        $attributes['aboutus_sec1_desc'] = strip_tags(get_field( 'aboutus_sec1_desc', $post->ID ));
    }
    
    add_filter( 'algolia_posts_page_index_settings', 'cwy_page_index_settings' );
    function cwy_page_index_settings( array $settings ) {
    	$settings['attributesToIndex'][] = 'unordered(aboutus_title)';
        $settings['attributesToSnippet'][] = 'aboutus_title:50';
    
        $settings['attributesToIndex'][] = 'unordered(aboutus_desc)';
        $settings['attributesToSnippet'][] = 'aboutus_desc:50';
    
        $settings['attributesToIndex'][] = 'unordered(aboutus_sec1_title)';
        $settings['attributesToSnippet'][] = 'aboutus_sec1_title:50';
    
        $settings['attributesToIndex'][] = 'unordered(aboutus_sec1_desc)';
        $settings['attributesToSnippet'][] = 'aboutus_sec1_desc:50';
        // Always return the value we are filtering.
        return $settings;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Quick thoughts/notes

    I believe add_filter( 'algolia_post_shared_attributes', 'cwy_algolia_page_attributes', 10, 2 ); is going to be indexing for Autocomplete based indexes. So only the add_filter( 'algolia_searchable_post_shared_attributes', 'cwy_algolia_page_attributes', 10, 2 ); line would be needed for that part.

    Also simply saving and pushing this specific snippet of code to the site’s code isn’t going to automatically get everything indexed, you’ll need to do a bulk re-index as well, or at least start editing/saving the pages so that a re-index occurs individually. If you’re not upgraded to 2.7.0, you’ll probably want to either perform the upgrade or do the bulk re-indexing, as you’re dealing with largely metadata.

    Assuming you’re not wanting to use the Autocomplete here, for your settings you’ll want to use the algolia_searchable_posts_index_settings filter hook, and then you’ll also want to click the “push settings” button at the top of that page.

    BEFORE YOU DO THAT though, if you’ve been doing any index configuration from directly in your Algolia.com dashboard, and not through code/the plugin, clicking the “push settings” button WILL overwrite those changed settings. It tends to be best to be either all in-dashboard or all in-code/push settings button. As is at this time, it’s overwrite, not merge.

    So, to sum up, need a touch of filter changes, and then also potential settings pushes and bulk re-indexing before everything will get to in the index.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Any changes or new developments here @nguyenthixuan?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add ACF field to searchable for page content’ is closed to new replies.