Adding Categories across posts, and custom post types

I currently have a list of categories that go with my ‘posts’
I want the same categories to go with my custom post types ‘services’ and ‘work’

The following code, adds the tags and categories to the work and services custom posts.

add_action('init', 'demo_add_default_boxes');

function demo_add_default_boxes() {
    register_taxonomy_for_object_type('category', 'work');
    register_taxonomy_for_object_type('post_tag', 'work');
    register_taxonomy_for_object_type('category', 'services');
    register_taxonomy_for_object_type('post_tag', 'services');
}

But when I categorise a work/service post, it does not appear on the /category/ page, only the ‘posts’ from that category appear.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

By default, only post_type post is shown on category pages. To enable your custom post types, you could hook pre_get_posts, check if it’s a category page, and if so add your post types:

function wpse_category_set_post_types( $query ){
    if( $query->is_category() && $query->is_main_query() ){
        $query->set( 'post_type', array( 'post', 'work', 'services' ) );
    }
}
add_action( 'pre_get_posts', 'wpse_category_set_post_types' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x