Using WordPress ‘post_author_meta_box()’ PHP function

The post_author_meta_box WordPress PHP function displays a form field with a list of authors.

Usage

post_author_meta_box( $post );

Custom example:

$post = get_post( 42 ); // Get a specific post with ID 42
post_author_meta_box( $post ); // Display the author meta box for that post

Parameters

  • $post (WP_Post): Required. The current post object.

More information

See WordPress Developer Resources: post_author_meta_box

Examples

Display author meta box in a custom meta box

This code adds a custom meta box to display the author meta box on a custom post type called “book”.

function add_book_author_meta_box() {
    add_meta_box(
        'book_author_meta_box',
        'Book Author',
        'display_book_author_meta_box',
        'book',
        'side',
        'default'
    );
}
add_action( 'add_meta_boxes', 'add_book_author_meta_box' );

function display_book_author_meta_box( $post ) { post_author_meta_box( $post ); }

Change the author of a post

In this example, we change the author of a post with ID 99 to the user with ID 3.

$post_id = 99;
$user_id = 3;
$updated_post = array(
    'ID' => $post_id,
    'post_author' => $user_id,
);
wp_update_post( $updated_post );

Display author meta box for all post types

This code adds the author meta box to all post types in the admin area.

function add_author_meta_box_to_all_post_types() {
    $post_types = get_post_types( array( 'public' => true ), 'names' );
    foreach ( $post_types as $post_type ) {
        add_meta_box( 'author', 'Author', 'post_author_meta_box', $post_type, 'side', 'default' );
    }
}
add_action( 'add_meta_boxes', 'add_author_meta_box_to_all_post_types' );

Display author meta box on a custom plugin settings page

In this example, we display the author meta box on a custom plugin settings page.

function my_plugin_settings_page() {
    add_options_page(
        'My Plugin Settings',
        'My Plugin',
        'manage_options',
        'my-plugin-settings',
        'my_plugin_settings_page_content'
    );
}
add_action( 'admin_menu', 'my_plugin_settings_page' );

function my_plugin_settings_page_content() { $post = get_post( 1 ); // Get the post with ID 1 echo '<h2>My Plugin Settings</h2>'; echo '<div class="author-meta-box">'; post_author_meta_box( $post ); echo '</div>'; }

Display author meta box on a custom frontend form

This code displays the author meta box on a custom frontend form.

function display_frontend_author_meta_box() {
    $post = get_post( 1 ); // Get the post with ID 1
    echo '<form>';
    echo '<div class="author-meta-box">';
    post_author_meta_box( $post );
    echo '</div>';
    echo '<button type="submit">Submit</button>';
    echo '</form>';
}
add_shortcode( 'frontend_author_meta_box', 'display_frontend_author_meta_box' );

To display the form, use the shortcode `[frontend_author_meta_box]