Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme.Learn More

How to Fix Custom Post Type 404 Errors In WordPress

January 10, 2018

With the release of WordPress 3.0 came the ability to add “Custom Post Types” to your WordPress themes which is a very valuable tool and I’ve used in in many WordPress themes I’ve created. By now Custom Post types have become extremely popular and used in almost every WordPress theme out there. But anyone whom has worked with custom post types has probably encountered the dreadful 404 Not Found Error when trying to access a post from the post type at one point or another. Luckily there is almost always a simple fix to fixing these errors.

Below I have listed some of the more common issues people have with Custom Post Types and why they may be receiving these errors. Hopefully they will help at least a few people out there.

1. Check Your Permalink Settings

This is probably one of the most common reasons people are getting 404 errors on their custom post types and I have dealt with it many times. I have seen many fixes out there such as flushing the rewrite rules (which I do not recommend) but personally I have had best luck with the following simply fix:

Solution:

  • Set your custom permalink structure (such as %postname%)
  • Click Save
  • See if your single custom post pages return 404 error pages
  • If they do, go back and change permalinks back to default and save
  • Now try setting the custom permalink again and save

Going back and forth has normally helped fix my errors and I’ve had a lot of success with this method.

Now, on some servers if your permissions aren’t set correctly this may not work and you may have to update your .htaccess file manually. To do this you will have to log into your site via FTP or SFTP and browser to your root WordPress directory (same place where your wp-config.php file and wp-content folder is located). Here you should find a file named .htaccess which you can modify (if you don’t see it make sure your FTP program has the option to display hidden files enabled and if there simply isn’t one then create one). Now make sure the file contains the core WordPress code as mentioned in the WordPress docs, which looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Important: If you are modifying an existing .htaccess file make sure to back up the file on your computer first just incase you mess anything up.

2. Check for slug conflicts (having a page with the same slug as your post type)

Another thing that may cause a 404 error is that you have a main page to display your post type post and it has the same slug as your actual post type singular slug. For example if you have a post type named “portfolio” and you also have a main “Portfolio” page both with the slug “portfolio” (in other words to access a portfolio post you would go to site.com/portfolio/sample-post) this creates a conflict causing 404 errors on your singular post type posts. That’s why you often find that the portfolio post type uses the slug “projects” or “portfolio-item” for the singular slug.

Solution:

  1. You can change the page name so it’s different then the custom post type
  2. You can change your custom post type slug which is done by altering the rewrite parameter when registering your custom post type

3. Auto Flush Rewrite Rules (for developers)

Another cause of 404 errors is whenever a new post type is registered you have to “flush” your rewrite rules in WordPress. This can be done by going to Settings > Permalinks and clicking the save button (mentioned in the first section of this post).

If you are working on a custom theme or plugin with registered post types you may want to consider automatically flushing the rewrite rules for your end user when they activate your theme or plugin to prevent any 404 errors. Below is an example of the code you can use:

// Code for themes
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

// Code for plugins
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );
function myplugin_flush_rewrites() {
	// call your CPT registration function here (it should also be hooked into 'init')
	myplugin_custom_post_types_registration();
	flush_rewrite_rules();
}

Have Another Error or Solution?

If you are having another error or you have a better solution then mine please comment below and let me know. Not only will it help me out but it will probably help other people looking for a fix to their problem. Thanks!

post-type-404-error
Article by AJ Clarke WPExplorer Staff
Published on: April 8, 2011
Last updated on: January 10, 2018
Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

25 Comments

  1. Andy says:

    I’m not sure if this might be a pagination issue or not; but recently my site stopped accessing pages 2,3,4… and brought up a 404 error instead. It’s using your neptune theme, but recently the ISP changed the mySQL database details (I’ve fixed these I think…); so now I’m not sure what else to try! (I’m a real newbie)

    By the way, I’d happily purchase a wpexplorer theme if it looked like neptune (its a lovely, simple design) and did the following: instead of key words filtering out only a page at time, could they search the whole portfolio and then filter?

    • AJ Clarke | WPExplorer says:

      You tried saving your permalink settings? What about changing the slug of the portfolio page?

      Also maybe try de-activating any plugins.

      I might consider a premium version…The filter as you mention though would be pretty difficult, it would need to be done via AJAX and I’m my opinion would be a bit to server “heavy”.

  2. bucurblog says:

    God bless you, you’ve made ​​my day so beautiful.After searching many solutions for my theme that I’m working I did after reading this article to do the custom post type pagination products page.I started jumping around the house with joy if you had se me :)))

    • AJ Clarke | WPExplorer says:

      That’s awesome! You’re welcome 😉 This is an old post, glad it’s still helping people out.

  3. Archimidis says:

    You saved Me!Thanks a lot!

  4. James says:

    Had the issue with the post type & page containing the post types having the same name – your site was the only one to suggest this might be the problem, thanks!

  5. Pankaj Chauhan says:

    I was getting the same error on my blog. It was not happening with all the posts but with one specific post. After, few hours I noticed that the category name was causing the issue. so I deleted the category and recreated it with a different slug. It worked.

    Luckily, in that category, I had no posts otherwise…

  6. Retail Computer Solutions says:

    Just been working on custom post types and the first solution resolved the 404 error. Thanks for the tip.

  7. Clarke Design says:

    I’ve got a 404 problem with my custom posts type.

    Everything works perfectly in Firefox, but in IE and Chrome I get 404 errors.

    I’ve tried your fix, I’ve tried every combination of permalinks, but still IE and Chrome will not show the custom posts type – shows 404

    Driving me nuts

    • AJ Clarke | WPExplorer says:

      Maybe try clearing your browser and website cache? There is no reason why a 404 would be on one browser and not the other, that is a server related error and not browser-dependent.

  8. june says:

    I’m having a different issue with custom post types and pagination. I should have about 12 pages of posts (12 posts on a page). I get to page 8 and it acts like there are no additional pages. My previous posts button doesn’t appear and if I try to use the url to skip to another page (ie, page 9), I get a 404 error. If you are in the admin area, there are obviously more than 96 posts.

    The pagination code I’m plugging into the query looks like this:
    $args = array(
    ‘post_type’ => ‘cptname’,
    ‘posts_per_page’ => ’12’,
    ‘paged’ => get_query_var( ‘paged’ ));

    Any suggestions would be great! Thanks!

    • AJ Clarke says:

      Sounds like the default WordPress query is taking over, which is a common issue when using WP_Query. This is an example of how I would set it up:

      https://gist.github.com/wpexplorer/8657325

      • june says:

        Thanks for the suggestion! I’m thinking you’re right in that its conflicting with another query… but your solution didn’t change anything. With your code I get the same problem and still hit a dead end at page 8

        If I look at an individual page, the prev/next buttons there continue all the way to the oldest post. It just seems to stop at page 8 at this archive page level.

        Is there any way to tell which query is taking over? Is it possible that its not necessarily the default query causing the conflict?

        • AJ Clarke says:

          Are you adding your custom loop in a standard page? And are you sure the slug for this page isn’t taken by lets say the post type archive?

  9. Kieran McCarthy says:

    Thank you AJ your method “404 Error When Setting Custom Permalinks” worked for me.

  10. Viktor Hanacek says:

    Thank you, AJ 🙂

  11. hiteshbabariya275 says:

    An error occurred in the application and your page could not be served. Please try again in a few moments.

    If you are the application owner, check your logs for details.

    This error comes when I use flush_rewrite_rules() function in my custom post type creation

  12. Chuck says:

    I know this is a REALLY old post but note that calling flush_rewrite_rules() on every page will cause A LOT of database load. If this method is called in the functions.php or any other theme file, it will load on EVERY page load. This will cause your database to work very hard and will slow down page loads. The better option would be to just refresh the rewrite rules by going to the permalink settings and hitting save whenever you feel necessary (custom post type, new slug, etc.

    • AJ Clarke says:

      Agreed Chuck, flush_rewrite_rules should run ONLY on theme-switch, if you look at my code I have it set to run during the after_switch_theme filter. So it’s not calling on every page load, but only when you switch themes or plugins. That way if your theme has custom post types when a user switches to that theme it will flush the rewrite rules 1x and hopefully prevent 404 errors.

  13. Harsh Agrawal says:

    Hey AJ
    I’m on Nginx server, and there is no .htacecss file there. So permalink not working with custom post type. Any solution for the same?

  14. Rami says:

    Hi! I tried avaery single solution I found on the web but did not work, the solution was for sure one of what you mensioned up there, but non of them worked till I CLEANED THE BROWSER’S CACHE! the website was still redirecting me to the error page, so pelase tell that they MUST clean the cache before trying any of your solutions!

    Thank you.

    • AJ Clarke says:

      404 errors shouldn’t be getting cached, very weird…Thanks for commenting, maybe it will help someone else out in the future 😉

  15. Maciej says:

    Thank you! Switching back and forth permalinks helped me! The 404 on my custom posts was drving me insane. Thank you once again! Cheers!

  16. sammyskills says:

    Thanks man. This was very helpful.

Leave a Reply

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