• I am having trouble with the get_children function. I am using the example for “Show the first image associated with the post and re-key the array” in the codex: http://codex.wordpress.org/Function_Reference/get_children

    $args = array(
    	'numberposts' => 1,
    	'order'=> 'DESC',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID,
    	'post_type' => 'attachment'
    	);
    
    $get_children_array = get_children($args,ARRAY_A);  //returns Array ( [$image_ID]...
    $rekeyed_array = array_values($get_children_array);
    $child_image = $rekeyed_array[0];  
    
    $TheImageID = $child_image['ID'];
    $TheImageAttribs = wp_get_attachment_image_src( $TheImageID, 'full' ); // returns an array
    $TheImageURL = $TheImageAttribs[0];

    In most cases, $TheImageURL is the url for the full sized image. However, in SOME cases, the $get_children_array is an empty array, resulting in no value for $TheImageID…

    So, sometimes it works, and sometimes it doesn’t (NOTE: All the posts I’ve tried have an attached image in the post content area, which was uploaded through MEDIA).

    Does anyone know how to fix this?

    I’ve tried this solution, but it doesn’t give any results at all (for any of my posts)
    http://wordpress.org/support/topic/31-get_children-attachment-problem

Viewing 4 replies - 1 through 4 (of 4 total)
  • All the posts I’ve tried have an attached image in the post content area, which was uploaded through MEDIA

    Perhaps but the key question would be – what post were they attached to?

    Thread Starter jabbamonkey

    (@jabbamonkey)

    Huh? Not sure what you are asking.

    I created a post, and I clicked the ADD MEDIA button. And, through that, I inserted an image INTO the post content area.

    I’ve also included the same image as a features image (but clicking the “featured image” link at the bottom of the right column.

    And, now, when I VIEW these posts, the $get_children_array does not have a value. Isn’t the purpose of the code that I used to get the first attachment from within the content of the post?

    Any progress jabbamonkey? I’m running into the same issue.

    Hmm. I think I solved it for my case: I think I’ve misunderstood attachments. If you create a post and upload a NEW image via the media uploader tool that image is attached to that post and will be returned when you query get_children.

    This introduces some wrinkles:
    1. In the case where your new post re-uses an existing image in the media library it isn’t attached to this new post and isn’t returned in get_children($new_post->ID).

    2. In the case where you edit your post to remove the image from the content it doesn’t detach the image. So you still get the image when you query get_children($edited_post->ID).

    If you edit your post and click the media button and then filter your media by ‘uploaded to this post’ you’ll see what is and isn’t directly attached to the post.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_children does not get attached images for SOME of my posts’ is closed to new replies.