Hello WordPress developers. Over the years, I've found answers to many questions I've had with WordPress. I want to thank you for your help. Today, I have a question myself, and seek for expert advice.
I'm building a site for a movie theatre. I have a custom post type movies. I'm using Types plugin to store the showdates and -times for each movie as a repeating date/time field. It stores the date/time as a Unix timestamp to postmeta table with meta_key "wpcf-showtime". Each movie has many showtimes, thus there is a number of meta_key/meta_value pairs having the same key, e.g.
post_id: 70, meta_key: wpcf-showtime, meta_value 1417548600
post_id: 70, meta_key: wpcf-showtime, meta_value 1417813200
post_id: 70, meta_key: wpcf-showtime, meta_value 1417896000
For the front page of the site, I want to show all ongoing movies till next week's Wednesday i.e. movies that have showtimes from now on, excluding movies that occur in the past or are marked as future (custom taxonomy).
My problem is ordering the movies; I want to show them ordered by showtime so, that the movie having the closest showtime to present time comes first.
This is the query I'm running:
$startdate = time(); $enddate = new DateTime('thursday next week'); $ongoing_movies = new WP_Query( array( 'post_type' => 'movies', 'meta_key' => 'wpcf-showtime', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => 'future', 'operator' => 'NOT IN' ) ), 'meta_query' => array( array( 'key' => 'wpcf-showtime', 'value' => array ( $startdate, $enddate->format('U')), 'compare' => 'BETWEEN', ), ) ));
This returns the correct movies, ordered by showtime. But the showtime it uses for sorting is the first value occurrence for meta_key "wpcf-showtime" stored for that particular movie. I.e. the first movie displayed may not be the one that has the next show, but the one that has the earliest showtime stored in db.
I would appreciate any help on finding an approach; pre_get_posts action, posts_orderby filter?