WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] adding a WHERE 'x' >= 'y' to my WP_Query (3 posts)

  1. niceandripe
    Member
    Posted 3 years ago #

    i'm using this to pull in my 'events' custom post type

    WP_Query('post_type=events&posts_per_page=10&meta_key=Date&orderby=meta_value&order=ASC&')

    but I want to add a WHERE clause so it only brings in dates that are in the future

    i've managed to kinda do it with this, but this only works in the 'loop'

    <?php $recent = new WP_Query('post_type=events&posts_per_page=10&meta_key=Date&orderby=meta_value&order=ASC');
    
    if ($recent->have_posts()) :
    while($recent->have_posts()) : $recent->the_post(); 
    
    $fakedate = get_post_meta($post->ID, 'Date', true);
    list($y, $m, $d) = explode('/', $fakedate);
    $makedate = mktime(0, 0, 0, $m, $d, $y);
    $eventdate = strftime('%d %b %Y',$makedate);
    $checkdate = strftime('%Y%m%d ',$makedate);
    $todaysdate = date('Ymd'); 
    
    if ($checkdate >= $todaysdate) : ?>
    
      <a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php echo $eventdate; ?> - <?php the_title(); ?></a>
    
    <?php endif; ?>
    <?php endwhile; ?>
    <?php else : ?>
    
      <p>There are currently no events to display</p>
    
    <?php endif; ?>
  2. jonburney
    Member
    Posted 3 years ago #

    You can add in the 'meta_compare' value to perform a comparison on the date field that you're pulling in

    'meta_key=Date&meta_compare=>=&meta_value=_TODAYS_DATE_HERE_'

    That will check that the meta value you're pulling in is greater than or equal to todays date. You'll need to replace _TODAYS_DATE_HERE with the date in the correct format.

    There's more details on the meta query parameters here http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

    Jon

  3. niceandripe
    Member
    Posted 3 years ago #

    thanks Jon - I totally overlooked that when trawling thru the codex!

Topic Closed

This topic has been closed to new replies.

About this Topic