Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
share|improve this question

2 Answers 2

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;
}
share|improve this answer
    
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
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.

share|improve this answer
    
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  t f Oct 24 '13 at 6:21
    
It's also your own question. You can edit it to clarify. –  infamouse Jun 17 at 1:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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