2016-10-13 23 views
1

我最近將Algolia Search Wordpress插件實施到我的客戶端的新WP版本中。該網站將主要用於收集調查數據併爲其管理員提供強大的搜索功能。難以讓Algolia Search識別我的自定義字段

我已創建一個自定義帖子類型,其中包含每個調查提交的唯一帖子。每個調查輸入字段都會推送到帖子內的自定義字段。

在下面的示例中,我試圖實施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

你能確認你使用ACF插件嗎?另外,你是否在引入過濾器後重新編制索引? – rayrutjes

回答

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 & longitude出門柱。

+0

非常感謝。這實際上解決了我近期可能遇到的另一個問題,它在$ attributes數組中包含多個自定義字段。 –

+0

不客氣,請注意,雖然在這個例子中我實際上只在數組中添加了一個條目,它是_geoloc。但是可以根據需要添加儘可能多的數據;),您不必強制使用嵌套的數組路徑。 – rayrutjes

+0

感謝您的澄清!你知道我可以怎樣給數組添加額外的屬性嗎? –

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); 
相關問題