Algolia - WordPress将单个术语排除在索引之外

时间:2017-09-13 11:01:09

标签: wordpress algolia

我试图从CPT“事件”的“events_status”分类中排除ID为3795的单个术语“存档”。当我试图在WordPress后端重新索引“事件”cpt时,我得到了一个空错误的poppup。这是我的代码:

// EXCLUDE TERM FROM BEING INDEXED
function custom_should_index_term( $should_index, WP_Post $post ) {

    $terms_to_exclude = array( 3795 );

    if ( false === $should_index ) {
        return $should_index;
    }


    if ( $post->post_type !== 'events' ) {
        return  $should_index;
    }


    $post_term_ids = wp_get_post_terms( $post->ID, 'events_status' );
    $remaining_term_ids = array_diff( $post_term_ids, $terms_to_exclude 
   );
    if ( count( $remaining_term_ids ) === 0 ) {
        return false;
    }

    return $should_index;
     }
    add_filter('algolia_should_index_post', 'custom_should_index_term', 
    10, 2);
   add_filter('algolia_should_index_searchable_post', 
   'custom_should_index_term', 10, 2);

1 个答案:

答案 0 :(得分:0)

这是怎么做的。 wp_get_post_terms()还需要包含我们想要获取的字段的数组。由于我们正在比较ID,我们要求它为我们提供ID。

// EXCLUDE TERM FROM BEING INDEXED
function custom_should_index_term( $should_index, WP_Post $post ) {

    $terms_to_exclude = array( 3795 );

    if ( false === $should_index ) {
        return $should_index;
    }


    if ( $post->post_type !== 'events' ) {
        return  $should_index;
    }


    $post_term_ids = wp_get_post_terms( $post->ID, 'events_status', array("fields" => "ids") );
    $remaining_term_ids = array_diff( $post_term_ids, $terms_to_exclude );
    if ( count( $remaining_term_ids ) === 0 ) {
        return false;
    }

    return $should_index;
}
add_filter('algolia_should_index_post', 'custom_should_index_term', 10, 2);
add_filter('algolia_should_index_searchable_post', 'custom_should_index_term', 10, 2);