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.