How to Fix Custom Post Type 404 Errors in WordPress:-With releasing of WordPress 3.0 version, ability to add custom post types to WP themes evolved. But now the custom post type has been popular and used with almost every WP theme. Anyone whom has worked on custom post types has surely encountered dreadful 404 not found error when trying to make use of a post from post type from one to another point. So there is always a fix to fixing these kind of errors.
In this post we have listed down common issues with custom post types and why they receiving these errors.
- Check Permalinks Settings:- This is one of the main reason, people getting these errors on their custom post types. I have seen various fixes out like as flushing rewrite rules but personally recommend following simply fix:
Solution:-
- Set custom permalink structure (eg: %postname%)
- Click on save
- See if the post returns 404 error page
- If they do, navigate back and change permalink structure to default
- Tap on save
- Now set custom permalinks again and save.
Now, if permissions are not setup correctly on some servers this may not work for you and you have to update your .htaccess file manually. In .htaccess file make sure the file contains the same code as mentioned below.
# 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
How to Fix Custom Post Type 404 Errors in WordPress
- Check for slug Conflict:- Another thing that may create 404 errors is you have main webpage to show custom post type posts and it has same slug as actual post type singular slug. For instance- if you have a post name “portfolio” and also have a portfolio page both have slug portfolio. This shows conflicts causing 404 errors on single post type posts. That’s why you often find that portfolio post type make use of slug “projects” or “portfolio-item” for singular slug.
Solution:-
- Change the page name so it’s different then custom post type.
- Change custom post type slug which is done by altering rewrite parameter when registering custom post types.
- Auto Flush Rewrite Rules:-
Another issue which cause 404 errors is a new post type is registered you have to flush rewrite rules in WordPress. This can be done through Settings-> Permalinks and then save button.
If you are making changes on custom theme or plugin with registered post type, you may want to flush automatically the rewrite rules for end users when they activate particular theme or plugin to prevent 404 errors. Below is the code you can make use of:
// 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();
}