admin管理员组文章数量:1130349
Is it possible to have multiple custom post types appear on a category page?
For example, I have a category Animals and I can assign it to both these custom post types: dog, cat
Both cpts use a pre_get_posts filter:
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
But only one cpt will appear on the Animals category page.
If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.
I have tried changing the priority settings on the add_filter calls.
And none of this affects the display of normal posts in the category from appearing on the category page.
I'd like to have any post, of any type, appear on the Animals page.
What am I missing?
Is it possible to have multiple custom post types appear on a category page?
For example, I have a category Animals and I can assign it to both these custom post types: dog, cat
Both cpts use a pre_get_posts filter:
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
But only one cpt will appear on the Animals category page.
If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.
I have tried changing the priority settings on the add_filter calls.
And none of this affects the display of normal posts in the category from appearing on the category page.
I'd like to have any post, of any type, appear on the Animals page.
What am I missing?
Share Improve this question asked Dec 5, 2018 at 16:53 shanebpshanebp 5,0857 gold badges28 silver badges40 bronze badges 2- Whatever filter runs last is going to overwrite whatever ran before it, as you're not adding to post types already set by previous filters, you are replacing it entirely with a different value. – Milo Commented Dec 5, 2018 at 17:07
- So obvious I couldn't see it. Thx. – shanebp Commented Dec 5, 2018 at 18:34
1 Answer
Reset to default 2Add to $post_type array, don't replace it...
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type[] = 'dog';
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
Is it possible to have multiple custom post types appear on a category page?
For example, I have a category Animals and I can assign it to both these custom post types: dog, cat
Both cpts use a pre_get_posts filter:
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
But only one cpt will appear on the Animals category page.
If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.
I have tried changing the priority settings on the add_filter calls.
And none of this affects the display of normal posts in the category from appearing on the category page.
I'd like to have any post, of any type, appear on the Animals page.
What am I missing?
Is it possible to have multiple custom post types appear on a category page?
For example, I have a category Animals and I can assign it to both these custom post types: dog, cat
Both cpts use a pre_get_posts filter:
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
But only one cpt will appear on the Animals category page.
If I deactivate the plugin for the dogs cpt, then any cat cpts assigned to Animals will appear on the category page.
I have tried changing the priority settings on the add_filter calls.
And none of this affects the display of normal posts in the category from appearing on the category page.
I'd like to have any post, of any type, appear on the Animals page.
What am I missing?
Share Improve this question asked Dec 5, 2018 at 16:53 shanebpshanebp 5,0857 gold badges28 silver badges40 bronze badges 2- Whatever filter runs last is going to overwrite whatever ran before it, as you're not adding to post types already set by previous filters, you are replacing it entirely with a different value. – Milo Commented Dec 5, 2018 at 17:07
- So obvious I couldn't see it. Thx. – shanebp Commented Dec 5, 2018 at 18:34
1 Answer
Reset to default 2Add to $post_type array, don't replace it...
function dog_query_post_type($query) {
if( is_category() && $query->is_main_query() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type[] = 'dog';
else
$post_type = array( 'post', 'dog', 'nav_menu_item');
$query->set('post_type',$post_type);
return $query;
}
}
add_filter('pre_get_posts', 'dog_query_post_type');
本文标签: categoriesmultiple custom post type on category page
版权声明:本文标题:categories - multiple custom post type on category page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749125908a2319775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论