0

I have custom post type for "rooms" registered, along with a custom taxonomy. Here is the code:

<?php

/*******************************************/
/* Rooms CPT
/*******************************************/

add_action('init', 'room_register');

function room_register() {

    $labels = array(
        'name' => _x('Rooms', 'post type general name'),
        'singular_name' => _x('Room', 'post type singular name'),
        'add_new' => _x('Add Room', 'add button'),
        'add_new_item' => __('Add New Room'),
        'edit_item' => __('Edit Room'),
        'new_item' => __('New Room'),
        'view_item' => __('View Room'),
        'search_items' => __('Search Rooms'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'has_archive' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_icon'     => 'dashicons-screenoptions',
        'supports' => array('title','editor'),
        'rewrite' => array(  'slug' => 'rooms/%room_category%', 'with_front' => false )
    //'rewrite' => array('slug' => 'products')
      );

    register_post_type( 'rooms' , $args );
}

/*******************************************/
/* Rooms Taxonomy
/*******************************************/

add_action( 'init', 'rooms_taxonomy', 0 );
function rooms_taxonomy(){

        $labels = array(
        'name'              => _x( 'Room Category', 'taxonomy general name' ),
        'singular_name'     => _x( 'Room Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Room Categories' ),
        'all_items'         => __( 'All Room Categories' ),
        'parent_item'       => __( 'Parent Room Category' ),
        'parent_item_colon' => __( 'Parent Room Category:' ),
        'edit_item'         => __( 'Edit Room Category' ),
        'update_item'       => __( 'Update Room Category' ),
        'add_new_item'      => __( 'Add New Room Category' ),
        'new_item_name'     => __( 'New Room Category' ),
        'menu_name'         => __( 'Room Categories' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_in_menu'          => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'rooms', 'with_front' => false ),
        '_builtin' => false
    );

    register_taxonomy( 'room_category', 'rooms', $args );

}

However when I try to visit the url on my website:

http://www.upperhouse.org/rooms

I am getting a 404 error. Shouldn't this be the landing page for the taxonomy? Very confused here. Thanks for any help!

  • taxonomies don't have landing pages, only pages for each term in your taxonomy. – Milo Aug 17 '16 at 4:05
0

Now that you've made the custom taxonomy (and slug), you probably need to 'reindex' so to speak. Go to your Wordpress Admin > Settings > Permalinks and make a change (make note of your original setting). Save, then change back to the original setting and save again.

Your url new taxonomy should work then.

  • I tried this and also set up all the appropriate archive and single pages for my post type but the "home" page for the taxonomy is still returning 404. Right now I am working around this by putting all the rooms in an "all" category and displaying them on upperhouse.org/rooms/all. – James Hamann Aug 16 '16 at 22:24
0

James, you will probably need to create an archive-rooms.php file to display the page. See Post Type Templates docs here. In addition, you will need to re-save your permalinks after registering the post type.

Hope this helps.

EDIT

After inspecting your code further I see your Taxonomy slug is the same as your Custom Post Type slug. This may be why you are getting the 404 error. You may be able to still achieve that functionality by either:

  1. Changing the taxonomy slug and the custom post type slug to be unique.

OR

  1. Setting your permalink structure to /%postname%/ and follow instructions from this article.

I have not tested the second option personally but other users in the comments seem to have it working.

  • ^ Jason is right about the archive-rooms.php if you would like to customize your template, but even that won't work until you do the permalink resave we both mentioned. Thumbs up! – Faye Aug 16 '16 at 20:01
0

you'll need to either refresh your permalinks as @REactionFaye state. A more procedural method would be to use flush rewrite rules.

You can see from this page on the wordpress codex: https://codex.wordpress.org/Function_Reference/register_post_type:

If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.

If the code is in a plugin:

add_action( 'init', 'my_cpt_init' );
function my_cpt_init() {
    register_post_type( ... );
}

function my_rewrite_flush() {
    // First, we "add" the custom post type via the above written function.
    // Note: "add" is written with quotes, as CPTs don't get added to the DB,
    // They are only referenced in the post_type column with a post entry, 
    // when you add a post of this CPT.
    my_cpt_init();

    // ATTENTION: This is *only* done during plugin activation hook in this example!
    // You should *NEVER EVER* do this on every page load!!
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

while if it's in your theme:

add_action( 'init', 'my_cpt_init' );
function my_cpt_init() {
    register_post_type( ... );
}

function my_rewrite_flush() {
    my_cpt_init();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );

There are lots of other goodies too on that page.

as for the problem though. you may want to check your rewrite rule for room cateogry and see if you can get it loaded by just saying: (your category should not be the CPT name. so i changed "room" to "room-types".

    'rewrite' => array( 'slug' => 'room-types', 'with_front' => false ),

and change the actual CPT rewrite to this:

    'rewrite' => array(  'slug' => 'rooms', 'with_front' => false )

Lastly, as a sidenote to make sure there are no conflicts now or in the future, espceically because you're already using rewrites, you should not use such generic term for your cpt... try adding a prefix. ie: rt_rooms. (this tip is also from the codex, but has saved me before)

Again remember, after you make the changes, if you're not using flush re-write, you need to manually flush by going to settings and saving the permalink page.

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.