In WordPress, custom post types are a powerful way to extend the functionality of your site. However, there are situations where you might want to disable or deregister a custom post type, especially if it comes with a theme or plugin you’re using, but isn’t necessary for your site. This can be achieved by deregistering the custom post type in your child theme, allowing you to retain customizations even when the parent theme or plugin is updated.
This article will guide you through the process of deregistering or disabling a custom post type in your child theme.
Why Would You Want to Disable a Custom Post Type?
Custom post types can be useful for organizing content, but they may not always be relevant for every site. Here are some common reasons you might want to disable a custom post type:
- Irrelevant Content Types: If a custom post type (such as “Portfolio” or “Testimonials”) is not used on your site, it just adds clutter.
- Plugin Optimization: Some plugins register custom post types that are not needed for your specific use case. Disabling these can improve site performance.
- Avoid Conflicts: If you are using multiple plugins or themes that register similar custom post types, you may want to disable one to avoid conflicts.
Step-by-Step Guide to Deregister a Custom Post Type
To disable a custom post type in your child theme, you need to unregister the post type using the unregister_post_type() function. Follow these steps:
Step 1: Identify the Custom Post Type Slug
First, you need to know the slug of the custom post type you want to disable. The slug is a unique identifier used when registering the custom post type. You can find it by looking at the plugin or theme’s code that registers the post type or through the WordPress admin dashboard URL when editing the custom post type.
For example, if your URL is:
https://example.com/wp-admin/edit.php?post_type=portfolio
The custom post type slug is portfolio.
Step 2: Add Code to Your Child Theme’s functions.php
To deregister the custom post type, add the following code to your child theme’s functions.php file:
function my_custom_unregister_post_type() {
// Replace 'your_post_type' with the actual slug of the custom post type
unregister_post_type('your_post_type');
}
add_action('init', 'my_custom_unregister_post_type', 11);
Explanation of the Code:
unregister_post_type()is used to remove the custom post type.- The function is hooked to the
initaction, with a priority of11to ensure it runs after the custom post type has been registered (default priority is10).
Replace 'your_post_type' with the actual slug of the post type you wish to disable.
Step 3: Test Your Site
After adding the code, check your WordPress dashboard to confirm that the custom post type is no longer visible. The content will not be deleted but will be hidden from the WordPress admin. If you want to revert the change, simply remove the code snippet from your functions.php file.
Additional Considerations
- Deregistering Custom Post Types Registered by Plugins
- If the custom post type is registered by a plugin, it might be created after the
initaction. In such cases, you might need to use a different hook or a higher priority, such as:function my_custom_unregister_post_type() { unregister_post_type('your_post_type'); } add_action('wp_loaded', 'my_custom_unregister_post_type');
- If the custom post type is registered by a plugin, it might be created after the
- What Happens to Existing Content?
- When you deregister a custom post type, the existing content will not be deleted from the database. If you re-enable the post type in the future, the content will still be there.
- Alternative Approach: Using
remove_menu_page()- If you just want to hide the custom post type from the admin menu rather than completely deregistering it, you can use the following code:
function my_custom_remove_post_type_menu() { remove_menu_page('edit.php?post_type=your_post_type'); } add_action('admin_menu', 'my_custom_remove_post_type_menu');
- If you just want to hide the custom post type from the admin menu rather than completely deregistering it, you can use the following code:
Troubleshooting
- The Custom Post Type Is Still Visible: If the custom post type is still visible, make sure that the slug is correct and that the code is added in the child theme’s
functions.phpfile. You may need to increase the priority in theadd_action()function to ensure your code runs after the custom post type is registered. - White Screen of Death: This usually happens because of syntax errors in the
functions.phpfile. Make sure to double-check the code for any typos or syntax errors.
Conclusion
Deregistering or disabling a custom post type in your child theme is a simple way to declutter your WordPress dashboard and improve site performance. By using the unregister_post_type() function, you can easily control which post types are available without modifying core plugin or theme files, thus ensuring your customizations are preserved during updates.
Now you can take control of your WordPress site by selectively enabling or disabling custom post types as needed!