0

I've created a custom taxonomy register as the code below, but it does not find the taxonomy-specialty.php nor archive.php templates (page not found error).

I did following (as recommended by other threads found on SE and the web): - re-save the permalinks - find the taxonomy settings page in wp-admin and when I hover over 'View' it points me to 'mysite.com/events/specialties/footology' for a 'Footology' taxonomy entry I created for a post of type 'event' - so the URL is as expected. However when visiting the link still get page not found error.

$specialty_args = array(
            'hierarchical' => false,
            // This array of options controls the labels displayed in the WordPress Admin UI
            'labels' => array(
                'name' => 'Specialty',
                'singular_name' => 'Specialty',
                'search_items' => 'Search specialties',
                'all_items' => 'All specialties',
                'edit_item' => 'Edit specialty',
                'update_item' => 'Update specialty',
                'add_new_item' => 'Add new specialty',
                'new_item_name' => 'New specialty name',
                'menu_name' => 'Specialties',
            ),

            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => 'specialty',

            // Control the slugs used for this taxonomy
            'rewrite' => array(
                'slug' => 'events/specialties', // This controls the base slug that will display before each term
                'with_front' => false, // Don't display the category base before "/specialties/"
                'hierarchical' => false
            ),
        );

        register_taxonomy('specialty', 'event', $specialty_args);
        register_taxonomy_for_object_type('specialty', 'event'); //says in https://codex.wordpress.org/Function_Reference/register_taxonomy under 'Usage' to do this

I don't know what is wrong. Help appreciated, thank you!

| improve this question | | | | |
  • do you register your taxonomy on the init action? are there posts assigned to the term you are viewing? what is your post type's URL structure and is it hierarchical? var_dump($wp_query) in the 404 template and look at what WordPress is querying for. – Milo Oct 22 '15 at 5:04
  • You are not very specific, but I think you are having this problem – Pieter Goosen Oct 22 '15 at 5:50
0

The page not found error doesn't mean that the template file is not found/used. Note that a URL returns a 404 status header (not found), then 404.php is used (or index.php if 404.php doesn't exist).

I think your real problem is that you have not flushed the rewrite rules after the taxonomy has been registered. To do it, follow these steps:

Manually: go to settings->permalinks and click the save button (you don't need to change anything, just click the save button).

Auto: in your plugin, use flush_rewrite_rules() during plugin activation hook (never use flush_rewrite_rules() on every page load). For example:

register_activation_hook( __FILE__, 'cyb_plugin_activation' );
function cyb_plugin_activation() {

    cyb_register_taxonomy();
    flush_rewrite_rules();

}

add_action( 'init', 'cyb_register_taxonomy' );
function cyb_register_taxonomy() {

    $specialty_args = array(
            'hierarchical' => false,
            // This array of options controls the labels displayed in the WordPress Admin UI
            'labels' => array(
                'name' => 'Specialty',
                'singular_name' => 'Specialty',
                'search_items' => 'Search specialties',
                'all_items' => 'All specialties',
                'edit_item' => 'Edit specialty',
                'update_item' => 'Update specialty',
                'add_new_item' => 'Add new specialty',
                'new_item_name' => 'New specialty name',
                'menu_name' => 'Specialties',
            ),

            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => 'specialty',

            // Control the slugs used for this taxonomy
            'rewrite' => array(
                'slug' => 'events/specialty', // This controls the base slug that will display before each term (renamed to specialty from specialties)
                'with_front' => false, // Don't display the category base before "/specialties/"
                'hierarchical' => false
            ),
        );

        register_taxonomy('specialty', 'event', $specialty_args);
        register_taxonomy_for_object_type('specialty', 'event');

}

register_deactivation_hook( __FILE__, 'cyb_plugin_deactivation' );
function cyb_plugin_deactivation() {

    // Flush the rewrite rules also on deactivation
    flush_rewrite_rules();

}
| improve this answer | | | | |
  • I did the flush of rewrite rules but still not working. The text on the page loaded ( {domain}/events/specialty/footology )is what I put into the 404.php template ... totally stumped on this :-( – gvanto Oct 22 '15 at 2:36
  • I've tried the code I posted and it works perfectly. I can not say why it is not working for you without more information. Also, I would like to know why you have edited my answer to change the rewrite slug. I copied it from your question and it was (and still is in your question) 'slug' => 'events/specialties',. Anyway, I've tested the code with both slugs, events/specialties and events/specialty, and both work correctly. Can you provide more informatioin? For example the code that you are using to register the events post type could be useful. – cybmeta Oct 22 '15 at 5:58
-2
'show_ui'           => true,
'show_admin_column' => true,
'query_var'         => 'specialty',
'has_archive'       => true, //make sure you have added this one

What is your archive name? What is your archive url? Actually, your archive file name should be like that archive-specialty.php, your post type archive url will be like this yourdomain.com/events/specialties. I have tested, it should work on yours.

If you are not sure about custom post type, use wordpress type plugin or use wordpress generator

| improve this answer | | | | |
  • I think you have mixed taxonomies and custom post types: has_archive is not a valid argument for taxonomies (see register_taxonomy() docs) and archive-{taxonomyname}.php is not a valid taxnomy template file (see tempalte hierarchy in WordPress). Note that the question is about taxonomies, not custom post types. – cybmeta Oct 21 '15 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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