get_children() not working properly – Only on one post?

Client wants to show how many images await the reader in a post. I say: easy peazy. Now I realize: I lied.

The system only uses galleries, even for single pictures.

$images = get_children(array(
    "post_parent" = $post-ID, // $post-ID gives the right ID.
    "post_status" = "any",
    "post_type" = "attachment",
    "post_mime_type" = "image"
));

count($images); // only post with id 12 shows correctly 14 entries

In one post (ID 12, an eaaarly test entry) $images is filled with the correct amount of entries. In every other post I tested $images is wrongfully empty.

I even copied the exact same text (from the "Text"-Tab of TinyMCE, not "Visual") to post with ID 298. Still empty $images. Same custom fields. Both have a thumbnail (even if I delete it, no change) – Same taxonomy-settings..

Topic children get-children get-posts images Wordpress

Category Web


The get_children function retrieves posts (an upoaded image is also a post) that are dependent on the post with a certain ID. An image becomes a child of a post when it is uploaded while editing that post. It does not become a child of a post when it is reused.

So if you are writing post A and upload image B, B will become a child of A. If you then write post C and include image B in a gallery, B will not become a child of C. If you use get_children on C, you will find nothing.

What you are looking for is get_post_galleries.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.