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'm really hoping someone can help shed some light on this for me, it's getting very confusing and seems no-one has answered a question like this yet.

I have a custom post type (news) and a hierarchal custom taxonomy (news_category) - the two are linked. What I am trying to do is to get a clean permalink structure, that works in a similar way to how the default posts / categories works, namely:

Category URL: httpslashexampleurldotcom/news/category-1/

Second level category: httpslashexampleurldotcom/news/category-1/category-2/

Individual post: httpslashexampleurldotcom/news/category-1/category-2/post-title/

I can see that this must be possible somehow, as the default posts taxonomy does this, however duplicating the default taxonomy doesn't provide the same results. I believe that the default 'category' taxonomy is actually hardcoded to be treated differently, on line 206 of /wp-includes/canonical.php:

} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
        $category = get_category_by_path( $cat );
        $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
        if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
            $redirect_url = get_permalink($wp_query->get_queried_object_id());
    }

Here's what I have tried so far:

  1. As mentioned, I have duplicated the code to generate the categories taxonomy, without success.

  2. I have added the rewrite rules to the taxonomy and custom post type. This allows one of the two to work, depending on which one is first - so if taxonomy is first in the functions.php then taxonomy works and the individual posts return a 404. If I switch them the individual posts starts working and the taxonomy returns a 404. This indicates to me that the slug can only be used once, either for taxonomy or for CPT but not at the same time.

  3. I have tried adding a rewrite JUST to the taxonomy, and writing a function that hooks the post_type_link filter and overrides with a full category permalink. This continued to return a 404.

  4. I have looked into the rewrite rules in place at the moment, I can see that some custom redirect is happening, specific to the category taxonomy, perhaps associated with the above chunk of code.

So, I am in the current situation. Taxonomy works as requested, with top level category, followed by second level, but I cannot get the associated custom post type to fit into the URL structure, continually returning a 404.

Here's the code I'm currently using to create my taxonomy / CPT:

global $wp_rewrite;

$news_labels = array(
    'name'                       => 'News Categories',
    'singular_name'              => 'News Category',
    'menu_name'                  => 'News Categories',
    'all_items'                  => 'All News Articles',
    'parent_item'                => 'Parent News Category',
    'parent_item_colon'          => 'Parent News Category:',
    'new_item_name'              => 'New News Category',
    'add_new_item'               => 'Add New News Category',
    'edit_item'                  => 'Edit News Category',
    'update_item'                => 'Update News Category',
    'separate_items_with_commas' => 'Separate news categories with commas',
    'search_items'               => 'Search news categories',
    'add_or_remove_items'        => 'Add or remove news categories',
    'choose_from_most_used'      => 'Choose from the most used news categories',
);

$news_args = array(
    'labels' => $news_labels,
    'hierarchical' => true,
    'query_var' => 'category_name',
    'rewrite' => array(
        'hierarchical' => true,
        'slug' => 'news',
        'with_front' => $wp_rewrite->using_index_permalinks(),
        'ep_mask' => EP_CATEGORIES,
    ),
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
);

register_taxonomy('news_category', 'news', $news_args );

and the CPT:

$news_labels = array(
    'name' => _x( 'News Articles', 'news' ),
    'singular_name' => _x( 'News Article', 'news' ),
    'add_new' => _x( 'Add New Article', 'news' ),
    'add_new_item' => _x( 'Add New News Article', 'news' ),
    'edit_item' => _x( 'Edit News Article', 'news' ),
    'new_item' => _x( 'New News Article', 'news' ),
    'view_item' => _x( 'View News Article', 'news' ),
    'search_items' => _x( 'Search News', 'news' ),
    'not_found' => _x( 'No news found', 'news' ),
    'not_found_in_trash' => _x( 'No news found in Trash', 'news' ),
    'parent_item_colon' => _x( 'Parent News Article:', 'news' ),
    'menu_name' => _x( 'News Articles', 'news' ),
);

$news_args = array(
    'labels' => $news_labels,
    'description' => 'News articles',
    'supports' => array( 'title', 'editor', 'author', 'revisions' ),
    'taxonomies' => array( 'news' ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'menu_icon' => '/wp-content/themes/themename/images/icons/custom-post-types/news.png',
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive' => true,
    'can_export' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => false,
    'query_var' => false,
);

register_post_type('news', $news_args);

Can anyone help?

Many thanks,

Ryan

share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.