How to Fix WordPress Custom Post Type Permalink 404 Error

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”.

custom-post-type

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:

  1. 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 obviously

    Add

    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.

  2. 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.
  3. 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.
  4. 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.

42 Responses to How to Fix WordPress Custom Post Type Permalink 404 Error

  1. Jason says:

    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/

  2. aminfar69 says:

    tnks

  3. ucarman says:

    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

    • Chris Sorbi (Admin) says:

      One of your plugins is causing the problem. put the flush_rewrite_rules(); in the function.php of your them and try again.

  4. ucarman says:

    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.

  5. ucarman says:

    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.

    • Chris Sorbi (Admin) says:

      Have you tried switching your theme for the default theme? Do that and re-save the permalinks and see what happens.

  6. ucarman says:

    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?

  7. Hi there, I’ve got the same problem. Plugin generate the right url but I only see 404 error page. Just tried flushing etc.

  8. Heeren says:

    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.

    • Chris Sorbi (Admin) says:

      You can’t use .html in the permalink structure. Remove the .html and you should be fine. It should only read %postname%

  9. frank says:

    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?

  10. Gokula krishnan says:

    Hi Chris, Great!!! its worked out for me. Very useful and valid post.

  11. MPossDesign says:

    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.

  12. Andrés says:

    Thank you! You’ve been really helpful, I was about to rewrite my custom post types code to try and fix it.

  13. Arles says:

    Thanks man! Re-saving the permalink structure did it for me 😀

  14. “The flush_rewrite_rules();” snippet resolved this issue for me.

    Thank you so much!

  15. Jonathan says:

    …and remember the ‘has_archive’ => true argument in register_post…

    • Colin says:

      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!

  16. 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

  17. trashy says:

    THANKS, simply flushing the permalink settings worked for me.

  18. Todd says:

    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.

  19. Csaba says:

    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?

  20. Csaba says:

    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

  21. Milena says:

    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!

  22. Guy Labbé says:

    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!)

  23. Lara says:

    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!

  24. I’ve been searching for 2 hours
    I’ve find you’re precious article and have a solution
    You make my day !!

    Many Thanks

  25. Andru says:

    Thank very much!

  26. Roy Huggins says:

    This article did the trick for me. Thanks!

  27. Just to share. In my case it was the parameter

    ‘query_var’ => false

    It should be true

  28. subash says:

    good post

  29. Jesualdo says:

    flush_rewrite_rules();

    This have fixed my problems 🙂 TY!!!

  30. Michael says:

    Thanks. Resaving permalink fixed it.

  31. Permalinks save the day, again. Thanks!

  32. Joshua Davis says:

    Settings/Permalink -> Save helped me! Thanks!

  33. Stéphane says:

    Thanks a lot ! I would have looked for hours without reading your article. Have a great day!

  34. […] have tried disabling all plugins, basically I have tried everything on this website How to Fix 404 and anything I could find on here. Does anyone know of anything else that could be casuing […]

  35. Clyde Thomas says:

    Settings/Permalink -> Save helped me! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *


*