WordPress is by far one of the most popular content management systems (CMS) in the world. Its flexibility and ease of use make it an ideal choice for both beginners and experienced web developers. However, despite the wide range of features that WordPress offers out of the box, it does not have a native function to set a default category for new posts. Fortunately, various tools and plugins come to the rescue. Here are some ways to set a default category for new posts in WordPress.
1. Editing the functions.php file
The most direct way to set a default category for new posts is to edit the functions.php file in our WordPress theme. Here are the steps you need to take:
- Go to the WordPress administration panel and select the "Appearance" tab and then "Theme Editor".
- Find and open the functions.php file.
- At the end of the file, paste the following code:
add_action( 'save_post', 'set_default_category' ); function set_default_category( $post_id ) { // Set the ID number of your default category $default_category_id = '1'; $categories = wp_get_post_categories( $post_id ); if ( empty( $categories ) ) { wp_set_object_terms( $post_id, $default_category_id, 'category' ); } }
Remember to replace '1' with the ID of the default category you want to set.
Attention! Editing theme files carries risks, and it's always a good idea to back up your site before doing so.
2. “Default Category” plugin
Another, safer solution is to use one of the plugins available in the official WordPress repository. One of them is “Default Category”.
- Install and activate the “Default Category” plugin in your WordPress admin panel.
- After activating the plugin, go to the "Settings" tab and select "Default Category".
- In the newly opened window, you can choose a default category for new posts.
3. “WP Category Permalink” plugin
One more tool is the “WP Category Permalink” plugin. This plugin allows you to choose a default category for your permalink, which can be useful for SEO.
- Install and activate the “WP Category Permalink” plugin.
- Once activated, go to "Settings" and select "Permalinks".
- In the “Optional” section you will find a new “Default category” field. Select the category you want to set as default.
In summary, WordPress does not offer a native solution for setting a default category for new posts, but we can achieve this in many other ways. Whether you're an experienced developer or a beginner, you'll find a way that works for you.