• 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)
  • From the Codex:

    category_name (string) – use category slug (NOT name).

    And, I believe your second compare should be ‘<‘ instead of ‘>’.

    Thread Starter FinDes

    (@findes)

    I tried this and it netted the same results as before.

    Thread Starter FinDes

    (@findes)

    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.