WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#15239 closed defect (bug) (invalid)

register_post_type rewrite argument breaks template hierarchy

Reported by: croakingtoad Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.1
Component: Posts, Post Types Keywords: register_post_type
Focuses: Cc:

Description

I have been working on this issue for days and have discovered, that when using template hierarchy for custom post types, for example, a CPT of 'sets' would use single-sets.php as a template page. This works fine, except when the CPT is called 'lego-sets' and you rewrite it to 'sets' then the template hierarchy "breaks" and WP resorts to single.php or some other generic file in the template hierarchy.

Doesn't work because of the rewrite with template hierarchy:

add_action('init','create_post_types');
function create_post_types() {
	$sets_labels = array(
		'name' 				=> __('LEGO Sets','post type general name'),
		'singular_name' 	=> __('LEGO Set','post type singular name'),
		'add_new'			=> __('Add New','sets'),
		'add_new_item'		=> __('Add New LEGO Set'),
		'edit_item'			=> __('Edit LEGO Set'),
		'new_item'			=> __('New LEGO Set'),
		'view_item'			=> __('View LEGO Set'),
		'search_items'		=> __('Search LEGO Sets'),
		'not_found'			=> __('No LEGO Sets found'),
		'not_found_in_trash' => __('No LEGO Sets found in Trash'),
		'parent_item_colon'	=> ''
	);
	$sets_args = array(
		'labels' 				=> $sets_labels,
		'public'				=> true,
		'publicly_queryable'	=> true,
		'show_ui'				=> true,
		'query_var'				=> true,
                // this is the problem line, if you set it to just true or false it's fine, but if you add the array and slug it breaks
		'rewrite'				=> array('slug' => 'sets', 'with_front' => false),
		'capability_type'		=> 'post',
		'hierarchical'			=> false,
		'menu_position'			=> null,
		'supports'				=> array('title','editor','author','thumbnail','excerpt','comments')
	);
	$sets = register_post_type('lego-sets', $sets_args);

Change History (4)

#1 @croakingtoad
10 years ago

I should add, in the above instance, neither single-lego-sets.php nor single-sets.php works. In fact, it doesn't seem to matter what you call the CPT or rewrite it to, the template hierarchy just stops working. If you leave the rewrite argument off or just give it a true/false value (instead of a rewritten slug) then the template hierarchy is preserved.

#2 @nacin
10 years ago

  • Milestone changed from Awaiting Review to 3.1

It should still work using the post type name, not the rewrite slug value.

#3 @sorich87
10 years ago

I just tried this with the latest trunk and it works with the post type name (single-lego-sets.php) as expected.

#4 @nacin
10 years ago

  • Milestone 3.1 deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.