【问题标题】:Algolia - WordPress exclude single term from indexingAlgolia - WordPress 从索引中排除单个术语
【发布时间】:2017-09-13 19:21:31
【问题描述】:

我试图从 CPT“事件”的“events_status”分类中排除 ID 为 3795 的单个术语“存档”。尝试重新索引 WordPress 后端中的“事件”cpt 时,出现空错误弹出窗口。这是我的代码:

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

【问题讨论】:

    标签: wordpress algolia


    【解决方案1】:

    这是正确的做法。 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多