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've found several similar question but none of them explains why the below is giving me nothing with ASC order. It works with DESC.

Basically it's a query that should list posts where the custom field values are equal or greater to the current date, then arrange it in reversed order, so the first post should be a post that's custom field are the closest the the current date.

<?php query_posts($query_string . '&showposts=4&order=ASC&post_type=custompost&meta_key=mydate&orderby=meta_value'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

        <?php $todays_date = date("Y/m/j"); ?>
        <?php $today = strtotime($todays_date); ?>
        <?php $exp_date = get_post_meta('mydate'); ?>
        <?php $expiration_date = strtotime($exp_date); ?>
        <?php if ($expiration_date >= $today) { ?>

            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>

    <?php } else { ?>
    <?php };?>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?> 

Also, I've tried many different queries but none of them worked, the above script is the latest I have.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

With some more googling:

<?php
$today = date("Y/m/j");
query_posts(array(
'post_type' => 'custompost',
'posts_per_page' => 4,
'meta_key' => 'mydate',
'orderby' => 'meta_value',
'order' => 'ASC',
    'meta_query' => array(
        array(
           'key' => 'mydate',
           'meta-value' => $value,
           'value' => $today,
           'compare' => '>=',
           'type' => 'CHAR'
       )
)
));
if (have_posts()) :
while (have_posts()) : the_post();
?>
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?> 

I hope it will help others!

share|improve this answer
add comment

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.