-1

I am coding a Live Show box for a radio website to show who is on air and who is up next. I've been using the get_post function to retrieve posts based on the category (in this case the day of the week), and whether the show has yet to reach its End Time (custom field) in order to display the show which should be live on air now.

However, it's had a turn and will work sometimes, and will show the wrong show title at others. Any ideas where I am going wrong?

Code is below and visit http://livewire1350.com to see it in action.

EDIT: I have modified the code based on @CharlesClarkson 's comments but there is still a problem occurring. For debugging purposes I set the code to show the whole entire list of posts in ascending order of time but between 0:00 and 9:00, it begins to list 22:30 descending to 18:00 before resuming at 9:00 in ascending fashion up until 17:00 (like below). Any ideas why this could be happening?

LIVE: 9:29

0:00

LIVE: 9:29

22:30

LIVE: 9:29

21:00

LIVE: 9:29

19:30

LIVE: 9:29

18:00

LIVE: 9:29

9:00

LIVE: 9:29

10:30

LIVE: 9:29

12:00

LIVE: 9:29

12:30

LIVE: 9:29

14:00

LIVE: 9:29

15:30

LIVE: 9:29

17:00

<?php
date_default_timezone_set('Europe/London');
$now  = time();
$time = date( 'G:i');
$day_of_the_week = date( 'w', $now );
//                                    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'          => 'DESC',
    'meta_query'     => array(
        array(
            'key'     => 'Start_Time',
            'type'    => 'TIME',
        )
    )
   )
);
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();?>
<div class="liveshow">
    <div id="livebanner">

    <div id="showwrap">
    <div id="showdetails">
        <div id="onair">
            <h3>LIVE: <?php echo $time ?></h3><p class="showname"><a href="<?php the_permalink() ?>"><?php $short_title = substr(the_title('','',FALSE),0,18);
                                                echo get_post_meta( get_the_ID(), 'Start_Time', true );

                                             ?></p>
            </div>
<?php
endwhile;
endif;
?>
| improve this question | | | | |
  • Any idea under what specific conditions it fails? What is the relationship of the "wrong title" to what should be the correct title? – s_ha_dum Oct 7 '13 at 23:18
  • 1
    And also, please, format and indent your code so it is easy for anyone to follow the logic. I have to say that, right now, the appeal to read this code is almost zero. – brasofilo Oct 8 '13 at 1:08
  • This code is incomplete. You never close most of the <div>s that you open in the while loop and the if conditional never ends. It gets to else: statement, but the endif; is missing. – Charles Clarkson Oct 8 '13 at 3:52
  • Is the wrong title appearing when the page is loaded around midnight on the server? – Charles Clarkson Oct 8 '13 at 5:10
  • @barsofilo - sorry I have tried to reformat it to make it easier for you – Tech Oct 8 '13 at 9:35
0

Possible Problems:

  • A change in the time zone could between 'End_Time' and date( 'G:i' ).
  • A change in the time between calling date( 'G:i' ) and date( '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'.

| improve this answer | | | | |
  • Still no luck I'm afraid. It is weird as I've tested the server date() values against the 'End_Time" values of the posts and there's nothing suspicious, yet it doesn't order them by the time... Also checked it against a local server which seems to work fine, yet the live site still has problems despite the coding being the same. – Tech Oct 9 '13 at 0:02

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.