Having trouble querying posts in the loop
-
Hi,
I’ve created a plugin which adds a custom post type (programmes) with some custom meta. I’m now trying to create a shortcode which displays programmes if their broadcast-date (custom meta) is greater than or equal to today’s date.
I’m working on a local install so I can’t post a link, but I’ve posted the code I’m using below. The broadcast-date uses the html datepicker input and is rendered on the page in YYYY-MM-DD format (although in the input field it shows DD/MM/YYYY), so I’ve tried both formats in a php variable with today’s date to compare it with, but it returns all the programmes, regardless of date.
add_shortcode( 'my-schedule', 'my_schedule_shortcode' ); function my_schedule_shortcode( $atts ) { $todaysDate = date("Y-m-d"); // I've tried date("d-m-Y") too ob_start(); $query = new WP_Query(array( 'post_type' => 'programmes', 'meta_query' => array( array( 'orderby' => 'broadcast-date', 'key' => 'broadcast-date', 'value' => $todaysDate, 'compare' => '>=' ), ), )); if ( $query->have_posts() ) { ?> <div class="schedule-container" > <?php while ( $query->have_posts() ) : $query->the_post(); $programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); $programmeBroadcastDate = get_post_custom_values('broadcast-date', $post->ID); ?> <div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )"> <div class="schedule-titlebar"> <h3><?php the_title(); ?></h3> <p><?php echo $programmeBroadcastDate[0] ?></p> </div> <div class="schedule-description"> <?php the_excerpt(); ?> </div> <a class="schedule-link-overlay" href=" <?php the_permalink(); ?> " title=" <?php the_title(); ?> " ></a> </div> <?php endwhile; wp_reset_postdata(); ?> </div> <?php $myvariable = ob_get_clean(); return $myvariable; } }Any ideas?
Thanks
The topic ‘Having trouble querying posts in the loop’ is closed to new replies.