Possible Problems:
- A change in the time zone could between
'End_Time'
anddate( 'G:i' )
. - A change in the time between calling
date( 'G:i' )
anddate( 'w' )
. - Repeated use of the
date( 'w' )
function. - A meta query logic problem.
Any of these could be the culprit.
The time zone problem
If the time set in the 'End_Time'
custom value is a different time zone from the web server, you might need to change date( 'G:i' )
to adjust that time zone change. I do not know all the ramifications of using this call:
date_default_timezone_set('Europe/London');
You should probably do some testing near midnight (server time) to see what date( 'G:i' )
and date( 'w' )
are returning and compare them with typical 'End_Time'
custom values from posts.
The date( 'w' )
function
You check it repeatedly, but what happens near midnight on the server?
if ( date( 'w' ) == 0 ) {
else if ( date( 'w' ) == 1 ) {
else if ( date( 'w' ) == 2 ) {
else if ( date( 'w' ) == 3 ) {
else if ( date( 'w' ) == 4 ) {
else if ( date( 'w' ) == 5 ) {
else if ( date( 'w' ) == 6 ) {
Near midnight the value in date( ‘w’ ) could change. If it does, there is a chance that $args
is either unset or set to a value with the wrong category.
It would be better to check date( 'w' )
only one time:
$day_of_the_week = date( 'w' );
if ( $day_of_the_week == 0 ) {
else if ( $day_of_the_week == 1 ) {
else if ( $day_of_the_week == 2 ) {
else if ( $day_of_the_week == 3 ) {
else if ( $day_of_the_week == 4 ) {
else if ( $day_of_the_week == 5 ) {
else if ( $day_of_the_week == 6 ) {
You really do not need all the if else if
stuff. Since you are only changing the 'cat'
argument, this should do the same thing without all that typing.
$time = date( 'G:i' );
// S M T W T F S
$day_of_the_week_categories = array( 17, 18, 12, 13, 14, 15, 16 );
$day_of_the_week = date( 'w' );
// The Query
$the_query = new WP_Query( array(
'cat' => $day_of_the_week_categories[ $day_of_the_week ],
'posts_per_page' => 1,
'offset' => 0,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'End_Time',
'value' => $time,
'compare' => '>',
'type' => 'TIME',
),
),
);
Change in the time between calling date( 'G:i' )
and date( 'w' )
Close to midnight (server time) the call to date( 'G:i' )
might occur on Tuesday and the call to date( 'w' )
might occur on Wednesday. This might be fixed by changing the code above to this:
$now = time();
$time = date( 'G:i', $now );
$day_of_the_week = date( 'w', $now );
The PHP date()
function gets the time from the PHP time()
function. By doing all time calculations off a single call to time()
, $time
and $day_of_the_week
should always refer to the same time and day.
A meta query logic problem.
This problem is harder to diagnose without working directly on the web site and testing the posts there. It is very important that all date and time functions used to calculate 'End_Time'
are also used to calculate $time
and $day_of_the_week
. Similar midnight problems may arise when calculating 'End_Time'
.