# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: categories, authors, editorial
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/content-default-category-per-author/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Default Category per Author" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
