最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Category param redirect to post type archive

programmeradmin0浏览0评论

I'm trying to filter categories by post type.

e.g: the following URI will retrieve all post from the category news.

/?post_type=post.

Here is how I'm updating the main query:

function gpc_filter_categories( WP_Query $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return $query;
    }

    if ( isset( $_GET['post_type'] ) ) {
        $get = esc_sql( $_GET['post_type'] );
        $query->set( 'post_type', $get );
    }

    return $query;
}

add_action( 'pre_get_posts', 'gpc_filter_categories' );

This actually work great.

But here is the problem: it doesn't work with custom post type.
Let's say I've a post type named movie.

When accessing the following URI, the archive.php template is used instead of category.php.

/?post_type=movie.

发布评论

评论列表(0)

  1. 暂无评论