【问题标题】:Algolia - WordPress - Exclude multiple taxonomies from indexingAlgolia - WordPress - 从索引中排除多个分类
【发布时间】:2017-09-12 19:15:41
【问题描述】:

1) 我试图将某些分类法排除在 Algolia 的索引之外。我无法完成这项工作。这是我的代码:

function exclude_taxonomy( $should_index_category, WP_Post $post )
{
// Add all post types you don't want to make searchable.
$excluded_event_categories = array(
'post_type' => 'events',
'tax_query' => array(
    array(
        'taxonomy' => 'events_category',
        'field'    => 'slug',
        'terms'    => 'xclude',
    )
),

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

return ! in_array( $post->taxonomy, $excluded_event_categories, true );
}

 // Hook into Algolia to manipulate the post that should be indexed.
 add_filter( 'algolia_should_index_searchable_post', 'exclude_taxonomy', 10, 2 );

2) 我将如何使用多种分类法来做到这一点。所以我有第二个:

'tax_query' => array(
    array(
        'taxonomy' => 'events_status',
        'field'    => 'slug',
        'terms'    => 'archive',
    )
),

谢谢。

【问题讨论】:

    标签: wordpress algolia


    【解决方案1】:

    我认为问题在于$excluded_event_categories 变量。

    我相信你的代码应该类似于下面的 sn-p。如果我理解正确,您希望将 events_categoryevent_category 排除在索引之外。

    function exclude_taxonomy( $should_index_category, WP_Post $post )
    {
        if ( false === $should_index_category ) {
            return false;
        }
    
        return ! in_array( $post->taxonomy, array('events_category', 'events_status'), true );
    }
    
    // Hook into Algolia to manipulate the post that should be indexed.
    add_filter( 'algolia_should_index_searchable_post', 'exclude_taxonomy', 10, 2 );
    

    【讨论】:

    • Julien Bourdeau - 我需要从多个术语中排除多个分类法。因此,我需要从“events_status”分类中删除“归档”术语,并且需要从“events_category”分类中删除“xclude”术语。
    • 不应该是这样的: function custom_should_index_terms( $flag, WP_Post $post ) { $excluded_ids = array( 3795, 1187 ); if ( in_array( $post->term_id, $excluded_ids ) ) { return false; } 返回 $flag; } add_filter('algolia_should_index_post', 'custom_should_index_terms', 10, 2); add_filter('algolia_should_index_searchable_post', 'custom_should_index_terms', 10, 2);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2015-11-25
    相关资源
    最近更新 更多