Support

Account

Home Forums General Issues How do I filter and sort event posts with start and end date

Helping

How do I filter and sort event posts with start and end date

  • Hello Everyone!

    Having a hard time nailing down a WP_Query, hoping for some help. I have a custom post type for events, with a start_date and end_date field. I’d like to get a list of future or ongoing events. That is events with a start date or end date that is greater then now. Here is what I have, it give me back a list of events, but not in the correct order:

    <?php
    $query_args = array(
      'paged' => 1,
      'posts_per_page' => 6,
      'orderby' => 'meta_value_num',
      'order' => 'ASC',
      'post_type' => 'event',
      'post_status' => 'publish',
      'meta_key' => 'start_date',
      'meta_query' => array(
        'relation' => 'OR',
        array(
          'key' => 'start_date',
          'value' => date('Ymd', strtotime('now')),
          'type' => 'numeric',
          'compare' => '>=',
        ),
        array(
          'key' => 'end_date',
          'value' => date('Ymd', strtotime('now')),
          'type' => 'numeric',
          'compare' => '>=',
        ),
      ),
    );
    $my_query = new WP_Query($query_args);
    ?>
    • Elliot

    • October 30, 2013 at 4:17 pm

    Hi @m4olivei

    Just to clarify, the meta_query is working, but the order is wrong?

    Your orderby param looks good. Have you debugged the SQL run on the WP_Query object?

    You can do this by simply printing out the object after you create it

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.