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!

Read More

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' => '>=',
      ),
    ),
);

Related posts

1 comment

  1. 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'
          ),
        ),
    );
    

Comments are closed.