View Single Post
  #1  
Old 05-29-2011, 08:14 PM
Bill Murray Bill Murray is offline
Registered User
Pro Plus Member
 
Join Date: Aug 2010
Posts: 645
Default Using categories with custom post types

I have a custom post type named 'community', and I've registered it with taxonomies to include 'category'. I've also added:

PHP Code:
register_taxonomy_for_object_type('category''community'); 
immediately following the register_post_type() command. However, a link to the category archive to which the CPT's are assigned doesn't show the CPT's. Since I only have CPT's assigned to that category, I get the 'no posts match...' response to the query.

If I add a filter to the query with this code ...

PHP Code:
if ( !is_admin() ) {
    
add_filter('pre_get_posts''query_post_type');
    function 
query_post_type($query) {
        if(
is_category() || is_tag() || is_home() && 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','community','nav_menu_item');
        
$query->set('post_type',$post_type);
        return 
$query;
        }
    }

the CPT's show up in the category archive just fine.

Does anyone have any idea why the register_taxonomy_for_object_type() doesn't accomplish that, forcing me to add the filter? Until I got the function right, it was breaking nav menus, and the admin check was something that became necessary in WP 3.1. If I could avoid adding the filter, I'd really like to do that.