How can I display a specific user’s first published post?

I’m looking for a way to display a user’s first published post using their unique ID.

I have a function that calls a list of specific users using their IDs, and would like to display the date the user first published a post.

This is the code I put in, however, the cells return blank with no PHP errors in the log.

$useremail = $ticket['email'];
$user = get_user_by( 'email', $useremail );
$IDuser = $user->ID; 

$args = array(
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'author' => $IDuser,
    'orderby' => 'ID',
    'order' => 'ASC',
);
$first_post = new WP_Query($args);
if ($first_post->have_posts()) {
    $first_post->the_post();

    echo the_time('M j, Y');

    wp_reset_postdata();
}

1 Answer
1

The following query retrieves the oldest post of a specified user/author:

$user_id = 42; // or whatever it is
$args = array(
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'author' => $user_id,
    'orderby' => 'date',
    'order' => 'ASC',
);
$first_post = new WP_Query($args);
if ($first_post->have_posts()) {
    $first_post->the_post();

    // Now you can use `the_title();` etc.

    wp_reset_postdata();
}

If you want to show the first post a specific user wrote (no matter what the post’s publish date is set/altered to), then you have to use 'orderby' => 'ID',.

Leave a Comment