• 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.

    <?php
    /*
     * Template Name: Live Show Widget
     * Description: Displaying live show details.
     */
     ?>
    <script type="text/javascript">
    <!--
    
    function openWindow(theURL,winName,features) { //v2.0
      window.open(theURL,winName,features);
    }
    //-->
    </script>
    <?php
    date_default_timezone_set('Europe/London');
    $now  = time();
    $time = date( 'G:i', $now );
    $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',
                'value'   => $time,
                'compare' => '<',
                'type'    => 'TIME',
            )
        )
       )
    );
    // The Loop
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();?>
    <div class="liveshow">
    	<div id="livebanner">
    	<a href="http://livewire1350.com/listen-live/" onclick="openWindow('http://livewire1350.com/listen-live/','Listen Live','scrollbars=yes,width=350,height=600'); return false;">
    	<img id="listen" src="http://livewire1350.com/wp-content/listenlive.jpg" /></a>
    
    	<div id="showwrap">
    	<div id="showdetails">
    		<div id="onair">
    			<h3>LIVE:</h3><p class="showname"><a href="<?php the_permalink() ?>"><?php $short_title = substr(the_title('','',FALSE),0,18);
    												echo $short_title;
    												if (strlen($short_title) > 17){
    													echo '...';
    												}  ?></a></p>
    			<a href="http://livewire1350.com/listen-live/" onclick="openWindow('http://livewire1350.com/listen-live/','Listen Live','scrollbars=yes,width=350,height=600'); return false;">
    			<div id="showimg"><?php the_post_thumbnail('onair-thumb', array('title' => ''.get_the_title().'' )); ?></div></a>
    			<p class="showmeta"><a href="<?php the_permalink() ?>">View Page</a> | <a href="http://livewire1350.com/listen-live/" onclick="openWindow('http://livewire1350.com/listen-live/','Listen Live','scrollbars=yes,width=350,height=600'); return false;">Listen Live</a></p>
    		</div>
    <?php
    endwhile;
    
    // Reset Query
    wp_reset_postdata();
    
    else:  ?>
    <div class="liveshow">
        <div id="livebanner">
    	<a href="http://livewire1350.com/listen-live/" onclick="openWindow('http://livewire1350.com/listen-live/','Listen Live','scrollbars=yes,width=350,height=600'); return false;">
    	<img id="listen" src="http://livewire1350.com/wp-content/listenlive.jpg" /></a>
    
    	<div id="showwrap">
    	<div id="showdetails">
    		<div id="onair">
    			<h3>LIVE:</h3><p class="showname"><embed  src='stream.swf'
      width='240'
      height='20'
      allowscriptaccess='always'
      allowfullscreen='true'
      flashvars='streamer=rtmp://stream'
    /></p>
    			<p class="showmeta"></p>
    		</div>
    
    <?php endif;
    
    $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'     => 'Start_Time',
                'value'   => $time,
                'compare' => '>',
                'type'    => 'TIME',
            )
        )
       )
    );
    // The Loop
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();?>
    
    		<div id="nextonair">
    			<h5><?php echo get_post_meta( $post->ID, 'Start_Time', true ); ?>:</h5>
    			<p class="nextshow"><?php the_title(); ?></p>
    <?php
    endwhile;
    
    // Reset Query
    wp_reset_postdata();
    
    else:  ?>
    <div id="nextonair">
    			<h5></h5>
    			<p class="nextshow"></p>
    <?php endif; ?>
    		</div>
    	</div>
    	</div>
    	</div>
    
    </div>
Viewing 1 replies (of 1 total)
  • I believe the problem is the use of the ‘G:i’ format in $time combined with ‘orderby’ => ‘meta_value_num’.

    The colon in the format will confuse meta_value_num. Try using just ‘meta_value’ instead.

Viewing 1 replies (of 1 total)
  • The topic ‘can't get post based on custom field value being less than current time.’ is closed to new replies.