在get_posts中按日期自定义元框值排序

时间:2014-11-25 09:12:37

标签: php wordpress

我需要按自定义元框日期字段

订购帖子
    $args = array(
                'meta_key' => 'event_date',
                'orderby' => 'meta_value_num',              
                'order'         => 'ASC',                    
                'post_status'   => 'publish',
                'post_type'     => 'events',
                'numberposts'   => -1
            );
$poslist= get_posts( $args );

我尝试了上面的代码,但它不起作用, 日期归档格式为" yyyy-mm-dd"

2 个答案:

答案 0 :(得分:1)

尝试一下

的内容
$args = array(
  'post_type' => 'events',
  'meta_key' => 'event_date',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
    array(
      'key' => 'event_date',
      'compare' => 'LIKE',
    )
  )
);
$query = new WP_Query($args);

答案 1 :(得分:0)

           $fromDate = '2013-01-01';
            $toDate = '2014-01-01';
            $args = array(
                'meta_key' => 'event_date',
                'orderby' => 'meta_value',                
                'order'         => 'DESC',           
                'post_status'   => 'publish',
                'post_type'     => 'page',
                'numberposts'   => -1,
                'meta_query' => array( array(
                                        'key' => 'event_date',
                                        'value' => $toDate, // date to compare to, before this one
                                        'compare' => '<',
                                        'type' => 'DATE' //set the format
                                        ),
                                        array(
                                            'key' => 'event_date',
                                            'value' => $fromDate, // date to compare to, after this one
                                            'compare' => '>=',
                                            'type' => 'DATE' //set the format
                                        ),
                                    ),
            );

         $currentEventsPost = get_posts( $args );