StudioPress Community Forums

StudioPress Community Forums (http://www.studiopress.com/support/index.php)
-   General Discussion (http://www.studiopress.com/support/forumdisplay.php?f=7)
-   -   Custom Post Type categories (http://www.studiopress.com/support/showthread.php?t=123416)

KHCreative 11-05-2012 09:05 PM

Custom Post Type categories
 
Hello all,

I know this is going to fall outside of general support, but I'm hoping the community can help me figure this out.

I have added custom post types to a site and I'm having trouble with the categories. I have a category with only posts from my custom post types selected with it. If you select categories in the Wordpress admin, you can see that the category has the right number of posts associated with it. If you click that number to reveal the post, the page shows no post in that category. The same is true if you go to the front end and look at the category archive page for that category.

Here is the code for the cpt.
PHP Code:

function schedule_post_type() {

    
register_post_type'schedule',
    
// options for this post type
        
array('labels' => array(
        
'name' => __('Schedules''post type general name'), /* This is the Title of the Group */
        
'singular_name' => __('Schedule''post type singular name'), /* This is the individual type */
        
'add_new' => __('Add New''custom post type item'), /* The add new menu item */
        
'add_new_item' => __('Add New Schedule'), /* Add New Display Title */
        
'edit' => __'Edit' ), /* Edit Dialog */
        
'edit_item' => __('Edit Schedules'), /* Edit Display Title */
        
'new_item' => __('New Schedule'), /* New Display Title */
        
'view_item' => __('View Schedule'), /* View Display Title */
        
'search_items' => __('Search Schedule'), /* Search Custom Type Title */
        
'not_found' => __('Nothing found in the Database.'), /* This displays if there are no entries yet */
        
'not_found_in_trash' => __('Nothing found in Trash'), /* This displays if there is nothing in the trash */
        
'parent_item_colon' => ''
        
), /* end of arrays */
        
'description' => __'This is the Schedules post type' ), /* Custom Type Description */
        
'public' => true,
        
'publicly_queryable' => true,
        
'exclude_from_search' => false,
        
'show_ui' => true,
        
'query_var' => true,
        
'menu_position' => 5/* this is what order you want it to appear in on the left hand side menu */
        
'menu_icon' => get_stylesheet_directory_uri() . '/images/announcements/calendar-admin.png'/* the icon for the custom post type menu */
        
'rewrite'    => array( 'slug' => 'schedule''with_front' => false ), /* you can specify it's url slug */
        
'has_archive' => 'schedule'/* you can rename the slug here */
        
'capability_type' => 'post',
        
'hierarchical' => false,
        
/* the next one is important, it tells what's enabled in the post editor */
        
'supports' => array( 'title''editor''author''thumbnail''excerpt''trackbacks''custom-fields''comments''revisions''sticky')
        ) 
/* end of options */
    
); /* end of register post type */

/* this ads your post categories to your custom post type */
register_taxonomy_for_object_type('category''schedule');
/* this ads your post tags to your custom post type */
register_taxonomy_for_object_type('post_tag''schedule');

}

// adding the function to the Wordpress init
add_action'init''schedule_post_type');


// now let's add custom categories (these act like categories)
    
register_taxonomy'custom_cat',
     array(
'schedule'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
     
array('hierarchical' => true/* if this is true it acts like categories */
     
'labels' => array(
     
'name' => __'Schedule Categories' ), /* name of the custom taxonomy */
     
'singular_name' => __'Schedule Category' ), /* single taxonomy name */
     
'search_items' => __'Search Schedule Categories' ), /* search title for taxomony */
     
'all_items' => __'All Schedule Categories' ), /* all title for taxonomies */
     
'parent_item' => __'Parent Schedule Category' ), /* parent title for taxonomy */
     
'parent_item_colon' => __'Parent Schedule Category:' ), /* parent taxonomy title */
     
'edit_item' => __'Edit Schedule Category' ), /* edit custom taxonomy title */
     
'update_item' => __'Update Schedule Category' ), /* update title for taxonomy */
     
'add_new_item' => __'Add New Schedule Category' ), /* add new title for taxonomy */
     
'new_item_name' => __'New Schedule Category Name' /* name title for taxonomy */
     
),
     
'show_ui' => true,
     
'query_var' => true,
     )
    );
    
// now let's add custom tags (these act like categories)
    
register_taxonomy'custom_tag',
     array(
'schedule'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
     
array('hierarchical' => false/* if this is false, it acts like tags */
     
'labels' => array(
     
'name' => __'Schedule Tags' ), /* name of the custom taxonomy */
     
'singular_name' => __'Schedule Tag' ), /* single taxonomy name */
     
'search_items' => __'Search Schedule Tags' ), /* search title for taxomony */
     
'all_items' => __'All Schedule Tags' ), /* all title for taxonomies */
     
'parent_item' => __'Parent Schedule Tag' ), /* parent title for taxonomy */
     
'parent_item_colon' => __'Parent Schedule Tag:' ), /* parent taxonomy title */
     
'edit_item' => __'Edit Schedule Tag' ), /* edit custom taxonomy title */
     
'update_item' => __'Update Schedule Tag' ), /* update title for taxonomy */
     
'add_new_item' => __'Add New Schedule Tag' ), /* add new title for taxonomy */
     
'new_item_name' => __'New Schedule Tag Name' /* name title for taxonomy */
     
),
     
'show_ui' => true,
     
'query_var' => true,
     )
    ); 

Can anyone tell me what I may be doing wrong?

Thank you,
Keith

andrea_r 11-06-2012 05:42 AM

Travis has a full series on working with custom post types and Genesis over here:
http://wpsmith.net/category/custom-post-types/

KHCreative 11-06-2012 07:44 AM

Great, thanks. Looks like I have some reading to do. While I'm sifting through this, if anyone has an idea or solution, please chime in. At first glance I don't see my answer easily found in Travis' posts. I know it's in there, but it may require a lot of code altering to match his in an effort to figure it out. I feel like I'm just missing one piece of the puzzle, since I can see the post associated with the category in the post count on the category admin page.

Thanks again,
Keith

Bill Murray 11-06-2012 12:47 PM

I think the problem is that normal archives for categories or tags don't include CPT's, so you have to add them. But just adding them to the filter can break custom menus. The code below adds CPT's to category or tag archives and accounts for custom menus.

Note, you have to change yourcustomposttype to your CPT, which in your case is schedule.

PHP Code:

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


Hope that helps.

KHCreative 11-06-2012 01:23 PM

Bill - I have been thinking about this today and thought the problem involved the archives not recognizing my cpt. I'll try your code and see how it works and let you guys know. I really appreciate your time responding and helping me out.

Have a great day.
Keith

KHCreative 11-06-2012 07:50 PM

Well, I suppose I've spent to much time behind the computer today, because I drawing a blank as to where to use this code. I tried writing it in the functions file and the archive page of the child theme (minimum). Any chance someone could hold my hand further, as I'm having a brain fart?

Thanks,
Keith

Bill Murray 11-06-2012 07:59 PM

It goes in the functions.php file of your child theme if that is where your CPT is registered. Otherwise, you can put it in the file that registers your CPT. I recommend putting it after the registration code.

I pasted that code from a working site, where I grabbed it from functions.php, where the CPT is registered just before, so that does work.

Post back with more details if that doesn't and I'll try to help.

KHCreative 11-06-2012 10:36 PM

Bill, that did it. Not sure why I couldn't get it to work the first time I put it in the functions file. I was probably just over thinking it.

Thank you so much for your time,
Keith


All times are GMT -5. The time now is 04:45 AM.

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