【问题标题】:Wordpress query_posts() with an array?带有数组的 Wordpress query_posts()?
【发布时间】:2011-03-15 11:11:35
【问题描述】:

我正在尝试根据数组中包含的多个 ID 来查询帖子。

我的数组(称为 $my_array)如下所示:

Array
(
    [0] => 108
    [1] => 129
    [2] => 145
)

我的查询看起来像这样:

<?php query_posts(array('post__in' => $my_array)); ?>

但是这只是返回一个帖子,帖子具有数组中第一项的 ID (108)。

我的语法错了吗?

【问题讨论】:

  • 请注意,如果您有多个帖子类型的 ID 范围,则可以使用 post_type => 'any'

标签: php wordpress arrays


【解决方案1】:
$args = array(
  'post_type' => 'page',//or whatever type
  'post__in' => array(108,129,145)
  );
query_posts($args);

$arr=array(108,129,145);
$args = array(
  'post_type' => 'page',
  'post__in' => $arr
  );
query_posts($args);

【讨论】:

  • 是否可以重新排序帖子或者我们是否坚持数组的顺序(这似乎是我正在处理的情况)
【解决方案2】:

您始终必须使用post__in 参数设置post_type。所以你的行应该如下所示:

<?php query_posts(array('post_type' => 'post', 'post__in' => $my_array)); ?>

这将使用您在数组中的 ID 查询帖子。

【讨论】:

    【解决方案3】:

    丹尼尔, 我发布了一个答案,尽管你可能已经找到了。我还没有发布 cmets 的声誉,query_posts 支持 WP_Query 的所有参数,包括排序,您可以将 'orderby' => 'title', 'order' => 'ASC' 添加到 query_posts 调用中

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 2012-08-31
      • 2016-10-05
      • 1970-01-01
      相关资源
      最近更新 更多