无法让Algolia Search识别我的自定义字段

Jim*_*ski 1 php wordpress plugins custom-fields algolia

我最近将Algolia Search Wordpress插件实现为我的客户端的新WP版本.该网站将主要用于收集调查数据,并为其管理员提供强大的搜索功能.

我创建了一个自定义帖子类型,其中包含每个调查提交的唯一帖子.每个调查输入字段都被推送到帖子中的自定义字段.

在下面的示例中,我正在尝试实现Algolia的自定义字段推送和检索功能:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia .我的自定义帖子类型标题为"提交",我正在尝试在该帖子类型中使用"organisation_name"键来推送自定义字段.同样,我认为我已正确设置所有内容,但此自定义字段未被Algolia索引.任何帮助将不胜感激.

// Push custom fields to Algolia
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   $attributes
 * @param WP_Post $post
 *
 * @return array
 */
function my_post_attributes( array $attributes, WP_Post $post ) {

    if ( 'submissions' !== $post->post_type ) {
        // We only want to add an attribute for the 'submissions' post type.
        // Here the post isn't a 'submission', 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['organisation_name'] = get_field( 'organisation_name', $post->ID );

    // Always return the value we are filtering.
    return $attributes;
}


// Make custom fields searchable
add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );
// We could also have used a more general hook 'algolia_posts_index_settings',
// but in that case we would have needed to add a check on the post type
// to ensure we only adjust settings for the 'speaker' post_type.

/**
 * @param array $settings
 *
 * @return array
 */
function my_posts_index_settings( array $settings ) {

    if ( 'submissions' !== $post->post_type ) {
        return $settings;
    }

    // Make Algolia search into the 'bio' field when searching for results.
    // Using ordered instead of unordered would make words matching in the beginning of the attribute
    // make the record score higher.
    // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestoindex
    $settings['attributesToIndex'][] = 'unordered(organisation_name)';

    // Make Algolia return a pre-computed snippet of 50 chars as part of the result set.
    // @see https://www.algolia.com/doc/api-client/ruby/parameters#attributestohighlight
    $settings['attributesToSnippet'][] = 'organisation_name:50';

    // Always return the value we are filtering.
    return $settings;
}
Run Code Online (Sandbox Code Playgroud)

ray*_*jes 5

这里的示例:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia解决了与Advanced Custom Field插件的集成问题.

话虽这么说,你也可以使用get_fieldWordPress原生的'get_post_meta' 替换这些调用.

这是一个例子:

<?php

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   $attributes
 * @param WP_Post $post
 *
 * @return array
 */
function my_post_attributes( array $attributes, WP_Post $post ) {



$attributes['_geoloc']['lat'] = (float) get_post_meta( $post->ID, 'latitude', true );

$attributes['_geoloc']['lng'] = (float) get_post_meta( $post->ID, 'longitude', true );


// Always return the value we are filtering.

return $attributes;
}
Run Code Online (Sandbox Code Playgroud)

在这里,我们得到2个元字段latitudelongitude帖子.