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

custom post types - meta query condition don't work

programmeradmin1浏览0评论

I have created a custom post type (Event). I want to show only two posts on the front page and the post should be greater than or equal to today's date. So I write this meta_query.

$today = date('ymd');
      $homePostEvents = new WP_Query(array(
        'posts_per_page' => -1,
        'post_type' => 'event',
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_query' => array(
          array(
            'key' => 'event_date',
            'compare' => '>=',
            'value' => $today, // Today,
            'type' => 'NUMERIC'
          )
        )
      ));

So the outcome should be the future date event post but it also bring up the previous date post too.

I have created a custom post type (Event). I want to show only two posts on the front page and the post should be greater than or equal to today's date. So I write this meta_query.

$today = date('ymd');
      $homePostEvents = new WP_Query(array(
        'posts_per_page' => -1,
        'post_type' => 'event',
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_query' => array(
          array(
            'key' => 'event_date',
            'compare' => '>=',
            'value' => $today, // Today,
            'type' => 'NUMERIC'
          )
        )
      ));

So the outcome should be the future date event post but it also bring up the previous date post too.

Share Improve this question asked Jun 2, 2020 at 2:33 Moumita akterMoumita akter 52 bronze badges 2
  • In what format are your dates stored? – vancoder Commented Jun 2, 2020 at 22:16
  • Ymd format..... – Moumita akter Commented Jun 2, 2020 at 22:31
Add a comment  | 

1 Answer 1

Reset to default 0

Can you try changing 'type' => 'NUMERIC' to 'type' => 'DATE' ?

Reference: https://codex.wordpress/Class_Reference/WP_Meta_Query

发布评论

评论列表(0)

  1. 暂无评论