Custom Post Query with Multiple Meta Values
-
Having problems querying posts. Each post has a meta value called “end_date_value”. Most of these posts have multiple values in there.
I’m trying to order the posts by these values even, including the multiples.
Example:
Show A: 2011-01-01, 2011-04-02, 2011-06-29
Show B: 2011-01-04, 2011-02-05, 2011-08-05Should Show:
Show A: 2011-01-01
Show B: 2011-01-04
Show B: 2011-02-05
Show A: 2011-04-02
Show A: 2011-06-29
Show B: 2011-08-05<?php $today = date('Y-m-d'); $query = new WP_Query( array( 'post_type' => array( 'shows' ), 'meta_key' => 'end_date_value', 'meta_value' => $today, 'meta_compare' => '>=' ) ); if ($query->have_posts()): while ( $query->have_posts() ) : $query->the_post(); show_posts(); endwhile; endif; ?>
This Query Only Shows:
Show A: 2011-01-01
Show B: 2011-01-04$querystr = " SELECT wp_posts.*, wp_postmeta.meta_value FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'end_date_value' AND wp_postmeta.meta_value >= '2011-07-10' AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'shows' ORDER BY wp_postmeta.meta_value "; $pageposts = $wpdb->get_results($querystr, OBJECT); if ($pageposts): global $post; foreach ($pageposts as $post): setup_postdata($post); show_posts(); endforeach; else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php endif; ?>
This Query Shows:
Show A: 2011-01-01
Show B: 2011-01-04
Show B: 2011-01-04
Show A: 2011-01-01
Show A: 2011-01-01
Show B: 2011-01-04
- The topic ‘Custom Post Query with Multiple Meta Values’ is closed to new replies.