Order by custom field date with ASC order

admin2025-06-02  5

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.

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 asked Jul 26, 2012 at 17:23 elbatronelbatron 4051 gold badge8 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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!

After searching multiple similar posts, I put a solution together from parts of various other SO posts, hopefully it can help somebody else.

I had a custom meta field called "date" (poor naming convention, I know), and this query shows all posts with a custom "date" meta field in the future, sorting by closest to today.

$args = array(
        'post_type' => 'training-course',
        'posts_per_page' => '-1',
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'date',
                'value' => date("YYmmdd"),
                'compare' => '<='
            )                   
         )
    );

For others referencing this question, there is also a built-in way of doing this that is simpler, you can use eventDisplay in your query with the values upcoming, past, or custom. Tribe Events documentation is a bit miserable so who knows what custom does. If you use tribe_get_events(), it assumes upcoming by default, and I believe certain queries are automatically hijacked to show upcoming.

Here is one that shows 4 upcoming events, ordered by start date, from near to the future.

array(
  'showposts' => 4,
  'post_type' => 'tribe_events',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_key' => '_EventStartDate',
  'eventDisplay' => 'upcoming'
));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748840636a314165.html

最新回复(0)