代码之家  ›  专栏  ›  技术社区  ›  user3652337

Algolia-WordPress-instantsearch页面如何排除具有特定ID的帖子

  •  0
  • user3652337  · 技术社区  · 9 年前

                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__'
                    }
                });
    
    2 回复  |  直到 9 年前
        1
  •  4
  •   rayrutjes    9 年前

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

    本文详细介绍了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 );
    
        2
  •  -3
  •   Souvik Sikdar    9 年前

    class-algolia-search.php

    然后找到这个代码

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

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

    在我的代码1,2,3中,是您想要排除的post ID。 谢谢