1
votes

I don't know what I'm doing wrong. I have a custom post type called models. Within this custom post type I register also a custom taxonomy. This custom taxonomy is used for categories. But I getting every time a 404 error.

This is how I create my custom post type:

$labels = array(
        'name' => __('Modellen', 'models'),
        'singular_name' => __('Model', 'models'),
        'add_new' => __('Nieuwe toevoegen', 'models'),
        'add_new_item' => __('Nieuw model', 'models'),
        'edit_item' => __('Model bewerken', 'models'),
        'new_item' => __('Nieuw model', 'models'),
        'view_item' => __('Model bekijken', 'models'),
        'search_items' => __('Model zoeken', 'models'),
        'not_found' => __('Niks gevonden', 'models'),
        'not_found_in_trash' => __('Niks gevonden in de prullenbak', 'models'),
        'parent_item_colon' => __('Model', 'models'),
        'menu_name' => __('Modellen', 'models'),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'supports' => array('title', 'page-attributes', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions'),
        //'taxonomies'  => array('category'), // Old
        //'taxonomies'  => array('category'), // New not used any more
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => false,
        'capability_type' => 'post',
    );

    register_post_type('models', $args);
    //register_taxonomy_for_object_type('category', 'models'); // Old
    //register_taxonomy_for_object_type('category', 'models'); // New not used any more

This is how I create my custom taxonomy:

$labels = array(
        'name'              => _x('Categorieën', 'taxonomy general name', 'textdomain'),
        'singular_name'     => _x('Categorie', 'taxonomy singular name', 'textdomain'),
        'search_items'      => __('Zoek Categorieën', 'textdomain'),
        'all_items'         => __('All Categorieën', 'textdomain'),
        'parent_item'       => __('Hoofd categorie', 'textdomain'),
        'parent_item_colon' => __('Hoofd categorie:', 'textdomain'),
        'edit_item'         => __('Bewerk categorie', 'textdomain'),
        'update_item'       => __('Update Categorie', 'textdomain'),
        'add_new_item'      => __('Nieuwe categorie', 'textdomain'),
        'new_item_name'     => __('Nieuwe categorie naam', 'textdomain' ),
        'menu_name'         => __('Categorieën', 'textdomain'),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_rest' => false,
        'show_tagcloud' => false,
        'show_in_quick_edit' => true,
        'show_admin_column' => false,
        'rewrite' => array('slug' => 'models', 'hierarchical' => true),
    );
    //register_taxonomy('category', array('models'), $args); // Old
    register_taxonomy('cat_models', array('models'), $args); // New

Thanks!

Edit:
Maybe good to mention:

  • I execute the code inside my own custom plugin.
  • I use the default Twenyseventeen theme
  • WordPress 4.8

Solution: Thanks to @AmitJoshi I needed to change the category to something else. Above the updated code with the changes.

3
Have you gone into Settings and clicked the "Save Permalinks" button? You don't actually have to change anything, just click the button, then try againJakeParis
Are you registering your taxonomy before calling register_taxonomy_for_object_type()?JakeParis
@JakeParis No i didn't.justkidding96
I am not 100% sure, but can you try this same code but only change the new taxonomy from category to something else? This might just work.Amit Joshi
@AmitJoshi You are the hero of today!! I will update my question with your solution.justkidding96

3 Answers

0
votes

Often when adding new rewrites (such as you might do upon adding new taxonomies in a plugin or theme), you need to refresh the rewrite rules. If you don't, you'll run into 404 errors when trying to access the new taxonomy or custom post type.

The easiest way to rebuild/refresh the rewrites is to go to Settings > Permalinks and click the "Save Changes" button. You don't have to make any changes, just hit save.

0
votes

Yeah you were trying to register a custom taxonomy with the reserved term category. That's why it was not working.

0
votes

The solution hanks to @AmitJoshi below the updated code.

This is how I create my custom post type:

$labels = array(
        'name' => __('Modellen', 'models'),
        'singular_name' => __('Model', 'models'),
        'add_new' => __('Nieuwe toevoegen', 'models'),
        'add_new_item' => __('Nieuw model', 'models'),
        'edit_item' => __('Model bewerken', 'models'),
        'new_item' => __('Nieuw model', 'models'),
        'view_item' => __('Model bekijken', 'models'),
        'search_items' => __('Model zoeken', 'models'),
        'not_found' => __('Niks gevonden', 'models'),
        'not_found_in_trash' => __('Niks gevonden in de prullenbak', 'models'),
        'parent_item_colon' => __('Model', 'models'),
        'menu_name' => __('Modellen', 'models'),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'supports' => array('title', 'page-attributes', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'revisions'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => false,
        'capability_type' => 'post',
    );

    register_post_type('models', $args);

This is how I create my custom taxonomy:

$labels = array(
        'name'              => _x('Categorieën', 'taxonomy general name', 'textdomain'),
        'singular_name'     => _x('Categorie', 'taxonomy singular name', 'textdomain'),
        'search_items'      => __('Zoek Categorieën', 'textdomain'),
        'all_items'         => __('All Categorieën', 'textdomain'),
        'parent_item'       => __('Hoofd categorie', 'textdomain'),
        'parent_item_colon' => __('Hoofd categorie:', 'textdomain'),
        'edit_item'         => __('Bewerk categorie', 'textdomain'),
        'update_item'       => __('Update Categorie', 'textdomain'),
        'add_new_item'      => __('Nieuwe categorie', 'textdomain'),
        'new_item_name'     => __('Nieuwe categorie naam', 'textdomain' ),
        'menu_name'         => __('Categorieën', 'textdomain'),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_rest' => false,
        'show_tagcloud' => false,
        'show_in_quick_edit' => true,
        'show_admin_column' => false,
        'rewrite' => array('slug' => 'models', 'hierarchical' => true),
    );
    register_taxonomy('cat_models', array('models'), $args);