$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Order by custom field date with ASC order|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Order by custom field date with ASC order

matteradmin9PV0评论

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'
));

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far