Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type named "performance". I have a custom post query that orders posts by a custom field value (order-date). Now I want to exclude posts that have order-dates that are in the past. I would think I could use the same "order-date" value to compare against todays date to determine if a post should be shown, but I can't find an example that works with the ordering. Everything I've tried breaks the ordering.

This seems to work!

<?php
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $today = date('Y-m-d');
  query_posts(array(
    'post_type' => 'performance', 
    'posts_per_page' => 5, 
    'caller_get_posts' => 5, 
    'paged' => $paged,
    'meta_key' => 'order-date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
      array(
        'key' => 'order-date',
        'meta-value' => $value,
        'value' => $today,
        'compare' => '>=',
        'type' => 'CHAR'
        )
      )
    ));
if (have_posts()) :
while (have_posts()) : the_post();
?>

Note that I defined "$today" and added it to the meta_array for comparison with the meta-value. The past-dated performance items disappeared from the list and everything looks like it should. Thanks to Brady for putting me on the right track.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You need to make use of meta_query http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

<?php
 $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
 query_posts(array(
 'post_type' => 'performance', 
 'posts_per_page' => 5, 
 'caller_get_posts' => 5, 
 'paged' => $paged,
 'meta_key' => 'order-date',
 'orderby' => 'meta_value',
 'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'order-date',
            'value' => $value,
            'compare' => '>=',
            'type' => 'CHAR'
        )
    )
 ));
if (have_posts()) :
while (have_posts()) : the_post();
?>
share|improve this answer
    
Brady - Thanks, but I'm not sure how to implement this. I'm getting an error. –  Ray Gulick Jun 23 '11 at 23:44
    
Here is the code I implemented: <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'performance', 'meta_query' => array( array( 'key' => 'order-date', 'value' => $value, 'compare' => '>=' ), array( 'posts_per_page' => 5, 'caller_get_posts' => 5, 'paged' => $paged, 'meta_key' => 'order-date', 'orderby' => 'meta_value', 'order' => 'ASC' ), ) ); $query = new WP_Query( $args ); if (have_posts()) : while (have_posts()) : the_post(); ?> –  Ray Gulick Jun 23 '11 at 23:54
    
Can't get the code to display correctly; sorry. –  Ray Gulick Jun 24 '11 at 1:34
    
firstly what error are you getting. secondly please update your question with the code your trying. thirdly how are you storing your date, what format? –  Brady Jun 24 '11 at 6:17
    
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = array( 'meta_query' => array( array( 'key' => 'order-date', 'value' => $value, 'compare' => '>=' ), array( 'post_type' => 'performance', 'posts_per_page' => 5, 'caller_get_posts' => 5, 'paged' => $paged, 'meta_key' => 'order-date', 'orderby' => 'meta_value', 'order' => 'ASC' ) ) ); $query = new WP_Query( $args ); if (have_posts()) : while (have_posts()) : the_post(); ?> –  Ray Gulick Jun 25 '11 at 4:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.