There is earlier post with similar title, but it does not look in to the WordPress 3.3, and that is important as 3.3 advertises interestingly: “Use the postname permalink structure without a performance penalty”
Problem with WordPress 3.2 and earlier was that first it looked page names, and then 404. It didn’t check the arbitrary post types first. 3.3 on the other hand must look post types, then pages, and finally 404 (as it advertises this feature). This implies that custom post types without slug should be simple, if they didn’t hard code post_type=post
somewhere.
I can’t find a 3.3 specific solution yet though.
Question: How can I define permalink struct “/%postname%/” for any given custom post type “xyz”?
Thanks.
This isn’t easily done in WP 3.3 unless you trick the rewrite rules to be in the correct spot and make wp_rewrite think verbose rules are being used in the front end. The class below works.
Holy car keys!
I think this works. It almost works, it’s super simple, only one line:P.S. If you try this at home, after adding this one line Go to “Settings” -> “Permalinks” and Save Changes, it refreshes the permalinks.
I was reading the WP
register_post_type()
source code and found a line:Needless to say but without slug I concluded it should work,
and it did. Even the permalink editing underneath the title in editor works correctly!Update: This breaks page permalinks, back to drawing board…
prettyboymp answer is almost the same I got yesterday, but I’m not happy with it. prettyboymp’s answer has a one flaw, it does not work when /%postname%/ is being used simultaenously on multiple post types.
Here is my answer, which also looks in to current structure, and creates array of post types to fallback on. There is one flaw in this too though, if two post types have same slug and both are /%postname%/ then it shows both.
I created a solution and i couldnt find a problem with it. Please try and tell me if you find a problem
Change ‘yemek’ with your post type name.
This link should answer your question:
http://ottopress.com/2011/how-the-postname-permalinks-in-wordpress-3-3-work/
The cleanest answer I could come up with for this (I’m building out a plugin that really needs a custom post type without any leading slug) is to use a custom page template instead of using a custom post type.
By doing this, your “custom post type” can have urls such as /whatever without having to worry about stepping on page or post permalinks.
To do this, I ended up doing the following:
This allowed me to:
Pull up a list of pages that use the page template using WP_Query
Add special processing by hooking into add_meta_boxes to store my custom data
Add my custom template to those displayed by filtering page_attributes_dropdown_pages_args, theme_page_templates, wp_insert_post_data, and template_include see this post on adding page templates to a plugin
The down sides
Of course, while this does not stomp on page or post links, it does have a couple of obvious downsides.
No archive
You won’t have an archive (if you want such), though that can be solved by creating another page template to draw an archive of all pages using your custom template.
Managed under Pages
You don’t get the nice left hand navigation in the admin that groups all of the post type together.
This could be partially solved by adding a filter to the page list (to let you filter by the page template being used), showing any page template used in a new column, etc.
That being said, I wanted something that would not cause users to wonder why they created a new custom page and found that they could either no longer reach normal pages or the new custom page caused an existing page on their site to disappear.
I know it isn’t a real solution, but it is an alternative that worked great for my needs.