2

I know how to get the last or oldest post data using following code

$posts = get_posts(array(
  'post_type' => 'post',
  'order_by' => 'publish_date',
  'order' => 'ASC'
));

And then I will get the date from the first array itself, but is there any quickest way to get that ?

1
  • You can use custom query $oldest_post_id = $wpdb->get_row("SELECT id FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date ASC");
    – Trilok
    Commented Aug 1, 2018 at 8:43

1 Answer 1

3
function is_first() {
    global $post;
    $loop = get_posts( 'numberposts=1&order=ASC' );
    $first = $loop[0]->ID; 
    return ( $post->ID == $first ) ? true : false;
} 

you can use this too

1
  • 2
    This also works, but for me this was the best one $oldest_post_id = $wpdb->get_row("SELECT post_date FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date ASC");
    – topper1309
    Commented Aug 1, 2018 at 16:07

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.