开发者

Order posts by custom post field

Asked this on another thread and thinking I had gotten a solution marked it as solved.

I have a custom post type called courses and have a field within that called pdf-order that basically represents where in the list the post is going to show. I am trying to get it to sort but am having no luck.

This is my code :

$course_loop = new WP_Query( array( 'post_type' => 'course',
                                    'posts_per_page' => 30,
 开发者_如何学C                                   'meta_key' => 'pdf-order',
                                    'orderby'=> 'meta_value',
                                    'order' => 'desc' ) );

Can anyone shed some light on this for me?

Thanks


Your original query is correct.
But!

I also discovered this today in the codex

I assume your CF 'pdf-order' are numbers.
But wp orders them alphabetically.
so if we have '1,2,3,10,11'
it will order : 1, 10 , 11, 2, 3
instead of what you would expect (1,2,3,10,11)

So add starting zero's so it would become: '001, 002, 003, 010, 011'

I hope this helps


If you are ordering your posts with numeric values in the Custom Field, you can use meta_value_num in place of meta_value.

$course_loop = new WP_Query( array( 'post_type' => 'course',
                                'posts_per_page' => 30,
                                'meta_key' => 'pdf-order',
                                'orderby'=> 'meta_value_num',
                                'order' => 'desc' ) );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜