WordPress.org

Ready to get started?Download WordPress

Forums

Using Meta Compare to Show Upcoming Dates for Custom Post (2 posts)

  1. k8peterson
    Member
    Posted 11 months ago #

    Hello,
    I have searched the forums but am not able to make this work. I need to only show upcoming dates and right now, it shows all dates regardless. In the below example, i am simply showing the next date.

    When I echo $today, it displays correctly as Y, d, m.

    I have tried the solution on this page and if just displays the else statement.
    http://wordpress.org/support/topic/order-custom-posts-by-custom-field-value-exclude-by-date?replies=12

    What am I missing? THANK YOU!

    <?php $today = date('y-m-d'); query_posts('post_type=>event&meta_key=event-date&meta_compare=>=&meta_value='.$today.'&orderby=meta_value&order=ASC&posts_per_page=1');
    ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <strong><?php echo get_post_meta($post->ID, 'date-for-humans', true); ?></strong><br />
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    <?php endwhile; ?>
    <?php else : ?>
    <p>See you next Season!</p>
    <?php endif; ?>
  2. Thea G
    Member
    Posted 8 months ago #

    Hi Kate,

    I did something very similar and I had the same problem...it was showing all the dates and not making the comparison. The problem turned out to be the way the date is formatted. Make sure the value in your meta-key, 'event' is in the same format as $today. This is where your problem is in this example. You need to use "Y-m-d". Also it is recommended to use new WP_Query instead of query_posts.

    <?php $today = date('Y-m-d');
    $args=array(
    	'post_type' => 'event',
    	'meta_key' => 'event-date',
    	'meta-compare' => '>=',
    	'meta-value' => $today,
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'paged' => $paged
    );
    $event_loop = new WP_Query($args)
    
    ?>

Reply

You must log in to post.

About this Topic