我已经看了好几个小时了,但还是没法让它发挥作用。
$newsItems2 = get_posts([
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => 3,
"orderby" => "date",
"order" => "DESC",
\'meta_query\' => [
[
\'key\' => \'news__type\',
\'value\' => \'general\',
\'compare\' => \'=\'
]
]
]);
查询正常,但拒绝在发布日期排序。我在这里俯瞰什么?(当我删除meta\\u查询部分时,它工作得很好!我是在尝试做一些不可能的事情吗?
顺便说一下,这段代码运行在分类页面上,但这不应该是一个问题,对吗?
我已经对此进行了进一步的研究,并将其转换为具有相同参数的WP\\u查询,得到了相同的结果。这不是排序!这是它正在执行的反SQL,实际上它完全忽略了orderby子句。为什么?
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1 AND ( ( wp_postmeta.meta_key = \'news__type\' AND wp_postmeta.meta_value = \'general\' ) )
AND wp_posts.post_type = \'post\'
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' OR wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order ASC LIMIT 0, 3
SO网友:pro4soft
Hello bro try this will work !
$newsItems2 = get_posts( [
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => 3,
"orderby" => "date",
"order" => "DESC",
"meta_key" => "news__type",
"meta_value" => "general",
] );