Why do I get 404 for my custom taxonomy?

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

add_action('init', 'create_publication_categories');
function create_publication_categories() {
    $args = array(
        'label' => __('Categories'),
        'has_archive' =>  true,
        'hierarchical' => true,
        'rewrite' => array(
            'slug' => 'topics',
            'with_front' => false
        ),
    );
    $postTypes = array(
        'publication'
    );
    $taxonomy = 'publication';
    register_taxonomy($taxonomy, $postTypes, $args);
}

So I have the template called taxonomy-publication.php, but I still get 404.

I have reset the permalink following this. And other answers such as this and this.

But I still get 404. Any ideas what I have missed?

up vote 3 down vote accepted

You use the same slug publication for custom taxonomy and custom post type. Slug should be unique.


Another thing (not related with 404) is the flush_rules. As you can read here flush on the init hook is bad idea.

Important:

  • Flushing the rewrite rules is an expensive operation, there are tutorials and examples that suggest executing it on the init hook. This is bad practice. It should be executed either on the 'shutdown' hook, or on plugin/theme (de)activation.
  • Flush rules once (it's better to store the state in option, instead of activation or deactivation, because on MultiSite they are useless), or when you know that the rewrite rules need to be changed. Don't do it on any hook that will triggered on a routine basis. More detail information in the comments on WP Engineer's post: Custom Post Type and Permalink
  • Make sure custom post types and taxonomies are properly registered before flushing rewrite rules, especially during plugin activation: Activation Checklist for WordPress Plugin Developers (inaccessible)

Example

Flush rules on theme activation:

add_action( 'after_switch_theme', 'custom_taxonomy_flush_rewrite' );
function custom_taxonomy_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
  • Thanks for the answer again. btw, how do I flush properly then if flush on the init hook is bad idea.? – laukok Aug 20 at 20:18
  • 2
    You can use after_switch_theme action hook or use option to store flush status. Before $wp_rewrite->flush_rules() check if option (e.g get_option('flush_done', 0) == 0) exists and save after flush (update_option('flush_done', 1)). @laukok – nmr Aug 20 at 20:41

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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