Sort Posts By Custom Field Date?

I have the following code displaying posts from a custom post type. However, I need them displayed in date order from in

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
Add a comment  | 

2 Answers 2

Reset to default 2

Use 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745200380a4616293.html

相关推荐

  • Sort Posts By Custom Field Date?

    I have the following code displaying posts from a custom post type. However, I need them displayed in date order from in

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信