You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP_Query使用post__in查询时不按指定顺序排序怎么解决?

问题根源

你代码中的排序参数名存在拼写错误,WordPress WP_Query 中指定排序规则的参数为 orderby,你写的order_by多了一个下划线,这是post__in顺序不生效的核心原因。

修正后的代码

$gridItem1 = get_field('large_grid_item');
$gridItem2 = get_field('second_grid_item');
$gridItem3 = get_field('third_grid_item');

$args = array(
    'posts_per_page' => 3,
    'post_type' => array('post', 'page','programme'),
    'suppress_filters' => true,
    'post__in' => array($gridItem1, $gridItem2, $gridItem3),
    'orderby' => 'post__in', // 修正参数名,删除中间下划线
    'order' => 'ASC' // 可选,显式指定顺序和post__in数组传入顺序保持一致
);

$the_query = new WP_Query( $args );

额外说明

  • 查询前调用的wp_reset_query()没有实际作用,可以删除。该方法作用是重置WordPress主查询,一般用于自定义主查询逻辑后重置状态,不需要在自定义WP_Query执行前调用。
  • 自定义查询循环执行完毕后,建议调用wp_reset_postdata()恢复全局$post对象,避免影响页面后续其他查询逻辑。

内容的提问来源于stack exchange,提问作者Jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 07:54:04