0

I am getting a list of categories for a custom post type project using get_categories($args) with the following arguments:

$args = array(
'type'=> 'project',
'orderby'=> 'name',
'order'=> 'ASC',
'hide_empty'=> 1,
'hierarchical'=> 1,
'exclude'=> '',
'include'=> '',
'number'=> '',
'taxonomy'=> 'category',
'pad_counts'=> false 
);

Then I am iterating through the categories and linking to the archive with the following code:

<li><a href="<?=get_category_link( $category->term_id ); ?>" title="<?php sprintf( __( "View all posts in %s" ), $category->name ); ?>"><?=$category->name?></a></li>

The problem is, the resulting link is like:

http://domain.com/category/example_cat

which results in in a 404, rather than:

http://domain.com/projects/category/example_cat

I know I could do an str_replace() but it seems like there should be a proper way to do this, does anyone know how to ensure the category link is properly generated with the custom post type?

EDIT: I am rewriting the slug in the custom post type declaration with 'rewrite' => array('slug' => 'projects')

  • how have you changed your category links to /project/category/example_cat/? /category/example_cat/ is the default format. – Milo Sep 29 '13 at 16:11
  • I used the slug rewrite when declaring the custom post type: 'rewrite' => array('slug' => 'projects') – waffl Sep 30 '13 at 8:14
2

Your category URLs are correct, they won't change by virtue of being associated with a custom post type. I'll guess that the problem is that default category queries do not include any custom post types, so will display "Nothing found" on term pages where the only associated posts are a CPT. To fix that, you need to alter category queries via pre_get_posts:

function wpa_cpt_in_categories( $query ){
    if ( ! is_admin()
        && $query->is_category()
        && $query->is_main_query() ) {
            $query->set( 'post_type', array( 'post', 'project' ) );
        }
}
add_action( 'pre_get_posts', 'wpa_cpt_in_categories' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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