2016-08-14 58 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,並獲得了我正在尋找的結果。