简体   繁体   中英

Date issue in custom post type loop (wordpress)

I am using a custom post type with custom date fields to display tour dates on a page. Some of the posts have both a "from" and "to" date (because they last for more than one day) and some only have "from" (because they're only one day), and I want all future or current tour dates to be displayed.

It's all working perfectly, except for some reason the posts that only have a "from" date stop showing if that date is in 2016 or later. It doesn't make any sense to me!

Here are the args:

$today = date('Ymd');
$tour_args = array(
    'post_type' => 'tour_date',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'meta_key' => 'tour-date-from',
    'order' => 'ASC',
    'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => 'tour-date-from',
        'value' => $today,
        'compare' => '<=',
      ),
      array(
        'key' => 'tour-date-to',
        'value' => $today,
        'compare' => '>=',
      ),
    ),
);

This is now solved. The problem was that I needed to specify the meta type. I also needed to take out the 'meta_key' line and therefore amend the 'orderby' line to look for the right value.

$today = date('Ymd');
$tour_args = array(
    'post_type'      => 'tour_date',
    'posts_per_page' => -1,
    'orderby'        => 'tour-date-from',
    'order'          => 'ASC',
    'meta_query'     => array(
      'relation'     => 'OR',
      array(
        'key'     => 'tour-date-to',
        'value'   => $today,
        'compare' => '>=',
        'type'    => 'NUMERIC'
      ),
      array(
        'key'     => 'tour-date-from',
        'value'   => $today,
        'compare' => '<=',
        'type'    => 'NUMERIC'
      ),
    ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM