【问题标题】:Algolia - WordPress - instantsearch page how do I exclude post with certain IDAlgolia - WordPress - 即时搜索页面如何排除具有特定 ID 的帖子
【发布时间】:2017-08-08 00:47:52
【问题描述】:

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

这是默认的即时搜索设置。如何添加过滤器以从搜索中排除帖子 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__'
                }
            });

【问题讨论】:

    标签: wordpress algolia


    【解决方案1】:

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

    这里详细解释了使用 Algolia 插件为 WordPress 建立索引:https://community.algolia.com/wordpress/indexing-flow.html#indexing-decision

    这里有一段代码 sn-p 可以帮助您入门:

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

    【讨论】:

      【解决方案2】:

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

      并找到此代码

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

      在该代码之后添加该代码

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-29
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        • 2014-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多