0
votes

I have this loop that gets chosen categories for a custom post type of portfolio:

$wp_query = new WP_Query(array('category__in' => $portfolio_cat, 'post_type' => 'portfolio', 'showposts'=>$number_of_posts) );

It uses standard wordpress categories, but only shows portfolio type posts in those categories. The problem is, I want to list the category the portfolio post is in but when the category is clicked it leads to a 404 not found.

I am using this to display the category (successfully) under the portfolio post type:

<?php the_category('') ?>

Is there any way to have a custom post type use standard categories and not a custom taxonomy and be able to click the category to go to a page that lists all posts in the category?

1

1 Answers

2
votes

You have to use

'taxonomies' => array('category')

in your register_post_type() function to make it connected to the category. Also, you can use this

register_taxonomy_for_object_type('category','portfolio');

after you call the register_post_type() function.

If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
    {
        $query->set( 'post_type', array( 'post', 'page', 'portfolio' ) );
    }
    return $query;
}