最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Quickest way to get last or oldest post date - WP Query

programmeradmin2浏览0评论

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 ?

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 ?

Share Improve this question asked Aug 1, 2018 at 8:29 topper1309topper1309 751 gold badge2 silver badges9 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论