Make WordPress Core

Opened 15 years ago

Closed 15 years ago

#13022 closed defect (bug) (worksforme)

Pretty Permalinks not working with custom post type

Reported by: bigdawggi's profile bigdawggi Owned by: ryan's profile ryan
Milestone: Priority: normal
Severity: major Version: 3.0
Component: Permalinks Keywords: permalinks, rewrite, custom post type, post type
Focuses: Cc:

Description

Not sure if this is related to #12870, but it seems like a different issue so I'm starting up a new ticket. Feel free to merge if necessary.

I registered a custom post type (FAQ) as follows:

	register_post_type('FAQ', array(
		'label' => __('FAQs'),
		'singular_label' => __('FAQ'),
		'public' => true,
		'show_ui' => true,
		'capability_type' => 'post',
		'hierarchical' => true,
		'rewrite' => array('slug' => 'FAQ'),
		'query_var' => true,
		'supports' => array('title', 'editor', 'author'),
		'taxonomies' => array('category'),
	));

	// Allow the category taxonomy to be used for FAQs as well
	register_taxonomy_for_object_type('category', 'FAQ');

I have permalinks set to a fairly standard:

/%year%/%monthnum%/%postname%/

When I visit the url for the published FAQ (http://3.local/FAQ/test-1-in-faq/) it gives me the "Hello World" post, which isn't a FAQ. :)

If I go to this URL (http://3.local/FAQ/test-1-in-faq/?post_type=FAQ) it returns the proper post.

If I turn off permalinks entirely I can properly visit the post at: http://3.local/?post_type=FAQ&p=3

I also tried using a lower-case slug for the rewrite's slug value, but http://3.local/faq/test-1-in-faq/ returns a 404 page.

I'm using Rev 13954 (I believe that's the 3.0b release), but have also experienced similar behavior using the latest trunk.

Change History (7)

#1 @bigdawggi
15 years ago

  • Cc matt@… added

#2 @greenshady
15 years ago

Did you flush your rewrite rules? A quick and easy way to do this is to just visit your Permalinks settings page in the admin.

#3 @williamsba1
15 years ago

  • Cc brad@… added

Had the same issue and visiting the permalink settings page fixed the issue. Is there anyway we can automate this process when a new custom post type is created?

#4 @bigdawggi
15 years ago

Looks like that does work guys! I thought I had done that a dozen times, but must not have ever been in the right order ;)

So the process to make it work is:

  • define custom post type
  • visit permalinks settings page (which flushes the rewrite rules) -- don't even need to change or re-save the permalink settings
  • go add posts with custom post type

Thanks for the help.

The one issue with flushing rewrite rules each time is that the register_post_type function gets ran at 'init'..and so it'd be performance-prohibitive to flush the rewrite rules at every time WP spins up.

Potential Resolution:

  • register_post_type() checks a "registered_post_types" array in the options table for its post type.
  • If that post type does *not* exist in the array, then flush rewrite rules, and add that custom post type to the array.
  • do some sort of "cleanup" of the "registered_post_types" array via cron ???

#5 @bigdawggi
15 years ago

For what it's worth, indeed calling " flush_rewrite_rules();" after the registering of a new post type works great. Now to figure out how to make it scalable.

oh, and sorry for the lack of formatting in last post, here's the lists:

So the process to make it work is:

  1. define custom post type
  2. visit permalinks settings page (which flushes the rewrite rules) -- don't even need to change or re-save the permalink settings
  3. go add posts with custom post type

Potential Resolution:

  1. register_post_type() checks a "registered_post_types" array in the options table for its post type.
  2. If that post type does *not* exist in the array, then flush rewrite rules, and add that custom post type to the array.
  3. do some sort of "cleanup" of the "registered_post_types" array via cron ???

#6 follow-up: @greenshady
15 years ago

I was thinking the decision to do this with post types and taxonomies came down to leaving it to the plugin. When activating the plugin, it should flush the rewrite rules.

#7 in reply to: ↑ 6 @nacin
15 years ago

  • Milestone Unassigned deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Replying to greenshady:

I was thinking the decision to do this with post types and taxonomies came down to leaving it to the plugin. When activating the plugin, it should flush the rewrite rules.

Correct. No different than any other changes to rewrite rules.

Note: See TracTickets for help on using tickets.