最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sort Posts By Custom Field Date?

programmeradmin0浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论