【问题标题】:stop wordpress search showing a custom post type停止显示自定义帖子类型的 wordpress 搜索
【发布时间】:2017-02-11 16:53:31
【问题描述】:

我有一种自定义帖子类型,可用于使用 uncode 主题构建的页面上的某些文本块。我需要公开这些块,以便它们显示在页面上,但我想阻止它们出现在搜索结果中。

search.php 不像普通的 wordpress 搜索文件,它是 uncode-theme 文件并且没有正常的查询,我不认为所以我想我可能需要一个函数?

谁能告诉我如何做到这一点?

CPT 是“静态内容”

谢谢!

【问题讨论】:

    标签: wordpress search


    【解决方案1】:

    这里的答案取决于您是通过自己的代码创建 CPT,还是其他插件正在创建 CPT。有关这两种方法的详细说明,请参阅此链接:

    http://www.webtipblog.com/exclude-custom-post-type-search-wordpress/

    基本要点是这样的:

    如果您要创建自己的 CPT,您可以在 'exclude_from_search' => true 的 register_post_type() 调用中添加一个参数

    如果另一个插件/主题正在创建 CPT,您需要稍后设置此 exclude_from_search 变量,作为 CPT 过滤器的一部分,如下所示:

    // functions.php
    
    add_action( 'init', 'update_my_custom_type', 99 );
    
    function update_my_custom_type() {
        global $wp_post_types;
    
        if ( post_type_exists( 'staticcontent' ) ) {
    
            // exclude from search results
            $wp_post_types['staticcontent']->exclude_from_search = true;
        }
    }
    

    【讨论】:

    • 我的话,非常简单,谢谢!我自己注册了 CPT,所以只添加了 'exclude_from_search' => true
    • 所以我只是注意到添加 exclude_from_search 行实际上会停止在页面上显示的内容块。它消失了。我希望 CPT 出现在页面上,但不在搜索中。这可能吗?
    • 嗯..这很奇怪,它当然不应该那样做。您是否碰巧忘记了参数数组中 'exclude_from_search' => true 行末尾的逗号?如果您从 CPT 参数中完全删除该行,内容会返回吗?
    • 嗨,格雷格,不,我没有省略逗号,只是仔细检查了一下。当我删除排除行时,内容确实会返回。
    • 警告!这个公认的答案有副作用:它还会在包含从搜索中排除的帖子类型的分类法中产生不可用的分页。这里有更多细节stackoverflow.com/questions/8269713/…
    【解决方案2】:

    我认为接受的答案是正确的。 exclude_from_search 阻止所有 $query = new WP_Query 返回结果。

    核心说:

    ...检索任何类型 except 修订和类型 'exclude_from_search' 设置为 TRUE)

    这是一个常见的问题,并且与 前端搜索结果页面 混淆。在数据库中搜索帖子。

    在前端使用自定义查询呈现内容,需要exclude_from_search = false 或者使用其他方式,直接通过id获取内容。

    您需要改为过滤搜索前端机制。这是 true 从搜索中排除帖子类型,无需手动重新构建“已知”类型:

    function entex_fn_remove_post_type_from_search_results($query){
    
        /* check is front end main loop content */
        if(is_admin() || !$query->is_main_query()) return;
    
        /* check is search result query */
        if($query->is_search()){
    
            $post_type_to_remove = 'staticcontent';
            /* get all searchable post types */
            $searchable_post_types = get_post_types(array('exclude_from_search' => false));
    
            /* make sure you got the proper results, and that your post type is in the results */
            if(is_array($searchable_post_types) && in_array($post_type_to_remove, $searchable_post_types)){
                /* remove the post type from the array */
                unset( $searchable_post_types[ $post_type_to_remove ] );
                /* set the query to the remaining searchable post types */
                $query->set('post_type', $searchable_post_types);
            }
        }
    }
    add_action('pre_get_posts', 'entex_fn_remove_post_type_from_search_results');
    

    并且备注$post_type_to_remove = 'staticcontent'; 可以更改以适应任何其他帖子类型。

    请发表评论如果我在这里遗漏了什么,我无法找到另一种方法来防止这样的帖子类型场景,通过查询显示内容隐藏搜索/直接访问前端用户。

    【讨论】:

    • 我同意你的意见。
    • 根据此法典页面 exclude_from_search 用于前端搜索结果。但它有一些注释。codex.wordpress.org/Function_Reference/register_post_typeexclude_from_search (boolean) (重要性) 是否从前端搜索结果中排除该帖子类型的帖子。
    • 我觉得应该为未来的谷歌人(比如我自己)明确说明这一点:这种技术可能是您想要的,而不是公认的答案。在 CPT 配置中使用 'exclude_from_search' => true(按照接受的答案的建议)还将从分类档案中删除帖子,这可能不是您想要的。我自己刚刚经历过这个(WP 5.3.2),你可以看到 OP 在接受的答案的 cmets 中也经历过。如果您只想影响搜索结果页面,请使用此答案中的方法。
    • 我尝试了接受的答案,但它也从存档页面中删除了帖子。所以,我认为接受的答案是不正确的。我想知道为什么它有这么多的赞成票。这个答案对我有用。只需复制 functions.php 中的代码,然后将“staticcontent”一词替换为您想要的帖子类型。
    • 这是最好的和有效的解决方案,因为接受的答案有副作用:它还会在包含从搜索中排除的帖子类型的分类法中产生不可用的分页。这里有更多细节stackoverflow.com/questions/8269713/…
    【解决方案3】:

    首先,Jonas Lundman 的答案是正确的,应该是公认的答案。

    exclude_from_search 参数工作不正确 - 它也将帖子类型排除在其他查询之外。

    WP 问题跟踪系统上有一张票,但他们已将其作为 wontfix 关闭,因为他们无法在不破坏向后兼容性的情况下修复此问题。有关详细信息,请参阅 this ticketthis one

    我在 Jonas Lundman 提出的解决方案中添加了额外的检查,因为:

    • 在实际设置中,可能有其他插件试图修改搜索查询,因此简单地替换 post_type 可能会导致意外结果。
    • 我认为使用一组帖子类型来排除更灵活。
    add_action('pre_get_posts', 'remove_my_cpt_from_search_results');
    
    function remove_my_cpt_from_search_results($query) {
        if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
            return $query;
        }
    
        // can exclude multiple post types, for ex. array('staticcontent', 'cpt2', 'cpt3')
        $post_types_to_exclude = array('staticcontent');
    
        if ($query->get('post_type')) {
            $query_post_types = $query->get('post_type');
    
            if (is_string($query_post_types)) {
                $query_post_types = explode(',', $query_post_types);
            }
        } else {
            $query_post_types = get_post_types(array('exclude_from_search' => false));
        }
    
        if (sizeof(array_intersect($query_post_types, $post_types_to_exclude))) {
            $query->set('post_type', array_diff($query_post_types, $post_types_to_exclude));
        }
    
        return $query;
    }
    

    【讨论】:

    • 这是唯一对我有用的答案。
    猜你喜欢
    • 2018-10-29
    • 2013-12-22
    • 2016-08-15
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多