查询一篇文章使用 p,查询一个页面使用 page_id。需要按 slug 查文章使用 name,按页面路径使用 pagename。
$query = new WP_Query( array(
'post_type' => 'post',
'post__in' => array( 12, 7, 31 ),
'orderby' => 'post__in',
'ignore_sticky_posts' => true,
) );
多个对象使用 post__in,希望按传入 ID 顺序输出时配合 orderby => post__in。需要同时查询文章和页面,可把 post_type 设为 array( 'post', 'page' )。
一个容易踩的坑是把空数组传给 post__in:它可能不会返回空结果。因此查询前要先判断 ID 列表是否为空。外部输入用 wp_parse_id_list() 清理,自定义循环结束后调用 wp_reset_postdata()。
