Using WordPress ‘default_category_post_types’ PHP filter

The default_category_post_types WordPress PHP filter allows you to modify the post types that require a default category, in addition to the ‘post’ type.

Usage

add_filter('default_category_post_types', 'my_custom_default_category_post_types');
function my_custom_default_category_post_types($post_types) {
    // your custom code here
    return $post_types;
}

Parameters

  • $post_types (string[]): An array of post type names. Default empty array.

More information

See WordPress Developer Resources: default_category_post_types

Examples

Add a custom post type to require a default category

In this example, we add a custom post type called ‘product’ to require a default category.

add_filter('default_category_post_types', 'add_product_post_type');
function add_product_post_type($post_types) {
    $post_types[] = 'product';
    return $post_types;
}

Remove the ‘post’ post type from requiring a default category

In this example, we remove the ‘post’ post type from requiring a default category.

add_filter('default_category_post_types', 'remove_post_post_type');
function remove_post_post_type($post_types) {
    $key = array_search('post', $post_types);
    if ($key !== false) {
        unset($post_types[$key]);
    }
    return $post_types;
}

Replace default post types with a custom post type

In this example, we replace the default post types with a custom post type called ‘news’.

add_filter('default_category_post_types', 'replace_with_news_post_type');
function replace_with_news_post_type($post_types) {
    $post_types = ['news'];
    return $post_types;
}

Add multiple custom post types

In this example, we add multiple custom post types (‘recipe’ and ‘event’) to require a default category.

add_filter('default_category_post_types', 'add_multiple_post_types');
function add_multiple_post_types($post_types) {
    $post_types[] = 'recipe';
    $post_types[] = 'event';
    return $post_types;
}

Clear all post types

In this example, we clear all post types from requiring a default category.

add_filter('default_category_post_types', 'clear_all_post_types');
function clear_all_post_types($post_types) {
    return [];
}