For almost two hours I was pulling whatever hair is left out of my head because I was registering a custom post type, and its posts, archives and categories would generate 404 errors using the %category%/ %postname% permalink structure. I knew that my post type was fine so there was just something that kept messing up my work. In my case it was another plugin that was causing the errors but I figured I should put a semi-compete troubleshooting list together so you can fix your Custom Post Type permalink errors.
First thing first, let’s register a Custom Post Type called book and give it a pretty slug “book”.
This means that when you add a post under this type, your permalinks would be:
http://yoursite.com/book/your-post-name
and the archive for this post type would be:
http://yoursite.com/book/
What happens here is that WordPress tries to rewrite your slug to “book” and this is where the problems begin.
Things to check:
- Before you do anything else, first go to setting/permalinks and re-save the permalink structure. This will hard flush the rewrite rules inside your htacess and most of the time will fix your problem. If you’ve re-saved your permalink structure and the problem still exists go the next step. In your functions.php right after:register_post_type( ‘book’, $args ); // your slug and arguments will be different obviouslyAdd
flush_rewrite_rules();
Save, upload the file, and test again. If the permalink starts working, it means that your post type is registered correctly and something else is messing it up. Go ahead and remove the line you just added as although it seems that it fixed the problem, it’s just a band aid and not a good one either. What this function does is that it constantly flushes the rewrite rule with every request so you definitely don’t want it in there. Now that we know the post type registration was OK, Proceed to the next step.
- Make sure you don’t have a page, category, tag, post, tag base, or category base with the same slug. This means if you have a page called Book with a slug “book”, WordPress will go crazy and rightfully so. The fastest way to find out is to dump your database and open it with a text editor like Notepad++ and do a search for book. If you see a duplicate somewhere, rename either your custom post type slug or fix the other page. If this doesn’t fix the problem, proceed to the next step.
- This is where the real troubleshooting begins. If you have a custom theme, switch to one of the default themes that ship with WordPress like the Twenty Eleven or Twenty Twelve. Re-save the permalinks structure again and test. If this doesn’t fix it, proceed to the next step.
- Disable all the plugins and resave the permalinks again. This will definitely fix your problem so test and see. Now that we know that your theme is fine, your custom post type is fine, and permalinks have been flushed, there is only one possibility. It’s one of your plugins that’s calling the flush function and is doing wrong. Start activating your plugins one at a time, resaving the permalinks after each activation, and test your custom post type. This will pin point your problem source and then you either have the option to ditch the faulty plugin, report it to its developer, or try to fix it yourself.
Hope this comes handy the next time you’re pulling your hair out of your skull. If you have questions or comments, leave them below.
Hi! First of all, thank you for this post!
So, my problem is the error 404 in term children: example: site.com/custom-term/custom-term-children, as well terms in type ‘book’, for example.
How can I fixed this error?
Thanx!
I love it when a 5-year old post saves my ass. Thanks!
Thank you for this! I forgot to flush my permalink settings (that is, save them). This article reminded me to do this. 😉
Hello, am having an issue with the page displaying.
register_post_type( ‘books’,$arg);
am using a single.books.php page to read the content from the register post, in my sample book.
when am viewing the page, for exemple ../books/my-book/ its works.
However, when am going on : /books/ am having a 404 error. i think i must use a slug/taxomie-NAME.php, but don’t know which one. can you help ?
I was using CPT UI plugin and geting the same error. Nothing worked until I found an option in the Edit post type called Has Archive which was selected false by default. Made it True and bingo 🙂
Thanks a lot for this post.
Settings/Permalink -> Save helped me! Thanks!
Thanks a lot ! I would have looked for hours without reading your article. Have a great day!
Settings/Permalink -> Save helped me! Thanks!
Permalinks save the day, again. Thanks!
Thanks. Resaving permalink fixed it.
flush_rewrite_rules();
This have fixed my problems 🙂 TY!!!
good post
Just to share. In my case it was the parameter
‘query_var’ => false
It should be true
This article did the trick for me. Thanks!
Thank very much!
I’ve been searching for 2 hours
I’ve find you’re precious article and have a solution
You make my day !!
Many Thanks
Mwah!! You are an absolute star.
Had issues for hours and hours and was so stressed then found your blog, re saved my perms links and I’m a happy little Vegemite once again yaye!
Thank you thank you thank you!
just to share… this didn’t work for me, finally, my register_post_type arguments array had “has_archive” => false which caused the permalink to fail (strange!)
Hi Chris ,
Thanks for this tutorial. I have another issue though and I spent hours researching with no success. Perhaps you can help?
My custom posts have normal taxonomies assigned to them (just the regular categories and tags) but when I press on one – I get a 404 page. (Only when I press on blog categories & tags I get to see posts) How do I get the archive / category page to display custom posts too?
Thank you!
One remark: I could not find “register_post_type( ‘book’, $args ); // your slug and arguments will be different obviously” in the functions.php file under wp-includes
Hello,
It was very interesting what you have suggested here. Unfortunately, in my case it did not help. I went along all the way through the steps you have detailed but could not fix the problem.
I have a Hungarian website and if I change the Permalink settings form /%postname%/ to “Day and name” or “Month and name” for example, they are working fine. But “Post name” is not working.
I find it very strange, could you tell me why isn’t it working?
Just wanted to say thank you resetting the permalinks did it for me and this article is great for just troubleshooting errors in general thanks for the advice and saving my night! Gave it a facebook share as well.
THANKS, simply flushing the permalink settings worked for me.
Thank you very much, indeed.
flush_rewrite_rules() worked.
However, please note in the docs that it says this is an expensive operation, so the proper place to put it is in the activation or deactivation hook for the plugin — http://codex.wordpress.org/Function_Reference/flush_rewrite_rules
…and remember the ‘has_archive’ => true argument in register_post…
you are a gosh darn life saver! Looked all over this internet and this was the issue all along! Thanks everyone!! you are all great!
“The flush_rewrite_rules();” snippet resolved this issue for me.
Thank you so much!
Thanks man! Re-saving the permalink structure did it for me 😀
Thank you! You’ve been really helpful, I was about to rewrite my custom post types code to try and fix it.
Thank you for posting this. Just spent close to 3 hours trying to work out why my custom post type was getting a 404 error.
Hi Chris, Great!!! its worked out for me. Very useful and valid post.
Hi Chris, thanks a lot. I have searching the failure for hours. Because of your article I know now:My Theme doesn´t work right.
Can you give me any hints about what can be wrong in my theme?
Hi,
i was using custom permalinks %postname%.html and somhing was causing a redirect when vi visit post page it was redirecting to Postname.html/ .
flush_rewrite_rules(); in function.php fixed the issue .. Thanks a lot.
You can’t use .html in the permalink structure. Remove the .html and you should be fine. It should only read %postname%
Hi there, I’ve got the same problem. Plugin generate the right url but I only see 404 error page. Just tried flushing etc.
no
have done that. have also tried flush rewrite rules in default theme.have checked wp-posts tables in database to make sure there were no posts or pages with same name as custom post type. could it be a sever error?
You don’t have a caching plugin on your site, do you?
yes map-cap requirements are met. i have two other wordpress installations that are using all the same plugins. both of these work perfectly. i have run out of ideas was hoping i could find some solution.
Have you tried switching your theme for the default theme? Do that and re-save the permalinks and see what happens.
placed flush_rewrite_rules(); in functions.php of my theme still have error.
i did find that flush_rewrite_rules(); is included in the custom post type UI plugins php file.
Why not registering your CPT without a plugin? Also, the map-cap plugin has specific requirements, are these met?
http://wordpress.org/plugins/map-cap/faq/
hello
i have been trying to sort this out for about a week.
i am using custom post type UI plugin to register post types. Add Custom Post Types Archive to
Nav Menus plugin also map-cap plugin.
whenever i try to preview changes after post is published i get a page not found error.
I cannot view or edit custom post types with these plugins deactivated however i deactivated all others
I have note tried flush_rewrite_rules(); because i am unsure of where to place the code because i am using the plugin to register custom post types.
any ideas would be helpful. i tried everything else in your list with the above exceptions.
thanks
One of your plugins is causing the problem. put the flush_rewrite_rules(); in the function.php of your them and try again.
tnks
I just wanted to add this in the comments. I just spend 4 hours last night trying to get this permalink issue fixed. I tried everything I could find on the web. I am using Centos 6 on my vps. I found another area in the httpd.conf that reads:
#AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride none
Here is a quick article I wrote up so other people trying to fix would not waste so much time!
http://www.mobiledataup.com/fix-wordpress-permalink-404-error-with-centos-6/