Yes, this is pretty normal with the default category/tags archives. We wrote up this quick tutorial on how to handle, if you’re willing to try out some code solutions
https://docs.pluginize.com/article/17-post-types-in-category-tag-archives
We also provided some UI for this for a much less code based solution in CPTUI-Extended, our premium addon, but not sure if you want to go that route. I’ll leave that one up to you.
Welcome. Give it a go and let me know if you experience any issues I can help with. I’ll leave the support thread open until we get confirmation of success.
Thread Starter
Kyrian
(@oloccina)
I have just tested the below code in my functions.php file
but what happens is that now the category page lists ONLY the custom post type and not the WordPress default posts.
see: https://aikyam.net/c/contenuti/spirito/
Am i missing something here?
Thanks
// CPT UI - add custom post type to default wordpress taxonomies
function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( 'vid' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
Not from what I can see, you copy/pasted over pretty accurately and updated the correct spots.
Just in case, and it may feel a little odd, but do we for sure have some default posts that have the “spirito” category, as opposed to perhaps just a “contenuti” category?
Thread Starter
Kyrian
(@oloccina)
Yep
default posts
https://nimb.ws/1D8nCH
custom type posts
https://nimb.ws/5i0G8h
but I just tested with twenty twenty theme and it works, so is my theme issue (I use Uncode theme), I’ll have to check with them…
Thanks again!
Keep me updated, and hopefully they have some good leads on their end.
Thread Starter
Kyrian
(@oloccina)
Hi Michael,
I just wanted to update you on this.
Unfortunately Uncode Theme does not support the possibility to display more than 1 post type in the same taxonomy/category.
Pity! But I guess I’ll have to go a different way.
Thanks again for your support
Thread Starter
Kyrian
(@oloccina)
Hey Michael,
sorry to bother you again but today I was testing the possibility to show in the author archive page a CPT UI custom post type along with regular wordpress posts.
I thought it was not possible, since doing this in category pages with my Uncode theme seemed to not be possible.
But after testing this piece of code
// CPT UI - add custom post type to author archive page
function post_types_author_archives($query) {
if ($query->is_author)
$query->set( 'post_type', array('vid','post') );
remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'post_types_author_archives');
it just worked flawlessly (see https://aikyam.net/author/nican/)
So I am thinking if the code I was using to do the same thing in category pages was maybe not right. I am not a developer and I am just speculating here…
but this line
$cptui_post_types = array( 'vid','post' );
seems to only refer to custom post types? I want to show 1 custom post type (vid) along with regular wordpress posts.
Maybe it needs a little tweaking?
Below is the whole code I am using.
Thanks for any input on this!
// CPT UI - add custom post type to default wordpress taxonomies
function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( 'vid','post' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
Thread Starter
Kyrian
(@oloccina)
Alright,
I found another code snippet here https://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/ and this works to show custom post types along with regular wordpress posts in my Uncode theme.
I post it here in case someone needs it
function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = get_post_types();
$query->set( 'post_type', $post_types );
return $query;
}
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );
For what it’s worth, that last version just adds ALL of the post types, regardless of what/who is registering them.
Only thing standing out for me with the variation I’ve provided is if possibly the array merge is failing. Plus it’s specifying “post” twice but I don’t that should affect things too much.
Thread Starter
Kyrian
(@oloccina)
Yep,
I have slightly changed the code to specify only some post types (but I’ve posted the original code for other to use.
Here is the version I am using
// Add custom post type to default wordpress taxonomies
function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$post_types = array( 'post', 'vid', 'meditazione' );
$query->set( 'post_type', $post_types );
return $query;
}
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );
I have no idea why this one works either, but it does.
that’s the important part, it’s working.