I have a template that displays an actual event if we have a today one, or a next future one. The template uses this WP_Query, that compares events date with the actual date:
$args = array( 'post_type' => 'events', // Tell WordPress which post type we want 'orderby' => 'meta_value', // We want to organize the events by date 'meta_key' => 'event_start_date', // Grab the "start date" field created via a third party plugin (stored in YYYY-MM-DD format) 'order' => 'ASC', // ASC is the other option 'posts_per_page' => '1', // Let's show only one / the first event. 'meta_query' => array( // WordPress has all the results, now, return only the events after today's date array( 'key' => 'event_start_date', // Check the start date field 'value' => date_i18n("Y-m-d"), // Set today's date (note the similar format) 'compare' => '>=', // Return the ones greater than or equal to today's date 'type' => 'DATE' // Let WordPress know we're working with date ) ) ); $featured = new WP_Query( $args );
This works, but I found a problem. Accordingly to this answer, WordPress sets the PHP timezone to UTC for internal calculations. In my database events date are saved in a "Y-m-d" date format (without time). So, this means that at 2017-08-31 00:01 date/time (24h format) in my local timezone (UTC+2) the WP will compare a saved event date, say 2017-08-31, with 2017-08-30 of his internal UTC timezone (2 hours difference!) and still will display an old event instead of new one in these 2 hours after 2017-08-31 00:00 in my local time. How to solve this?
Changed the code as was recommended by @Milo. The problem is solved!
UPDATE - About duplicate
Guys, I suppose that for you my question is a duplicate of an existing question that already has a very clear answer, clear for you, but not also for me, as this answer doesn't have any example code. I seen that answer, if you have not noticed, and I put a link to it in my question before you, but this doesn't helped me much. I needed an exact solution for my situation, and @milo provided it for me, so I thank you him!