I need to place in post edit dashboard metabox with post author e-mail (or other user meta fields). So it can be edited when admin reviews this post.

$meta_id = get_the_author_meta( 'user_email', $user_id );

$meta_box = array(
    'id' => 'my-meta-box',
    'title' => 'DANE FIRMY',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'E-mail box',
            'id' => 'mail',
            'type' => 'text',
            'std' => $meta_id
        )
    )
);

This code works when $user_id is an integer (when I manually put there for example 4) but i want to dynamically get current author id ( $user_id ).

get_the_author_meta('user_mail') should work without specifying $user_id (codex says that :)) but code is in functions.php and outside the loop so it doesn't work. I'm starting with Wordpress and PHP so I don't know what to do next.

Also tried this:

global $post;
$user_id=$post->post_author;

The easiest way would be using get_post_field():

$post_author_id = get_post_field( 'post_author', $post_id );

For more details on this issue: have a look at this StackOverflow answer.

You can use the following:

/**
 * Gets the author of the specified post. Can also be used inside the loop
 * to get the ID of the author of the current post, by not passing a post ID.
 * Outside the loop you must pass a post ID.
 *
 * @param int $post_id ID of post
 * @return int ID of post author
*/
function wpse119881_get_author( $post_id = 0 ){
     $post = get_post( $post_id );
     return $post->post_author;
}
  • Hmm it doesnt work for me - i think function must be hooked to one of filters but dont know which. – th3rion Oct 24 '13 at 6:22
  • Works for me... are you sure you're passing it a (valid) post ID? – Stephen Harris Oct 24 '13 at 13:04
  • But i want to display this meta field in edit screen for every post (not just for one) and post author can be different so $post_id must be loaded dynamically according to edit screen. – th3rion Oct 24 '13 at 13:17
  • Set $post_id dynamically. If using inside a metabox, your metabox callback will be passed the $post object. So you can use $post->ID (You can probably just use $post->post_author for that meta. – Stephen Harris Oct 24 '13 at 16:12
up vote 0 down vote accepted
add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
function myprefix_edit_form_after_title() {
    global $post;
    $author_id=$post->post_author;
    $authord = get_the_author_meta( 'user_email', $author_id);
    echo $authord;
}

With this function I was able to display post author e-mail in post edit screen. Still don't know how to make it work with custom meta field but I think Im closer now.

  • It's also your own question. You can edit it to clarify. – funwhilelost Jun 17 '14 at 1:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.