我试图在我的网站上显示受欢迎的帖子,这些帖子是由浏览量和评论数混合选择的。因为视图总是远远多于评论,所以我将评论数乘以X,但它似乎没有达到我所希望的效果。查询结果仅按元值“Views”排序。我假设这是因为我需要计算视图+注释计数*X的总和,然后按它排序。。。但我不知道如何通过仅使用get\\u posts()来实现这一点。有人能给我指出正确的方向吗?:)
这是我的密码
$home_top_news = get_posts(
array(
\'post_type\' => array(\'post\',\'reviews\'),
\'post_status\' => \'publish\',
\'numberposts\' => \'15\',
\'meta_key\' => \'views\',
\'orderby\' => array(
\'meta_value_num\' => \'DESC\',
\'(comment_count * 10)\' => \'DESC\',
),
\'date_query\' => array(
array(
\'after\' => \'12 days ago\'
)
)
)
);
SO网友:shadylane
为此,需要使用$wpdb对象。
SQL看起来像这样(完全未经测试,需要添加时间条件)
SELECT ID, post_date, post_title, post_content, (meta_value + (comment_count * 10)) AS rank
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta pm
ON (ID = pm.post_id AND pm.meta_key = \'views\')
WHERE (post_type = \'post\' OR post_type = \'reviews\')
AND post_status = \'publish\'
ORDER BY rank DESC
LIMIT 0,15;