简体   繁体   中英

Order by custom field date

I'm using this to list all post titles...

<?php
$args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'orderby'           => 'title',
    'order'             => 'ASC'
);

query_posts($args);
if (have_posts()) :
    while (have_posts()) :
    the_post(); ?>
<p>
    <?php the_title(); ?>
</p>
<?php endwhile;
    endif;
    wp_reset_query();
?>

...and that works fine.

Each of my posts also has a custom field called start_date . All these start dates are entered in the format DD-MM-YYYY (example: 10-03-2016).

What I need to be able to do (and I'm not if this can even be done) is to orderby start_date (instead of title ).

To further illustrate, if there were 3 posts with start dates like this...

Post 1: 01-11-2016

Post 2: 03-01-2016

Post 3: 12-07-2016

...the posts would be displayed in the order of....

Post 2

Post 3

Post 1

...because this is in correct date order.

I hope this makes sense.

Cheers.

In wordpress WP_Query class accept args provide more helpful for post filter. You can check in official site documentation provide custom meta fields accept in query args in order and orderby.

https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

I have two methods handle in this situation.

  1. Your date format in start date is not properly mysql date format(YYYY-MM-DD). If you date format will same as mysql format so you need only change query args like:
$args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'meta_key'          => 'start_date',
    'post_status'       => 'publish',
    'orderby'           => 'meta_value',
    'meta_type'         => 'DATE',
    'order'             => 'ASC'
);
  1. If you do not want to change your date format on every each post or you have many post as difficult to change date format on each post so you can add filter to override order parameters.

Complete code

<?php
    $args = array(
        'posts_per_page'    => -1,
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'meta_key'          => 'start_date',
        'orderby'           => 'title',
        'order'             => 'ASC'
    );
add_filter( 'posts_orderby', function($orderby){
 return 'STR_TO_DATE( wp_postmeta.meta_value, "%d-%m-%Y") ASC';
});
query_posts($args);
if (have_posts()) :
    while (have_posts()) :
    the_post(); ?>
<p>
    <?php the_title(); ?>
</p>
<?php endwhile;
    endif;
    wp_reset_query();
?>

Hope this helpful for you sorry for poor English.

You can Query and sort posts by custom field value by just adding 'orderby' => 'name_of_your custom_field' to your WP_Query Statement.


<?php
$args = array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'orderby'           => 'name_of_your_custom_field',
    'order'             => 'ASC'
);

Please bear in mind that for example for a date field, the dates must be in "Ymd" format. (2022/12/09).

Hope somebody finds it helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM