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:
- You can change the page name so it’s different then the custom post type
- 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!