0

I have a custom post type called "artists". Attached to this is a custom taxonomy, genre.

Now, URLs /genre/<post slug> works perfectly fine, however the naked taxonomy page /genre/ gives a 404 error, and I'm struggling to find the error in my code that would do this.

This is my code:

// Genre
add_action( 'init', '_ct_genre', 0 );
function _ct_genre() {
    $label_p = 'Genrer';
    $label_s = 'Genre';
    register_taxonomy( 'genre', 'artists', array(
        'labels'                    => array(
            'name'                      => $label_p,
            'singular_name'             => $label_s,
            'menu_name'                 => $label_p,
            'all_items'                 => 'Alla ' . strtolower($label_p),
            'parent_item'               => 'Förälder',
            'parent_item_colon'         => 'Förälder:',
            'new_item_name'             => 'Nytt namn på ' . strtolower($label_s),
            'add_new_item'              => 'Ny ' . strtolower($label_s),
            'edit_item'                 => 'Redigera ' . strtolower($label_s),
            'update_item'               => 'Uppdatera ' . strtolower($label_s),
            'search_items'              => 'Sök ' . strtolower($label_p),
            'add_or_remove_items'       => 'Lägg till eller Ta bort ' . strtolower($label_p)
        ),
        'hierarchical'              => false,
        'public'                    => true,
        'show_ui'                   => true,
        'show_admin_column'         => true,
        'show_in_nav_menus'         => true,
        'has_archive'               => true,
        'show_tagcloud'             => true,
        'update_count_callback'     => '_update_post_term_count',
        'query_var'                 => true,
        'publicly_queryable'        => true,
        'rewrite'                   => array(
            'slug'                      => 'genre',
            'with_front'                => true,
            'hierarchical'              => true
        )
    ) );
}

All similar issues on StackExchange that I have found, does not apply here.

  • I've tried setting query_var to a string 'genre' instead, however that didn't work either.
  • I have of course flushed permalinks after each and every change.
7
  • After some more googling, I realize that this is default WP behaviour, as this answer states: wordpress.stackexchange.com/questions/235450/…. It's very unsatisfying, and I'd like to have to resolved. So if anyone has any ideas, I'm all ears. Oct 31, 2019 at 12:37
  • has_archive is for register_post_type() and not register_taxonomy(). Have you tried 'has_archive' => 'genre' with register_post_type()? That will give you a custom post type archive accessible at example.com/genre. However, the posts being displayed would be from any taxonomies attached to the post type.
    – Sally CJ
    Oct 31, 2019 at 13:40
  • Yes I realize has_archive is for CPTs, not taxonomies. I merely did a test and forgot to remove it. Oct 31, 2019 at 14:26
  • An option I see possible is by creating a standard Page, give it the slug genre and assign it to a custom template, and in that template, you can run a custom SQL like this. Or you can try tax_query with the terms set to term IDs of terms in the genre taxonomy - example.
    – Sally CJ
    Oct 31, 2019 at 16:36
  • 1
    @SallyCJ That is essentially what I ended up doing :-) Although a custom SQL query isn't necessary. WP_query does everything that's needed. Nov 5, 2019 at 10:15

0

Your Answer

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