This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
default_category_post_types filter
Filters post types (in addition to ‘post’) that require a default category.
To use default_category_post_types filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function modify_default_category_post_types_defaults which takes 1 parameter and we registered using add_filter. The first parameter default_category_post_types is the name of the hook, the second parameter modify_default_category_post_types_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook so you can use remove_filter to remove default_category_post_types filter.
Parameters
- $post_types : (string[]) An array of post type names. Default empty array.
Below the 1 parameter is required to use this hook.
Live Example
apply_filters('default_category_post_types', string[] $post_types)
Below is an example of how you can use this hook.
Example 1: Modifying Default Category Post Types
In this example, we modify the default category post types.
function weplugins_modify_default_category_post_types_defaults($post_types) { // Update the $post_types variable according to your website requirements and return this variable. // You can modify the $post_types variable conditionally too if you want. return $post_types; } // Add the filter add_filter("default_category_post_types", "weplugins_modify_default_category_post_types_defaults", 10, 1);
Example 2: Adding Custom Post Type to Default Category
In this example, we add a custom post type to the default category.
function weplugins_add_custom_post_type($post_types) { $post_types[] = 'custom_post_type'; return $post_types; } // Add the filter add_filter("default_category_post_types", "weplugins_add_custom_post_type", 10, 1);
Example 3: Removing a Hook Callback
To remove a hook callback, use the example below.
remove_filter("default_category_post_types", "weplugins_modify_default_category_post_types_defaults", 10, 1);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you’re having any trouble using this hook or need customization, please contact us and we’d be happy to assist you.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.