Title: Custom Meta Queries
Last modified: August 21, 2016

---

# Custom Meta Queries

 *  [FinDes](https://wordpress.org/support/users/findes/)
 * (@findes)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/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)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/custom-meta-queries/#post-4999843)
 * From the Codex:
 * > category_name (string) – use category slug (NOT name).
 * And, I believe your second compare should be ‘<‘ instead of ‘>’.
 *  Thread Starter [FinDes](https://wordpress.org/support/users/findes/)
 * (@findes)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/custom-meta-queries/#post-4999845)
 * I tried this and it netted the same results as before.
 *  Thread Starter [FinDes](https://wordpress.org/support/users/findes/)
 * (@findes)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/custom-meta-queries/#post-4999893)
 * I am closer, with the code below, the Upcoming events show in the first loop,
   but the past events query duplicates the first upcoming event 4 times.
 *     ```
       <h1>2013-14 Concert Season</h1>
       		<?php
       			$today = date('Ymd');
       			$args1 = array(
       				'type' => 'post',
       				'category_slug' => 'Featured1314',
       				'meta_key' => 'concert_date',
       				'meta_query' => array(
       					'relation' => 'AND',
       						array(
       							'key' => 'concert_date',
       							'value' => $today,
       							'compare' => '>='
       						)
       					),
       				'orderby' => 'meta_value_num',
       				'order' => 'ASC'
       				);
       			$upcoming_query = new WP_Query($args1);
       			if( $upcoming_query->have_posts() ) { ?>
       		<h2>Upcoming Concerts</h2>
       		<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();
       			$today = date('Ymd');
       			$args2 = array(
       				'type' => 'post',
       				'category_slug' => 'Featured1314',
       				'meta_key' => 'concert_date',
       				'meta_query' => array(
       					'relation' => 'OR',
       						array(
       							'key' => 'concert_date',
       							'value' => $today,
       							'compare' => '<'
       						)
       					),
       				'orderby' => 'meta_value_num',
       				'order' => 'ASC'
       				);
       			$past_query = new WP_Query($args2);
       			if( $past_query->have_posts() ) {?>
       			<h2>Past Concerts</h2>
       		<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)

The topic ‘Custom Meta Queries’ is closed to new replies.

## Tags

 * [meta_query](https://wordpress.org/support/topic-tag/meta_query/)
 * [meta_value_num](https://wordpress.org/support/topic-tag/meta_value_num/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [FinDes](https://wordpress.org/support/users/findes/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/custom-meta-queries/#post-4999893)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
