Algolia - WordPress - 即时搜索页面如何排除具有特定 ID 的 post

Algolia - WordPress - instantsearch page how do I exclude post with certain ID

在 Algolia 即时搜索页面(WordPress 插件)我如何排除具有特定 ID 的 post?

这是默认的即时搜索设置。如何添加过滤器以从搜索中排除 post ID?

            var search = instantsearch({
                appId: algolia.application_id,
                apiKey: algolia.search_api_key,
                indexName: algolia.indices.searchable_posts.name,
                urlSync: {
                    mapping: {'q': 's'},
                    trackedParameters: ['query']
                },
                searchParameters: {
                    facetingAfterDistinct: true,
        highlightPreTag: '__ais-highlight__',
        highlightPostTag: '__/ais-highlight__'
                }
            });

转到插件文件夹 > 包含 > class-algolia-search.php

并找到这段代码

$query->set( 'post__in', $post_ids );

恰好在该代码之后添加此代码

$query->set( 'post__not_in', array(1,2,3));

然后告诉我结果。 在我的代码中,1、2、3 是要排除的 post 个 ID。 谢谢

不将帖子作为结果的一部分显示的唯一好的解决方案是不将它们编入索引。

此处详细说明了使用 WordPress 的 Algolia 插件编制索引:https://community.algolia.com/wordpress/indexing-flow.html#indexing-decision

这是一个可以让您入门的代码片段:

<?php
// to put in the functions.php file of your active theme.
/**
 * @param bool    $should_index
 * @param WP_Post $post
 *
 * @return bool
 */
function exclude_post_ids( $should_index, WP_Post $post )
{
    // Add all post IDs you don't want to make searchable.
    $excluded_ids = array( 14, 66 );
    if ( false === $should_index ) {
        return false;
    }

    return ! in_array( $post->ID, $excluded_ids, true );
}

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