0

I had an issue with a custom taxonomy and a custom post type using the same URL.

My URL I am using for my post type is /news and then I have a taxonomy which I need to have /news/cars

I followed this post to get my URL's to work but now I cant access the actual taxonomy listing pages as i get a 404.

Mixing custom post type and taxonomy rewrite structures?

This is my code I am using to do this functionality:

    function register_custom_post_types() {
    $args = array(
        'labels' => array(
            'name' => __( 'News' ),
            'singular_name' => __( 'News' )
        ),
        'menu_position' => 5,
        'public' => true,
    'query_var' => 'news',
        'has_archive' => 'news',
        'hierarchical' => true,
        'taxonomies' => array('news_categories'),
    'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => array('with_front' => false, 'slug' => 'news/%news_categories%'),
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );
    register_post_type( 'news', $args);

}
add_action( 'init', 'register_custom_post_types' );

add_action( 'init', 'create_my_taxonomies', 0 );

    function create_my_taxonomies() {
        register_taxonomy(
            'news_categories',
            'news',
            array(
                'labels' => array(
                    'name' => 'News Categories',
                    'add_new_item' => 'Add New Category',
                    'new_item_name' => "New Category"
                ),
                'query_var' => 'news',
                'show_ui' => true,
                'show_tagcloud' => false,
                'hierarchical' => true,
                'rewrite' => array(
                    'with_front' => false,
                    'slug' => 'news'
                )
            )
        );
    }

    add_filter('post_type_link', 'news_term_permalink', 10, 4);
    function news_term_permalink($post_link, $post, $leavename, $sample) {
        if ( false !== strpos( $post_link, '%news_categories%' ) ) {
            $glossary_letter = get_the_terms( $post->ID, 'news_categories' );
            $post_link = str_replace( '%news_categories%', array_pop( $glossary_letter )->slug, $post_link );
        }
        return $post_link;
    }
0

1 Answer 1

2

The only problem that I have found is 'query_var' => 'news' in register_taxonomy(). Change it to 'query_var' => true and it will start working.

function create_my_taxonomies() {
        register_taxonomy(
            'news_categories',
            'news',
            array(
                'labels' => array(
                    'name' => 'News Categories',
                    'add_new_item' => 'Add New Category',
                    'new_item_name' => "New Category"
                ),
                'query_var' => true,
                'show_ui' => true,
                'show_tagcloud' => false,
                'hierarchical' => true,
                'rewrite' => array(
                    'slug' => 'news'
                )
            )
        );
    }

Reference: Codex Register Taxonomy

2
  • Thanks That has got rid of my 404 :). I just need to get my posts to appear now. I can work with that. Thanks!!
    – asaunders
    Commented Feb 28, 2014 at 9:41
  • I know it's an old thread, but I leave it here for others with the same doubt. As this answer shows, we just need to reset permalinks by accessing the setting page and saving it as is so as the new taxonomy appears. I had the same problem and that worked as intended.
    – JorgeAmVF
    Commented Jan 27, 2019 at 22:28

Your Answer

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

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