1

Im trying to get the user meta data from a post but only getting the one user:

$args = array(
    'numberposts' => 10,
    'offset' => 0,
    'category' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true
  );

  $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

foreach ($recent_posts as $post) {
 $user_id = get_the_author_meta('ID', true) // is this correct
 // Is there a function that I need to pass the post ID ($post["ID"])?
 var_dump($user_id);
}

When another user makes a post, I cant get their meta data. How, please?

1

You can pass the post's author as an argument to get_the_author_meta:

get_the_author_meta('ID', $post->post_author);

The second argument is the user's ID. This is stored in the post object in your loop, which you can access it by using $post->post_author.

Reason

The reason behind the current code of yours that isn't working is this piece of code that is included in the get_the_author_meta():

if ( ! $user_id ) {
    global $authordata;
    $user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
} else {
    $authordata = get_userdata( $user_id );
}

If you set the second argument to true ( which is the $user_id ), it will trigger the else, and by triggering the else you are passing a true to the get_userdata(), which will obviously won't work.

Take a look at this page of code reference for more details.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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