2

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();
}
2
  • Hi @t f - its defined just above $useremail = $ticket['email'];**<br>**$user = get_user_by( 'email', $useremail );**<br>**$IDuser = $user->ID; Tried both publish & ED in case of typo... echo $IDuser returns correct user IDs if I call it
    – Bysander
    Feb 20, 2014 at 17:05
  • let us continue this discussion in chat
    – Bysander
    Feb 21, 2014 at 13:40

1 Answer 1

4

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',.

5
  • 2
    +1 But for orderby => ID you'll have to keep in mind that this could as well be a private post, a draft, etc. I'd take the first one, but use post_status => published.
    – kaiser
    Feb 20, 2014 at 16:09
  • Hi there @kaiser and @t f - thank you for your responses... I put in the code above $args = array( 'posts_per_page' => 1, 'post_status' => 'published', '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(); } ?>
    – Bysander
    Feb 20, 2014 at 16:55
  • Changing $user_id to the $IDuser I have been using, orderby ID as I do want to include any post (draft, automatic etc) and 'post_status => published' however it returns a blank with no errors in php log... Have I done something wrong here - I used the_time('M j, Y') as recommended by another blog when you have multiple entries on a page.
    – Bysander
    Feb 20, 2014 at 16:58
  • @t f Good point - have edited into original under EDIT heading
    – Bysander
    Feb 20, 2014 at 17:00
  • @Bysander File edits, don't post code in comments. And if you really need to: Format them. Below the button on the right there's "help".
    – kaiser
    Feb 20, 2014 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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