0

I’m working on a games release feature for a website I’m building. So far everything is going smooth, but I’m stuck on one area. For the listings on the front end, I want it to look at the “release_date” field and only display games that have a release date >= that date.

I feel the code below should work, but it doesn’t return/display anything on the front end. And if I switch it to <=, it displays everything.

Any help would be greatly appreciated! Below is the query code.

$releasewindow = date('M j, Y', strtotime("-31 days"));
    
    $q = new WP_Query(array(
        'post_type' => 'game_release',
        'posts_per_page' => 5,
        'meta_key'  => 'release_date',
        'orderby'   => 'release_date',
        'order' => 'ASC',
        'meta_query' => array(
        array(
            'key'     => 'release_date',
            'compare' => '>=',
            'value'   => $releasewindow,
        )
    ),
3
  • 1
    M j, Y is a pretty strange format to store a date in - is that what the format actually is in the database?
    – bosco
    May 28, 2022 at 22:30
  • Yea, it is. It's an annoying story as to why they wanted it that way...
    – M. Straw
    May 31, 2022 at 14:15
  • That's super unfortunate - I don't believe there's an efficient or canonical way to select posts based on dates stored in that format. They either need to be in YYYY-MM-DD format or stored as a timestamp. What's happening with your current code is the SQL is just executing a standard alphanumeric comparison - so dates are getting ordered more or less by the abbreviated name of the month. Would it be permissible for you to add a second meta-field storing the same date in a more workable format?
    – bosco
    May 31, 2022 at 16:32

0

Your Answer

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

Browse other questions tagged or ask your own question.