SO网友:Stephen S.
在做了更多的研究之后,我找到了一种方法来做到这一点,尽管它似乎不是一个很好的解决方案,因为它需要很长的时间才能运行,因为它基本上只是在每个帖子中循环使用get_post_gallery()
.
也许有更好的方法,也许其他人以前也想过。
// Get All Post IDs
$post_image_query = new WP_Query(
array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\'
)
);
$selected_posts = $post_image_query->posts;
$image_list_array = array();
// Loop through all posts and grab the image gallery
foreach($selected_posts as $post) {
$post_gallery = get_post_gallery( $post, false );
//retrieve individual ids from string
$image_ids = explode(\',\', $post_gallery[\'ids\']);
//build the array
foreach($image_ids as $image_id) {
$image_list_array[] = $image_id;
}
}
// Get All Attachments of Posts from $post_image_query
$the_query = new WP_Query(
array(
\'post_type\' => \'attachment\',
\'post_status\' => \'inherit\',
\'orderby\' => \'post_parent__in\',
\'post_parent__in\' => $image_list_array
)
);