I'm trying to create an events page that lists events (custom post type) within the week. I'm having trouble properly selecting only the posts that have a 'start_date' that falls in the current week.

Here is my code:

    $day = date('w');
    $week_start = date('d-m-Y', strtotime('-'. $day . ' days'));
    $week_end = date('d-m-Y', strtotime('+' . (6 - $day) . ' days'));

    $args = array(
        'post_type' => 'hcc_events',
        'order' => 'ASC',
        'meta_key' => 'start_date',
        'orderby' => 'meta_value',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
                'key' => 'start_date',
                'value' => $week_start,
                'compare' => '>=',
                'type' => 'DATE'
                'key' => 'start_date',
                'value' => $week_end,
                'compare' => '<=',
                'type' => 'DATE'

Right now nothing is output at all. Any help is appreciated!

  • 4
    Dates have to be stored as time stamps or in the format yyyy-mm-dd to query on them – Milo Aug 16 '18 at 21:39
  • 1
    Just checking, sometimes it is the silliest errors that catch us. please show the code where you pass the args to the query function and how you deal with the result. Also might be good to check the format of the data in the database ? As Milo pointed out if you want to do < and > querying, store the dates with the YYYY first. – anmari Aug 17 '18 at 10:56
  • Changing the datetimes to the format of 'Y-m-d' worked perfectly! – BudBroesky Aug 20 '18 at 14:19

Changing the date format to Y-m-d on ACF and in my code was what fixed everything!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.