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

Having trouble getting Algolia Search to recognize my custom fields

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

我创建了一个自定义 post 类型,它将包含每个调查提交的唯一 post。每个调查输入字段都被推送到 post 内的自定义字段。

在下面的示例中,我尝试实现 Algolia 的自定义字段推送和检索功能:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia。我的自定义 post 类型名为 'Submissions',我正在尝试在该 post 类型中推送一个带有 '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;
}

我解决了!问题在于此代码仅适用于高级自定义字段插件 (ACF)。 my_post_attributes() 函数正在调用 get_field() 方法,该方法在我的 Wordpress 安装中不存在,因为我的自定义字段不是由 ACF 插件创建的。有一次,我安装了插件,代码成功将自定义字段推送到Algolia。

// 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 );

此处的示例:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia 解决了与高级自定义字段插件的集成问题。

也就是说,您也可以将 get_field 调用替换为 WordPress 原生的 '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;
}

这里我们从 post.

中得到 2 个元字段 latitude & longitude