我有以下数组:
array (12,15,21,32,33);
然后我使用以下查询来获取(以上id的)帖子:
$the_query = new WP_Query( array("post__in"=>$ids_array) ); // edited
while($the_query->have_posts())
{
$the_query->the_post(); // added on edit, but still does not work
the_title()."<br />";
}
但是我什么也没得到,没有错误,也没有中断。我已经检查了id,它们是正确的。
编辑:我把这个查询在加载到页脚的模块的末尾。我不知道它是否重要:
您忘记添加while ( $the_query->have_posts() ) : $the_query->the_post();
了。
你只是在检查你是否有帖子,但没有做任何进一步的
$ids_array = array (12,15,21,32,33);
$the_query = new WP_Query( array('post__in'=>$ids_array) );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2></br>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
编辑
我认为这里的潜在问题不是这个自定义查询,因为这个自定义查询工作。从你的评论看来,你在同一页面上使用了另一个自定义查询。
我不知道第一个查询的代码是什么样子的,但是这里有一些故障排除点,您需要去看看
很可能您没有在第一次查询时重置postdata。这将中断您的第二个查询。当您使用
WP_Query
和get_posts
运行自定义查询时,wp_reset_postdata
非常重要。检查在第一次使用WP_Query
实例后是否使用了wp_reset_postdata
。您的第一个查询应该与我的答案 中的格式相同您应该为
WP_Query
的每个实例使用不同的变量。例如,$variable1 = new WP_Query( YOUR ARGUMENTS )
用于第一个实例,$variable2 = new WP_Query( YOUR ARGUMENTS )
用于第二个实例