0

I needed to pass an image to a Pinterest plugin in posts without a Featured Image so I wrote a little function to get the id of the first picture in a post. This code is in the loop-single.php where the sharing buttons are located.

if ( has_post_thumbnail()) {
    $thumb_for_pinterest = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
} else {
    $attachments = get_children( array(
        'post_parent'    => get_the_ID(),
        'post_type'      => 'attachment',
        'numberposts'    => 1, // show all -1
        'post_status'    => 'inherit',
        'post_mime_type' => 'image',
        'order'          => 'ASC',
        'orderby'        => 'menu_order ASC'
    ) );

    foreach ( $attachments as $attachment_id => $attachment ) {
        $thumb_for_pinterest = wp_get_attachment_image_src($attachment_id, 'full' );
    }
}

My question is this: why do I need to use a foreach to get the attachment_id?
Specifically, what is this line doing?

foreach ( $attachments as $attachment_id => $attachment ) {...

I believe an associative array is being returned with only 1 object in it. However, when I tried to just pop the object out of the array and use it I got lots of errors. Only by putting it in the foreach could I get the image id out.

Is there an easier way to get the attachment_id of the first image in a post?

I have read a lot of questions and googled this quite extensively, but couldn't understand it completely.

1 Answer 1

3

get_children will return

an associative array of posts [...] with post IDs as array keys, or an empty array if no posts are found.


This statement $attachment_id => $attachment is the same as the more commonly used $key => $value.

Using this command echo '<pre>'.print_r($attachments,true).'</pre>'; we get the following:
resumed version of a get_children with numberposts => 2

Array
(
    [83] => stdClass Object
        (
            [ID] => 83
            [post_author] => 1
            [post_date] => 2012-06-22 05:06:22
            [post_content] => 
            [post_title] => barajas
            [post_excerpt] => 
            [post_status] => inherit
            [post_name] => barajas-4
            [post_modified] => 2012-06-22 05:06:22
            [post_modified_gmt] => 2012-06-22 05:06:22
            [post_parent] => 28
            [guid] => http://wp00.dev/wp-content/uploads/barajas3.jpg
            [menu_order] => 0
            [post_type] => attachment
            [post_mime_type] => image/jpeg
        )

    [180] => stdClass Object
        (
            [ID] => 180
            [post_author] => 1
            [post_date] => 2012-06-30 04:18:06
            [post_content] => 
            [post_title] => Yuri Atom_00281
            [post_excerpt] => 
            [post_status] => inherit
            [post_name] => yuri-atom_00281
            [post_modified] => 2012-06-30 04:18:06
            [post_parent] => 28
            [guid] => http://wp00.dev/wp-content/uploads/Yuri-Atom_00281.jpg
            [menu_order] => 0
            [post_type] => attachment
            [post_mime_type] => image/jpeg
        )

)

So, [83] and [180] are the $key and stdClass Object is their $value.
I think when calling get_children a foreach is almost mandatory, and this will be clear next...


If you use get_posts with exactly the same arguments, the result will be exactly the same except for one difference, the returned array will have numeric sequential keys:

Array
(
    [0] => stdClass Object
        (
            [ID] => 83
            [post_author] => 1
            [post_date] => 2012-06-22 05:06:22
        )

    [1] => stdClass Object
        (
            [ID] => 180
            [post_author] => 1
            [post_date] => 2012-06-30 04:18:06
        )
)

All that being said, if you use get_posts and numberposts => 1, you can substitue the foreach block for:

$thumb_for_pinterest = wp_get_attachment_image_src($attachments[0]->ID, 'full' );

With get_children we don't know what's the $key number, as it is the same as the Attachment ID, hence the foreach...

But with a regular numeric key (0, 1, 2, 3... etc), you can drop the foreach and call the first element of the array directly ($attachments[0]) and its object element (->ID).

1
  • 1
    Thanks. One of the best answers I have seen. Make so much more sense. Commented Jul 3, 2012 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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