【问题标题】:Can't retrieve data from WP_Query无法从 WP_Query 检索数据
【发布时间】:2013-06-20 14:14:04
【问题描述】:

我想使用 ID 数组检索帖子。我有这个数组

Array ( [0] => 40 [1] => 25 )

这是我的代码:

$query = WP_Query(array("p" => myCourseId()));

但它不起作用。什么错误?

新代码

$ids = myCourseId();

// The Loop

$query = WP_Query(array("post__in" => $ids));

myCoureID() 像我写的第一个一样返回一个数组

【问题讨论】:

标签: php wordpress


【解决方案1】:

你可以尝试用 get_posts 做同样的事情,像这样:

$ids = array(40,25);
$args = array(                        
    'numberposts' => -1,
    'posts_per_page' => -1,    
    'orderby'=>'post__in',                        
    'post__in'=>$ids
 ); 
$query = get_posts($args);

【讨论】:

  • 它不起作用!我不明白为什么:(这是我的代码$ids = myCourseId(); $args = array( 'numberposts' => -1, 'posts_per_page' => -1, 'orderby'=>'post__in', 'post__in'=>$ids ); $query = get_posts($args);
  • 你试过 print_r($query) 吗?真的是空的吗?尝试添加一个 'post_type' 参数,如下所示: $args = array( 'numberposts' => -1, 'posts_per_page' => -1, 'post_type' => 'post', 'orderby'=>'post__in' , 'post__in'=>$ids );而你的函数 myCourseId() 必须返回一个像这样的数组 array(40, 25) 而不是这样的 ( [0] => 40 [1] => 25 )
  • 啊好的,问题出在数组上。非常感谢!最后一个问题,我如何将这样的数组( [0] => 40 [1] => 25 )转换为兼容的数组?
  • $yourArray = array(40,25);
【解决方案2】:

试试:

$myArrayOfIds = array(40, 25);
$query = WP_Query(array("post__in" => myArrayOfIds));

【讨论】:

  • 不起作用 $ids = myCourseId(); // 循环 $query = WP_Query(array("post__in" => $ids));
  • 请更具描述性 - 我们正在努力提供帮助。什么不起作用?错误是什么?您应该先尝试不使用额外的myCourseId() 函数来简化问题。如果您的 WP_Query(array("post__in" => $ids)); 行不起作用,那么我敢打赌您的 $ids 变量不是数组,或者是关联数组。
  • 没有 myCourseId() 也无法工作。如果我写 $query = new WP_Query(array('post__in' => array(40)); 它不会显示任何内容
猜你喜欢
  • 2019-09-24
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多