简体   繁体   English

我可以从搜索和存档中排除自定义帖子类型的特定类别吗?

[英]Can I exclude a specific category of a custom post type from both search and archives?

I have a custom post type "courses" and the taxonomy for categories within this post type is "course-category."我有一个自定义帖子类型“课程”,并且此帖子类型中的类别分类是“课程类别”。

I have a course category called "Academy" and I would like to exclude that category from any search, and from any archive pages for that custom post type.我有一个名为“学院”的课程类别,我想从任何搜索以及该自定义帖子类型的任何存档页面中排除该类别。

Right now I have this code snippet but it doesn't seem to be working.现在我有这个代码片段,但它似乎没有工作。 It's for the search exclusion only but I need to exclude the category in both search pages and archive pages .它仅用于搜索排除,但我需要排除搜索页面和存档页面中的类别。

function wpb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set('post_type', array('courses');
        $query->set( 'course-category','-68' );
    return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

Have you tried using is_post_type_archive()?您是否尝试过使用 is_post_type_archive()?

function wpb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() || $query->is_main_query() && !is_admin() && $query->is_post_type_archive('courses') ) {
        $query->set('post_type', array('courses');
        $query->set( 'course-category','-68' );
        return $query;  
    }
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM