1

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..

3

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.

  • Okay, that clarifies it. Thank you. I'm curious: Can you provide an example where this behavior is benefitial? And why the hell is such an important concept nowhere to be found in the Wordpress Docs? Back2Topic: just to make sure we're on the same page: You suggest to get_post_galleries($post->ID, false) data and manually go through each index and add the IDs string to a $toCount var, to then explode and count it? And: How can I add single images without galleries to the count, if I some day decide to support them? – kater louis Dec 4 '17 at 16:35
  • If you want to be really sure you get all the images, you would have to make a filter for the_content and count all occurences of <img. – cjbj Dec 4 '17 at 16:37
  • So the only way, you say, is going through the contents rendered html .. Not so cool when I want the option to ignore duplicates. Wow. Wouldn't have thought counting a posts number of images is so exotic, that Wordpress doesn't have a native solution for it.. Is there a less performance demanding way then Regex to count <imgs? – kater louis Dec 4 '17 at 16:46
  • 1
    You can use get_the_content and apply do_shortcode to the result to get the html with shortcodes expanded. – cjbj Dec 4 '17 at 17:21
  • 1
    And two answer your previous comment: since you can type anything you like in the content field, WP would have no other option but to apply regex to the content in order to find img-tags. Actually, that is what it does to find shortcode-tags. – cjbj Dec 4 '17 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.