Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type called "work" and it has a taxonomy called "categories". Currently "/work" and "/work/category_slug/work_slug" works great. However "/work/category_slug" does not. I get a 404 page. Any ideas what I can do?

register_post_type('work', array(
'label' => 'Work',
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'work', 'with_front' => true),
'query_var' => true,
'has_archive' => true,
'supports' => array('title','editor','custom-fields','comments','revisions','page-attributes'),
'taxonomies' => array('categories'),
'labels' => array (
  'name' => 'Work',
  'singular_name' => 'work',
  'menu_name' => 'Work',
  'add_new' => 'Add New',
  'add_new_item' => 'Add New Item',
  'edit' => 'Edit ',
  'edit_item' => 'Edit Item',
  'new_item' => 'New Item',
  'view' => 'View',
  'view_item' => 'View Item',
  'search_items' => 'Search Items',
  'not_found' => 'No Work Items Found',
  'not_found_in_trash' => 'No Work Items found in Trash',
  'parent' => 'Parent Work Item',
)
) ); 


register_taxonomy( 'categories',array (
   0 => 'work',
),
array( 'hierarchical' => true,
    'label' => 'Categories',
    'show_ui' => true,
    'query_var' => true,
    'show_admin_column' => false,
    'labels' => array (
  'search_items' => 'Category',
  'popular_items' => '',
  'all_items' => '',
  'parent_item' => '',
  'parent_item_colon' => '',
  'edit_item' => '',
  'update_item' => '',
  'add_new_item' => '',
  'new_item_name' => '',
  'separate_items_with_commas' => '',
  'add_or_remove_items' => '',
  'choose_from_most_used' => '',
)
) ); 
share|improve this question
    
show us the code you use to register the taxonomy and post type. –  Milo Dec 12 '13 at 16:22
    
Alright added the code. –  paper_robots Dec 12 '13 at 16:43

2 Answers 2

It should be "categories/category_slug" not "work/category_slug" for the category archieve page

share|improve this answer
    
I've actually had it working the way I described, but I set it up over a year ago and can't remember what I did. Having "categories" in the url is just so ugly. –  paper_robots Dec 12 '13 at 16:17

Add this code to your functions.php.

// Include Custom Post Types on Tags and Categories Archive Pages

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag() || is_taxonomy()) {
    $post_type = get_query_var('post_type');
if($post_type)
  $post_type = $post_type;
 else
 $post_type = array('work', 'post'); // replace cpt to your custom post type
 $query->set('post_type',$post_type);
 return $query;
  }
}
share|improve this answer
    
Ok.. You can remove || is_tag() so it doesn't add CPT to tag archive page. –  pendjer Dec 12 '13 at 17:34
    
I tried this but didn't work. I'm not using the default category taxonomy I created my own so would is_category still be relevant? –  paper_robots Dec 12 '13 at 23:49
    
@paper_robots Just add is_taxonomy into this function. codex.wordpress.org/Function_Reference/is_taxonomy. Check updated code. –  pendjer Dec 16 '13 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.