我最近将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;
}
答案 0 :(得分:5)
此处的示例:https://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algolia解决了与高级自定义字段插件的集成。
话虽如此,您也可以使用WordPress原生的'get_post_meta'替换get_field
次来电。
以下是一个例子:
<?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;
}
这里我们得到2个元字段latitude
&amp; longitude
离开帖子。
答案 1 :(得分:2)
我解决了!问题是此代码仅用于高级自定义字段插件(ACF)。 my_post_attributes()函数正在调用我的Wordpress安装中不存在的get_field()方法,因为我的自定义字段不是由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 );