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

How to query posts with current or future date only

programmeradmin1浏览0评论

I want to only get posts with current or future dates, to show upcoming events.

Current arguments:

$args = array( 
    'post_type' => 'event', 
    'meta_key'  => 'event_start_date',
    'orderby'   => 'meta_value',
    'order'     => 'ASC'
);

This returns all the posts, including past events.

I read about post_status but it did not seem to work. I tried 'post_status' => 'future' without success.

I want to only get posts with current or future dates, to show upcoming events.

Current arguments:

$args = array( 
    'post_type' => 'event', 
    'meta_key'  => 'event_start_date',
    'orderby'   => 'meta_value',
    'order'     => 'ASC'
);

This returns all the posts, including past events.

I read about post_status but it did not seem to work. I tried 'post_status' => 'future' without success.

Share Improve this question edited Oct 30, 2014 at 16:37 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Oct 30, 2014 at 15:35 ChrizleeChrizlee 711 silver badge4 bronze badges 3
  • How is your event_start_date stored (YYYY-MMM-DD, int, ...)? Have a look at the WP_Query docs on Custom Field Parameters. – Pat J Commented Oct 30, 2014 at 16:46
  • It was a premade thing, it is stored as a longtext type. the actual date is stored like this: 2014-11-14 20:00 – Chrizlee Commented Oct 30, 2014 at 17:47
  • 1 Longtext or not, what is the format of the date string? See the first part of the following answer: wordpress.stackexchange/a/124163/21376 – s_ha_dum Commented Oct 30, 2014 at 17:49
Add a comment  | 

2 Answers 2

Reset to default 4

adding the following arguments did the trick

'meta_value' => date('Y-m-d h:i'),
      'meta_compare' => '>',

Adding 'post_status' => array('publish', 'future') to your $args will show only future published posts.

$args = array(
    'post_type'   => 'event', 
    'meta_key'    => 'event_start_date',
    'orderby'     => 'meta_value',
    'order'       => 'ASC',
    'post_status' => array('publish', 'future')
    );
get_posts( $args );
发布评论

评论列表(0)

  1. 暂无评论