【问题标题】:Wordpress WP_Query for multiple IDs returns null多个 ID 的 Wordpress WP_Query 返回 null
【发布时间】:2014-07-20 18:36:11
【问题描述】:

我有以下数组:

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,它们是正确的。

编辑:我已将此查询放在加载到页脚中的模块的末尾。不知道重要不重要:

【问题讨论】:

  • 如果 $ids_array 已经是一个数组,为什么要把它放到另一个数组中?你的代码不应该只是$the_query = new WP_Query( array("post__in"=&gt;$ids_array) ); 吗?

标签: php wordpress


【解决方案1】:

您忘记添加while ( $the_query-&gt;have_posts() ) : $the_query-&gt;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; 
  ?>

编辑

我认为这里的根本问题不是这个自定义查询,因为这个自定义查询有效。从您的 cmets 看来,您在同一页面上使用了另一个自定义查询。

我不知道第一个查询的代码是什么样的,但是这里有一些故障排除点你需要去看看

  • 很可能您没有在第一次查询时重置 postdata。这将破坏您的第二个查询。当您使用WP_Queryget_posts 运行自定义查询时,wp_reset_postdata 非常重要。在您第一次使用WP_Query 之后,检查您是否使用了wp_reset_postdata。您的第一个查询应与我的答案中的格式相同

  • 您应该为WP_Query 的每个实例使用不同的变量。例如$variable1 = new WP_Query( YOUR ARGUMENTS ) 用于第一个实例,$variable2 = new WP_Query( YOUR ARGUMENTS ) 用于您的第二个实例

【讨论】:

  • 它不起作用。可能是因为以前使用过 WP_Query 吗?因为当我使用带有“p”而不是“post__in”的直接 ID 时,它仍然返回 note?
  • 查看更新。根据您的评论添加了更多信息
猜你喜欢
  • 2015-12-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
相关资源
最近更新 更多