Custom Meta Queries
-
I am trying to list posts in category ‘Featured1314’ split into two lists, upcoming events are >= todays date, past events are < todays date. I have added custom fields to the posts with the name of ‘concert_date’ and the values match the date format Ymd (ex: 20140611)
The code I am using below is close, but the first query’s results are all posts in the featured category, and the second query results are just the posts that are >= today.
TIA.
<h2>Upcoming Concerts</h2> <?php $today = date('Ymd'); $args1 = array( 'type' => 'post', 'category_name' => 'Featured1314', 'meta_key' => 'concert_date', 'meta_query' => array( 'key' => 'concert_date', 'value' => $today, 'compare' => '>=' ), 'orderby' => 'meta_value_num', 'order' => 'ASC' ); $upcoming_query = new WP_Query($args1); ?> <ul><?php while ( $upcoming_query->have_posts() ) { $upcoming_query->the_post(); ?> <li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li> <?php } ?> </ul> <?php rewind_posts(); ?> <h2>Past Concerts</h2> <?php $today = date('Ymd'); $args2 = array( 'type' => 'post', 'category_name' => 'Featured1314', 'meta_key' => 'concert_date', 'meta_query' => array( 'key' => 'concert_date', 'value' => $today, 'compare' => '>' ), 'orderby' => 'meta_value_num', 'order' => 'ASC' ); $past_query = new WP_Query($args2); ?> <ul><?php while ( $past_query->have_posts() ) { $past_query->next_post(); ?> <li id="post-<?php the_ID(); ?>"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1><p><?php the_excerpt() ?></p></li> <?php } ?> </ul>
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Custom Meta Queries’ is closed to new replies.