1

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?

1 Answer 1

3

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();
}
3
  • Thanks for the answer again. btw, how do I flush properly then if flush on the init hook is bad idea.?
    – Run
    Commented Aug 20, 2018 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
    Commented Aug 20, 2018 at 20:41
  • or flush them manually by saving the settings>permalinks page..
    – honk31
    Commented Sep 26, 2018 at 7:43

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.