开发者

Custom loop not comparing metavalues properly

Hi so I have a custom loop that is supposed to show upcoming events only it's not taking into account the year so it's displaying future events as being in the past due to just the month and date.

<ul class="upcoming">
<?php // Get today's date in the right format
$todaysDate = date('m/d/y H:i:s');
?>

<?php query_posts('showposts=2&cat=9&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>

<?php if (have_posts()) : ?>
<?php while (have_posts()) : ?>  
<?php the_post(); ?>

<li>
    <?php 
    $Date = get_post_meta($post->ID, 'Date', true);
    ?>

        <?php if ($Date) : ?>
        <div class="date"><span class="month"> <?php echo date('M',strtotime($Date));?> </span>
            <span class="day"><?php echo date('j',strtotime($Date));?> </span></div>
        <?php endif; ?>
        <h4 class="EventTitle"><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
        <span class="EventType"><?php
                    foreach((get_the_category()) as $childcat) {
                    $category_link = get_category_link( 
                                            $childcat );
                    if (cat_is_ancestor_of(10, $childcat)) {
                    if (in_category('13'))
                    echo "<a href=\"$category_link\">Workshop</a>";
                    else echo "<a href=\"$category_link\">$childcat->cat_开发者_如何转开发name</a>";
                    }}
                    ?>
        </span>
            </li>

<?php endwhile; ?>
<?php else : ?>
    <p>Check back soon for new events.</p>
<?php endif; ?>                 
<?php wp_reset_query(); ?>

The ouput for this module is "Check back soon for new events", regardless of the fact that there are future events available.

I've scoured this thing for hours and can't come up with a reason for why it's ignoring the year. Any ideas?


It would seem to me that you are comparing the date as a string rather than as a date literal. And because you are comparing strings, and have it set as mm/dd/yyyy, the month and date take a greater precedence than the year does. So 10/21/2014 is less than 10/22/2010.

You may have to reformat your post_meta Date format to be able to make an accurate date comparison. The best way would be to use yyyy-mm-dd hh:mm:ss so everything is in order of decreasing precedence. Alternately, you could break apart the post_meta Date and compare the year then month then day and time in that order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜