2016-08-14 56 views
0

下面的代码返回即使我知道有一些具有给定的post_parent ID职位空数组:get_posts通过post_parent返回空数组

$args = array(
    'post_parent'  => 211167, 
); 

$posts_array = get_posts($args); 
$data->subfolder_count = sizeof($posts_array); 

我已经使用CLI尝试,并得到相同的结果

(12:05:29) mu-plugins/$ wp post get 211169 
+-----------------------+--------------------------------------+ 
| Field     | Value        | 
+-----------------------+--------------------------------------+ 
| ID     | 211169        | 
| post_author   | 12449        | 
| post_date    | 2016-08-14 11:21:26     | 
| post_date_gmt   | 2016-08-14 16:21:26     | 
| post_content   |          | 
| post_title   | SubFolder Two      | 
| post_excerpt   |          | 
| post_status   | publish        | 
| comment_status  | closed        | 
| ping_status   | closed        | 
| post_password   |          | 
| post_name    | subfolder-two      | 
| to_ping    |          | 
| pinged    |          | 
| post_modified   | 2016-08-14 11:21:26     | 
| post_modified_gmt  | 2016-08-14 16:21:26     | 
| post_content_filtered |          | 
| post_parent   | 211167        | 
| guid     | [redacted]       | 
| menu_order   | 0         | 
| post_type    | [redacted]       | 
| post_mime_type  |          | 
| comment_count   | 0         | 
+-----------------------+--------------------------------------+ 

(12:05:40) mu-plugins/$ wp post list --post_parent=211167 
+----+------------+-----------+-----------+-------------+ 
| ID | post_title | post_name | post_date | post_status | 
+----+------------+-----------+-----------+-------------+ 
+----+------------+-----------+-----------+-------------+ 

如果你能告诉我我做错了什么,我会很感激。

回答

0

get_posts()默认情况下仅提取帖子类型为post的帖子。既然你编辑了post_type我认为这是一个自定义的帖子类型。将正确的post_type添加到您的参数中。

+0

谢谢杰拉德。你是绝对正确的。我得出了同样的结论。 –

0

它看起来像post_type是必需的。我将我的代码更改为包含post_type而不是仅包含post_parent,并获得了我正在寻找的结果。