I have the following code displaying posts from a custom post type. However, I need them displayed in date order from information put in a custom field, can this be done?
I try this code :-
$course = new WP_Query(
array (
'posts_per_page' => '10' ,
'post_type' => 'course',
'orderby' => 'meta_value_num',
'meta_key' => 'course_date',
'order' => 'DESC'
)
);
But it shows me data like this :-
30-12-2014
28-9-2014
26-10-2014
19-12-2014
It must be like :-
30-12-2014
19-12-2014
26-10-2014
28-9-2014
What can I do?!
I have the following code displaying posts from a custom post type. However, I need them displayed in date order from information put in a custom field, can this be done?
I try this code :-
$course = new WP_Query(
array (
'posts_per_page' => '10' ,
'post_type' => 'course',
'orderby' => 'meta_value_num',
'meta_key' => 'course_date',
'order' => 'DESC'
)
);
But it shows me data like this :-
30-12-2014
28-9-2014
26-10-2014
19-12-2014
It must be like :-
30-12-2014
19-12-2014
26-10-2014
28-9-2014
What can I do?!
Share Improve this question edited Aug 17, 2014 at 10:10 Gabriel 2,24810 gold badges22 silver badges24 bronze badges asked Aug 17, 2014 at 9:59 user3704392user3704392 31 silver badge5 bronze badges 1- There is a plugin i wrote that does all kind of post/page ordering, also by custom fields. maybe you can use it as well: wordpress/support/plugin/wp-order-by – user82667 Commented Nov 20, 2015 at 12:33
2 Answers
Reset to default 2Use meta_value
instead of meta_value_num
<?php $course = new wp_query( array ( 'posts_per_page' => '10' ,
'post_type' => 'course',
'orderby' => 'meta_value',
'meta_key' => 'course_date',
'order' => 'DESC'
)
);
?>
$course = new WP_Query(
array (
'posts_per_page' => '10' ,
'post_type' => 'course',
'orderby' => 'meta_value_num date',
'meta_key' => 'course_date',
'order' => 'DESC'
)
);
Just add a 'date' in after the 'meta_value_num' in 'orderby' as per reference https://wordpress/support/topic/sorting-by-custom-field-date-and-post-date/ and I also verify it by use of this code.