Default Category per Author
Pre-selects a category for new posts based on who writes them, for example "Recipes" for your food writer, so posts land in the right section without anyone remembering to tick it.
Opens WPIDE on
Code
// User login => category slug.
$wpide_author_categories = array(
'admin' => 'harness-cat',
// 'jane' => 'recipes',
);
add_action( 'wp_insert_post', function ( $post_id, $post, $update ) use ( $wpide_author_categories ) {
if ( $update || 'post' !== $post->post_type || 'auto-draft' !== $post->post_status ) {
return;
}
$user = get_userdata( (int) $post->post_author );
if ( ! $user || empty( $wpide_author_categories[ $user->user_login ] ) ) {
return;
}
$term = get_category_by_slug( $wpide_author_categories[ $user->user_login ] );
if ( $term ) {
wp_set_post_categories( $post_id, array( (int) $term->term_id ) );
}
}, 10, 3 );
Notes
Map user logins to category slugs in $wpide_author_categories; writers can still change it before publishing.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.