按自定义字段对帖子排序 - 数字和字符串

时间:2013-10-09 09:17:22

标签: wordpress sorting wordpress-theming

所以我有一个自定义字段,以这种格式提供纯文本输出:dd。 MM yy(2013年10月9日)。这是我使用的循环:

query_posts(array('category_name'=>'somecategory', 'posts_per_page'=>'5','paged' => get_query_var('paged')));
while (have_posts()) : the_post();

//stuff
the_field('mydate'); //the output is: dd. MM yy
//stuff

endwhile;
wp_reset_query();
//paginate_links code...

如果我将此添加到query_posts:

'meta_key' => 'mydate', 'orderby'=>'mydate'

我的帖子只按天和年排序,看起来它似乎无法读取月份,这是一个字符串。这种分类有点无用。如果该网站是我的我会改变日期格式,它将被解决,但我正在为某人做这个网站,设计师已经在photoshop中绘制这种日期格式,所以它必须是这样的。那么我怎样才能使这种排序正常工作呢?

1 个答案:

答案 0 :(得分:0)

在query_posts之前添加以下代码:

add_filter('posts_where', 'filter_orderby');
 function filter_orderby( $where = '' ) {
    $where .= "  AND $wpdb->postmeta.meta_key = 'mydate' ORDER BY mydate DESC";
    return where;
}

希望有所帮助