• Resolved exifi

    (@exifi)


    Hi guys, I can’t understand how to make the snippet like this data._snippetResult['content'].value work but with ACFs

    I only found {{data.acf_field}} around but it shows the content including html tags

    do you have any solution about this?

    Thanks in advance for the support

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

    (@tw2113)

    The BenchPresser

    Hi @exifi

    I would check out our ACF-specific wiki page over at https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Advanced-Custom-Fields for some examples of working with ACF based fields including a spot that shows how to add them as a snippet-based value.

    The exact part of that page where the snippeting comes into play is https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Advanced-Custom-Fields#make-custom-fields-searchable

    Hope that helps.

    Thread Starter exifi

    (@exifi)

    Hi Michael, thanks for the reply.
    To index the ACFs I did this and I see them correctly in Algolia.

    function wds_algolia_custom_fields( array $attributes, WP_Post $post ) {
    $fields = [
    'payoff',
    'descrizione_lunga',
    'descrizione',
    ];

    foreach ( $fields as $field ) {
    $data = get_post_meta( $post->ID, $field );
    $data = get_field( $field, $post->ID );
    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 );

    But now I have to show the content on the search page.

    I would like to show an ACF for the description of a CPT. But if I show it with {{data.acf_field}} all the HTML comes out so I would like to try to show it as if it were the text snippet like data._snippetResult['content'].value

    Thanks

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Definitely understood, but you need to prepare your data a bit more to get that, which can be seen how with the link provided.

    For example:

    function my_posts_index_settings( array $settings ) {

    $fields = [
    'payoff',
    'descrizione_lunga',
    'descrizione',
    ];

    foreach ( $fields as $field ) {
    // If you want the fields searchable too, which makes sense.
    $settings['searchableAttributes'][] = 'unordered(' . $field . ')';

    // If you want the fields above to be added to the attributes to snippet.
    // The "50" is the string character limit to cut things off at.
    // @see https://www.algolia.com/doc/api-reference/api-parameters/#highlighting-snippeting
    $settings['attributesToSnippet'][] = $field . ':50';
    }

    return $settings;
    }
    add_filter( 'algolia_posts_speaker_index_settings', 'my_posts_index_settings' );

    This snippet will get all 3 ACF fields added to the searchable attributes, as well as add them into the snippet-ed portion.

    After that, you’re going to need to modify the templates used (Documentation: https://github.com/WebDevStudios/wp-search-with-algolia/wiki/Customize-Templates ) and start making use of the snippet based property. For example: `data._snippetResult['descrizione'].value

    If you need help verifying that the snippet version made it into index results, let me know where I can see the search in action, and I can check the Algolia results coming back in and all the available data to you.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Brief followup from my comment yesterday, the 50 is actually WORD count, not CHARACTER count. My apologies there.

    Extra apologies due to copy/paste, I realized that the action hook used in my example is for a specific post type from our documentation.

    You’d want to use 'algolia_posts_index_settings' for general/overall settings, or change out the speaker portion of the hook to a post type specific to your user case.

    Thread Starter exifi

    (@exifi)

    Thanks for the support ??

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Snippet result for ACF fields’ is closed to new replies.