StudioPress Community Forums
  StudioPress Community Forums > Forums > General Discussion
For help and support, access to your downloads, or to manage your account please log into My StudioPress.

These forums have been set to read-only so you can browse the existing topics for any questions you may have.

For general discussion on WordPress, CSS and design (NOT for support) visit the new Community Forums.
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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.
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Should I use 'posts and categories' or 'custom post types'? joshstauffer General Discussion 7 10-04-2010 09:11 PM


All times are GMT -5. The time now is 08:31 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.