Custom post type categories ignored
-
In my plugin, I registered a Custom Post Type with its own archive (
has_archive = true
).The archive displays a list of posts in a sort of grid (by default, the theme provides this grid, used by search results too).
Any single item in the list displays the direct link to the related post AND its categories (any category features a clickable link).
What happens: when I click one of the categories created within the admin area of the custom post type, no post is found, even if there are many.
This is a typical link:
https://www.my.domain/category/mycategory/mysubcategory/This happens with this scheme too:
https://www.my.domain/category/mycategory/At the same time, categories created from inside the traditional Post admin area suffer no such issue.
Why are my categories ignored?
[UPDATE]
After some research, I found the only way to get (not entirely…) what I want is use this code in functions.php:
add_action('pre_get_posts', function($query) { if ( ! is_admin() && $query->is_main_query() ) { if ( is_archive() || is_category() ) { $query->set( 'post_type', 'myCustomPost' ); } } });
Is this safe or wrong?
I found this thing too: if I have a nested category, this thing breaks my navigation.
- The topic ‘Custom post type categories ignored’ is closed to new replies.