2016-10-13 4 views
1

最近、Algolia Search Wordpressプラグインをクライアント用の新しいWPビルドに実装しました。サイトは基本的に調査データを収集し、管理者に堅牢な検索機能を提供するために使用されます。Algolia Searchに自分のカスタムフィールドを認識させるのに問題がある

私は、各調査依頼ごとに固有の投稿を含むカスタム投稿タイプを作成しました。各調査入力フィールドは、ポスト内のカスタムフィールドにプッシュされます。

次の例では、Algoliaのカスタムフィールドプッシュと実装を実装しようとしています。ここにはhttps://community.algolia.com/wordpress/advanced-custom-fields.html#push-custom-fields-to-algoliaがあります。私のカスタムポストタイプは「Submissions」と題されており、そのポストタイプ内に '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は、アドバンストカスタムフィールドプラグインとの統合に対処しています。

つまり、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; 
} 

我々はポストのうちの2つのメタフィールドlatitude & longitudeを取得します。

+0

ありがとうございました。これは実際には、近い将来に$ attributes配列に複数のカスタムフィールドを含む別の問題に取り組んでいます。 –

+0

この例では、実際に_geolocという配列に1つのエントリしか追加していませんが、歓迎します。しかし、必要な数だけ追加することができます)、ネストされた配列パスを使用する必要はありません。 – rayrutjes

+0

説明をありがとう!配列に追加の属性を追加する方法を知っていますか? –

2

私はそれを解決しました!問題は、このコードがAdvanced Custom Fieldsプラグイン(ACF)でのみ使用されることでした。 my_post_attributes()関数は、自分のカスタムフィールドがACFプラグインによって作成されていないため、Wordpressのインストールに存在しなかったget_field()メソッドを呼び出していました。一度、私はプラグインをインストールし、コードは正常に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); 
関連する問題