How to Force Default Author for New WordPress Posts

The following WordPress PHP function shows how to force a user to be the default author for new WordPress posts.

This can be helpful if you have multiple users, but want posts to be published against a different user.

If you’re not sure where to place this code I highly recommend you read How to create a WordPress plugin for your custom functions.

Set new and updated posts to use specific author

// Set the default author for new posts
function itsg_force_post_author( $data, $postarr ) {
$author_id = 1; // Replace 1 with the ID of the user you want to force as the author.
if (empty($postarr['ID'])) {
$data['post_author'] = $author_id;
}
return $data;
}
add_filter( 'wp_insert_post_data', 'itsg_force_post_author', 10, 2 );

// Set the revision author to the same author as the post
function itsg_set_revision_author($post_has_changed, $last_revision, $post) {
    global $wpdb;
    $wpdb->update(
        $wpdb->posts,
        array('post_author' => $post->post_author),
        array('ID' => $last_revision->ID),
        array('%d'),
        array('%d')
    );
 
    return $post_has_changed;
}
add_filter('wp_save_post_revision_check_for_changes', 'itsg_set_revision_author', 10, 3);

The first function hooks into the wp_insert_post_data filter, which is called when a new post is inserted into the database.

It takes two arguments:

  • $data, which is an array of data about the post being inserted, and
  • $postarr, which is an array of data about the post being inserted and any other optional arguments that were passed.

The function sets the $author_id variable to the ID of the user you want to force as the author.

You’ll need to replace 1 with the ID of the user you want to use.

It then sets the post_author value in the $data array to the $author_id value.

Finally, it returns the modified $data array, with the updated value that forces the post to be authored by that user.

Does this work for custom post types?

Yes – it does work for custom post types!

If you want to control how it works with custom post types, you can use the $postarr[‘post_type’] parameter.

function itsg_force_post_author_cpt( $data, $postarr ) {
$author_id = 1; // Replace 1 with the ID of the user you want to force as the author.
if ( $postarr['post_type'] == 'my_custom_post_type' ) {
$data['post_author'] = $author_id;
}
return $data;
}
add_filter( 'wp_insert_post_data', 'itsg_force_post_author_cpt', 10, 2 );

Frequently Asked Questions

Can I use a different author ID instead of 33?

Yes, you can replace the number ’33’ in the code with any valid author ID you’d like to set as the default author for new posts.

Will this custom function affect my existing posts?

No, the custom function only sets the default author for new posts. The existing posts will remain unchanged when edited.

Can I use this function to set the default author for custom post types?

Yes, the custom function works for any post type, including custom post types. The default author will be set for all new posts, regardless of the post type.

Can I add this code to a custom plugin instead of the theme’s functions.php file?

Absolutely! You can create a custom plugin and add the code snippets to the plugin’s main file. This approach ensures that your customizations are preserved when updating or changing your theme.

Can I set multiple default authors?

The custom function provided in this guide sets a single default author for all new posts. However, you can modify the code or create additional functions to assign different default authors for specific post types or based on other conditions.

How do I find my author ID?

To find your author ID in WordPress, follow these simple steps:

  1. Log in to your WordPress dashboard.
  2. Go to the “Users” menu on the left sidebar.
  3. Click on “All Users” to display a list of all users on your website.
  4. Locate the user whose author ID you want to find and hover your mouse over their username.
  5. Look at the URL displayed in the bottom-left corner of your browser or click on the “Edit” link below the username.
  6. The author ID can be found in the URL as a numeric value after “user_id=”

For example, if the URL is https://yourwebsite.com/wp-admin/user-edit.php?user_id=1&wp_http_referer=%2Fwp-admin%2Fusers.php, the author ID is ‘1’.