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've got a question that I've seen being asked before, but none of the answers solved my problems. It's something I've run into before, but never managed to solve.

I've got a clean Wordpress installation and in the functions.php file I've created a custom post type

register_nav_menu('main', 'Main navigation menu');

function add_custom_post_type() {
    $labels = array(
        'name' => _x('Books', 'post type general name'),
        'singular_name' => _x('Book', 'post type singular name'),
        'add_new' => _x('Add New', 'book'),
        'add_new_item' => __('Add New Book'),
        'edit_item' => __('Edit Book'),
        'new_item' => __('New Book'),
        'all_items' => __('All Books'),
        'view_item' => __('View Book'),
        'search_items' => __('Search Books'),
        'not_found' =>  __('No books found'),
        'not_found_in_trash' => __('No books found in Trash'), 
        'parent_item_colon' => '',
        'menu_name' => __('Books')
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    ); 

    register_post_type('book', $args);
}

I've made an archive-book.php and single-book.php template files for those and everything works okay. I can view the books when I go to "http://localhost/wordpress/book" Now I want to create a custom taxonomy, so I did this in functions.php as well:

add_action('init', 'add_custom_post_type');

function add_custom_taxonomy() {
    register_taxonomy(
        'rating',
        'book',
        array(
            'label' => 'Rating',
            'hierarchical' => true,
            'show_ui' => true,
            'query_var' => 'book',
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'book/rating'
            )
        )
    );
}

And the taxonomy shows up in the admin page, I can add new terms, and add them to books. But now comes the problem. I've I want to go to an overview of all books with, let's say, rating "nice", I would go to "http://localhost/wordpress/book/rating/nice". I've created the following template files: taxonomy-rating-nice.php, taxonomy-rating.php and taxonomy.php.

But somehow wordpress just shows the index.php file and doesn't list any books. I've tried flushing the rewrite rules with:

add_action('init', 'custom_taxonomy_flush_rewrite');
function custom_taxonomy_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

And:

flush_rewrite_rules(false);

But nothings seems to help.

So, anybody who can help me? If you need more information, please tell, cause I really want to fix this problem!

share|improve this question
add comment

1 Answer

You have a few issues -

  1. You're setting query_var to 'book' in your custom taxonomy, which is clashing with the query var for your custom post type. change that to true and it will default to the name of the taxonomy, 'rating'.

  2. There is no has_archive argument for register_taxonomy, though this is probably not effecting anything. This makes sense if you think about it, since a taxonomy term is always a form of archive.

  3. The last issue you may encounter is one of precedence - if multiple rewrite rules apply to a given URL, the one with highest precedence will be chosen. If you register your post type first, then your taxonomy, using your example http://localhost/wordpress/book/rating/nice, WordPress will think you want to load an attachment named nice belonging to a book named rating. If you register your taxonomy first, then register your post type, your taxonomy will take precedence in this case and everything should work as expected.

share|improve this answer
add comment

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.