I am well aware that there are several posts covering the topic of customizing the permalink URL structure of custom post types, custom taxonomies, and taxonomy terms. However, none of them are real clear, don't work correctly, or cause problems elsewhere.
Here are a few links, which already "kind of" cover URL rewriting.
- How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- Custom post types, taxonomies, and permalinks
- Mixing custom post type and taxonomy rewrite structures?
I am looking to create a permalink URL structure along the lines of:
- Post Type Name: cars
- Taxonomy Name: dealers
- Term Name: honda
- Post Title: 123 Hot Rod Lane
Example setup of the ideal permalink URL structure:
Generic permalink structure:
domain.com/post-type-name/taxonomy-name/term-name/post-title/
More specific permalink structure:
domain.com/cars/dealers/honda/123-hot-rod-lane/
/cars/
needs to be accessible as an archive listing of every post made in the post type./cars/dealers/
needs to be accessible as an archive listing of every post made in the taxonomy./cars/dealers/honda/
needs to be accessible as an archive listing of every post made in that taxonomy term./cars/dealers/honda/123-hot-rod-lane/
needs to be accessible as a singular view of the post made in the cars custom post type.
Just to be clear, so no one does any unnecessary work while replying to this question. I do NOT need anything other than just adding the correct rewrite rules for my post type, taxonomy, and terms. This questions is directly in regards to permalink URL rewriting, and NOT in relation to listing of posts or archives. I just need to be sure WordPress recognizes them properly, while using conditional calls like is_post_type_archive()
or is_tax()
or is_singular()
, etc.
I've attempted numerous times, from numerous questions/answers on this site. Nothing seems to work.
Here is my registered custom post type, and registered custom taxonomy:
public function register_custom_data(){
/* Register a post type for Cars. */
$labels = array(
'name' => _x('Cars', 'post type general name'),
'singular_name' => _x('Car', 'post type singular name'),
'add_new' => _x('Add New Car', 'Car'),
'add_new_item' => __('Add New Car'),
'edit_item' => __('Edit Car'),
'new_item' => __('New Car'),
'all_items' => __('All Cars'),
'view_item' => __('View Car'),
'search_items' => __('Search Cars'),
'not_found' => __('No Cars found'),
'not_found_in_trash' => __('No Cars found in Trash'),
'parent_item_colon' => '',
'menu_name' => __('Cars')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'can_export' => true,
'query_var' => true,
'rewrite' => true,
'menu_icon' => NULL,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => NULL,
'supports' => array(
'title',
'editor',
'thumbnail'
),
'rewrite' => array(
'slug' => 'cars',
'with_front' => true
)
);
register_post_type('mbe_cars', $args);
/* Car Dealers */
$labels = array(
'name' => _x('Dealers', 'taxonomy general name'),
'singular_name' => _x('Dealer', 'taxonomy singular name'),
'search_items' => __('Search Dealers'),
'all_items' => __('All Dealers'),
'parent_item' => __('Parent Dealer'),
'parent_item_colon' => __('Parent Dealer:'),
'edit_item' => __('Edit Dealer'),
'update_item' => __('Update Dealer'),
'add_new_item' => __('Add New Dealer'),
'new_item_name' => __('New Dealer Name'),
'menu_name' => __('Dealers')
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'dealers',
'hierarchical' => true
)
);
register_taxonomy('mbe_car_dealers', 'mbe_cars', $args);
/* Update urls. */
flush_rewrite_rules();
}
init
every time. In the long run, flushing would be moved to activation/deactivation of the plugin.bp-core-catchuri.php
to see what I'm talking about.