Reputation: 10056
I have a custom metabox, where I store the date in plain text like this: dd/mm/yy
and now I need to order the posts by that date.The custom field is called event_informations_date
<?php
$args = array( 'post_type' => 'Event','posts_per_page' => 1000 );
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Upvotes: 0
Views: 1670
Reputation: 70
meta_key => 'event_informations_date',
orderby => 'meta_value_number',
order => 'DESC'
Upvotes: 0
Reputation: 3274
You'll want to add
meta_key => 'event_informations_date',
orderby => 'meta_value',
order => 'DESC'
However, because you are storing as plain text you will get a lexicographic ordering not a chronological ordering. So for example, you'll get
23/01/2012
22/02/2012
15/12/2011
10/05/2013
which is a random date order. I'm not sure whether you can overcome that unless you store the date as YYYY-MM-DD which lexicographically sorts in date order.
Upvotes: 2
Reputation: 818
First, save the date as a timestamp. then you can do the following in your WP_Query
$query = new WP_Query(
array (
'orderby' => 'meta_value_num',
'meta_key'=> ' event_informations_date'
)
);
Upvotes: 0