To change the default category for posts in WordPress, you can use the default_category
filter. This filter allows you to modify the default category ID that is assigned to a post when no category is selected.
Here’s an example code snippet that demonstrates how to change the default category for posts:
function wpsnippets_change_default_category($default_category_id) {
// Set the desired category ID as the default
$default_category_id = 5; // Replace 5 with the ID of your desired category
return $default_category_id;
}
add_filter('default_category', 'wpsnippets_change_default_category');
In this example, we define a custom function wpsnippets_change_default_category
that takes the default category ID as a parameter. Inside the function, we set the desired category ID (in this case, 5) as the new default category. Finally, we use the add_filter
function to hook our custom function to the default_category
filter.
You can modify the code snippet by replacing 5
with the ID of your desired category. Once you have added this code to your theme’s functions.php
file or a custom plugin, the specified category will be set as the default for new posts.
This code snippet can be useful when you want to ensure that all new posts are automatically assigned to a specific category. For example, if you have a blog where most of your posts belong to a particular category, you can use this code to save time by automatically assigning that category to new posts.
Examples
Example 1: Change the default category for posts using the default_category
filter
This example demonstrates how to change the default category for posts by modifying the default_category
filter. By default, WordPress assigns the “Uncategorized” category to new posts. However, you can use this filter to change the default category to a different one.
function wpsnippets_change_default_category( $default_category ) {
// Change the default category to "News"
$default_category = 'news';
return $default_category;
}
add_filter( 'default_category', 'wpsnippets_change_default_category' );
In this code example, we define a custom function wpsnippets_change_default_category
that takes the default category as a parameter and returns the modified default category. We then use the add_filter
function to hook our custom function to the default_category
filter. Inside the function, we simply assign the desired default category (in this case, “news”) to the $default_category
variable and return it.
Example 2: Change the default category for posts programmatically
This example demonstrates how to change the default category for posts programmatically using the wp_insert_post_data
filter. Instead of relying on the default_category
filter, this approach allows you to dynamically set the default category based on certain conditions or logic.
function wpsnippets_change_default_category( $data, $postarr ) {
// Check if the post is a new post
if ( $data['post_status'] === 'auto-draft' ) {
// Change the default category to "Tutorials"
$data['post_category'] = array( get_cat_ID( 'tutorials' ) );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'wpsnippets_change_default_category', 10, 2 );
In this code example, we define a custom function wpsnippets_change_default_category
that takes two parameters: $data
(an array of post data) and $postarr
(an array of raw post data). We then use the add_filter
function to hook our custom function to the wp_insert_post_data
filter. Inside the function, we check if the post is a new post (by checking the post_status
value) and if so, we modify the post_category
value to set the desired default category (in this case, “Tutorials”).
Example 3: Change the default category for posts using a plugin
This example demonstrates how to change the default category for posts using a custom plugin. By creating a plugin, you can easily modify the default category without directly editing the theme’s functions.php file.
/**
* Plugin Name: Change Default Category
* Description: Changes the default category for new posts.
*/
function wpsnippets_change_default_category( $default_category ) {
// Change the default category to "Events"
$default_category = 'events';
return $default_category;
}
add_filter( 'default_category', 'wpsnippets_change_default_category' );
In this code example, we create a custom plugin by creating a new PHP file with the provided code and placing it in the wp-content/plugins/
directory. The plugin’s header contains the name and description. The rest of the code is similar to Example 1, where we define the wpsnippets_change_default_category
function and hook it to the default_category
filter using add_filter
. The default category is changed to “Events” in this example.