我正在尝试做类似的事情。Ordering posts by day (not time) AND meta value
我想列出按日期(而不是时间)排序的博客文章,然后在这一天内,我想按自定义字段my\\u post\\u rating对它们进行排序。这只是输出按评级排序的帖子。
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => \'-1\',
\'meta_key\' => \'my_post_rating\',
\'orderby\' => \'meta_value_num\',
\'ignore_sticky_posts\' => 1,
);
$new_query = new WP_Query( $args );
我也尝试过建议的解决方案,但没有奏效。请提出问题所在?
Edit // 已添加Var转储
字符串(344)“选择wp\\u posts。*从wp\\u posts内部加入wp\\u postmeta ON(wp\\u posts.ID=wp\\u postmeta.post\\u ID),其中1=1,wp\\u posts.post\\u type=\'post\'和(wp\\u posts.post\\u status=\'publish\'或wp\\u posts.post\\u status=\'private\')和(wp\\u posts.meta\\u key=\'my\\u post\\u rating\')GROUP BY wp\\u posts.ID ORDER BY wp\\u postmeta.meta\\u value+0 DESC”
最合适的回答,由SO网友:Tomás Cot 整理而成
add_filter(\'posts_orderby\', \'posts_orderby\');
function posts_orderby($orderby_for_query) {
global $wpdb;
$prefix = $wpdb->prefix;
$orderby_for_query = "LEFT(" . $prefix . "posts.post_date, 10) DESC, " . $orderby_for_query;
return $orderby_for_query;
}
这将生成以下查询:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\') AND (wp_postmeta.meta_key = \'my_post_rating\' ) GROUP BY wp_posts.ID ORDER BY LEFT(wp_posts.post_date, 10) DESC, wp_postmeta.meta_value+0 DESC