Support » Developing with WordPress » Archive for Parent Taxonomy not working (404 error)

  • Hi, I created a taxonomy (not a post type, only a taxonomy) called Books. Here’s my code:

    function add_custom_taxonomies() {
      $labels = array(
        'name' => 'Books',
        'singular_name' => 'Book',
        'search_items' => 'Search Books',
        'popular_items' => 'Popular Books',
        'all_items' => 'All Books',
        'parent_item' => 'Parent Book',
        'parent_item_colon' => 'Parent Book:',
        'edit_item' => 'Edit Books', 
        'update_item' => 'Update Book',
        'add_new_item' => 'Add New Book',
        'new_item_name' => 'New Book Name',
        'separate_items_with_commas' => 'Separate Books with commas',
        'add_or_remove_items' => 'Add or remove Books',
        'choose_from_most_used' => 'Choose from the most used Books',
        'menu_name' => 'Books',
      ); 
    
      $capabilities = array (
                'manage_terms' => 'manage_options',
                'edit_terms' => 'manage_options',
                'delete_terms' => 'manage_options',
                'assign_terms' => 'edit_posts'
                );
    			
      register_taxonomy('Books','post',array(
        'hierarchical' => true,
        'labels' => $labels,
    	'capabilities' => $capabilities,
        'has_archive' => true,
        'show_ui' => true,
    	'show_admin_column' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'Books' ),
      ));
    
    }
    add_action( 'init', 'add_custom_taxonomies', 0 );

    It works fine, I added terms like Thriller, Romance… I can access the archive page for each term:

    But the archive page for all my Books taxonomy doesn’t work, I get a 404 error!

    I created taxonomy-books.php so it should work but I don’t know what I did wrong… I checked several sources for creating taxonomies and the code in my functions.php seems to be fine. If anyone knows why the archive for my Books taxonomy doesn’t work, thank you for your help! 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Support Volunteer

    Since you didn’t provide a link, I can’t test this myself… what if you use /Books instead of /books? You used a capital B when you specified the slug.

    Hi Steve, thanks for your reply! My site is only accessible with my IP for the moment… I tried Books instead of books and it still doesn’t work 🙁

    Well, I tried archive.php and taxonomy-books.php but none of them work… I’m quite lost to say the least. Any idea anyone? <:\

    Moderator bcworkz

    (@bcworkz)

    AFAIK, it’s “normal” for taxonomy-with-no-term requests to not work as expected. Never worked out of the box for me. The query required is actually quite different than a simple term query. You need either a custom query or a “pre_get_posts” modified query that uses the “tax_query” arg using the “EXISTS” operator. Plus the “taxonomy” arg of course.

    $query = new WP_Query([
      'tax_query'=> [
        ['taxonomy'=>'books', 
        'operator'=>'EXISTS',]
      ],
     ]);

    BTW, I suggest that taxonomy key names should follow slug rules — all lower case, only hyphen special character.
    register_taxonomy('books','post', $your_args_array );
    “books”, not “Books”. Not doing so can cause unpredictable behavior on some servers. You can use whatever you like for labels, it’s only the registered key that should follow slug rules.

    Hi bcworkz, thank you very much! These are useful tips, now I understand how it works. I’m gonna use your Query, I have one question though: I use the archive.php default template, is it possible to alter the loop from the archive page to make it work for the taxonomy as well, without having to create taxonomy-books.php? I don’t know much of coding, my question may be dumb, forgive me if it is <:)

    Thanks again for your great help bcworkz! 🙂

    Moderator bcworkz

    (@bcworkz)

    You can use the “template_include” filter to force any template be used instead of the default. Your callback to this filter could check if is_tax('books') and if true return the path for archive.php. For example:

    add_filter('template_include', function($t){
      if ( is_tax('books')) return get_archive_template();
      return $t;
    });

    Hi bcworkz! It took me a couple of days to try the code you provided, with no success at all, I’m afraid… Not that your code doesn’t work but something is wrong somewhere on my side, I guess. I did everything you explained but I still get a 404 error on http://www.mywebsite.com/books/ even though http://www.mywebsite.com/books/thriller works just fine.

    I searched Google for 2 days with no success neither but I found some debug code (debug_rewrite_rules) I added to my functions.php to know which template is loaded and even if I don’t know much about how it works, something caught my attention:

    ‘books’ is supposed to be a taxonomy, so why it is called a category: category_name=books meanwhile on the ‘thriller’ archive page I get topics=thriller? Shouldn’t it be topics=books instead of category_name=books? Is it why it doesn’t load the archive page? Is it searching for another page? I created a taxonomy-books.php as well but I still get a 404 error…

    There is defintely something wrong, and the more I’m searching, the less I understand what it could be. Sorry to be so tedious but when I created my taxonomy I thought the archive for taxonomy (parent+children) would work out of the box but it doesn’t. I’m quite lost here <:)

    Moderator bcworkz

    (@bcworkz)

    Well, as indicated, there’s a rewrite rule matching “baseless” permalinks (those without “category” or “tag” as part of the permalink) as category terms instead of the expected post name. WP does not normally have such a rule, so code on your site at some point introduced such a rule. The rule can persist even after the enabling code is removed.

    Try visiting the permalinks settings screen. This will cause rewrite rules to be regenerated. I would normally expect the /books/ request match to be for a post of that name (rewrite query name=books), which should still result in “Not Found” unless there is indeed a post named “Books”.

    The code from my first post was an example of a proper query in isolation, but wouldn’t work by itself unless properly applied. Properly applied, it will work regardless of what rewrite rule was matched. The difference would be in how the code recognizes the appropriate request. For example, even if a /books/ request is matched as a category name, code in “pre_get_posts” could recognize this and alter the query accordingly, unsetting the “category_name” query_var and setting the “tax_query” query_var as in my example.

    In a normal installation that does not match to category_name, “pre_get_posts” code should recognize the expected books term in the “name” query_var (instead of category_name), unset “name” and set “tax_query” as suggested.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.