0

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' => '',
)
) ); 
2
  • show us the code you use to register the taxonomy and post type.
    – Milo
    Dec 12, 2013 at 16:22
  • Alright added the code. Dec 12, 2013 at 16:43

2 Answers 2

0

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

1
  • 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. Dec 12, 2013 at 16:17
0

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;
  }
}
3
  • Ok.. You can remove || is_tag() so it doesn't add CPT to tag archive page.
    – pendjer
    Dec 12, 2013 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? Dec 12, 2013 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, 2013 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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