Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a bunch of image posts and I'd like to get the ID of those images I didn't set a featured image so I can use wp_get_attachment_image_src() to get the urls of various sizes of those images.

This is what I created:

$img_posts = get_posts( array(
    'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field'    => 'slug',
          'terms'    => 'post-format-image',
          'operator' => 'IN'
        )
    )
) );

foreach( (array) $img_posts as $post ) {
    setup_postdata( $post );
   $imgID = get_img_id();
    //img sources
    $thumb = wp_get_attachment_image_src($imgID, 'thumb');
    $reg = wp_get_attachment_image_src($imgID, 'large');
    $big = wp_get_attachment_image_src($imgID);
}

The get_img_id function on the 6th to lastline, which is probably the source of all of my problems, looks like this:

function get_img_id() {
        global $post;
        $id = intval( $post->ID );
        $imgID = get_children( array(
                'post_parent' => $id,
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'order' => 'ASC',
                'numberposts' => 1
            ) );

    return $imgID;
}

When I do this I get no errors or output. If I add something like the_content to the array instead it functions properly.

UPDATE: Per @TheDeadMedic's suggestion I tried this:

$img_posts = get_posts( array(
    'cat' => '2',
    'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field'    => 'slug',
          'terms'    => 'post-format-image',
          'operator' => 'IN'
        )
    )
) );
foreach( (array) $img_posts as $post ) {
    setup_postdata( $post );
    $img_id = get_img_id();
    var_dump ($img_id);
}

This returned

array
    empty

Once per post queried.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Because $imgID is an array of post objects. Instead of:

return $imgID;

You need:

return $imgID ? key( $imgID ) : 0;

Since get_children returns an array of posts indexed by their ID, you can simply use key to return the value of the first one.

share|improve this answer
    
Thanks for your answer. I changed the line in my get_img_id function as you suggested. To test I did $imgID = get_img_id(); echo $imgID; I got the a zero, followed by the name of the post. I tried different numbers but I just got that number and the name of the post:( –  JPollock Jun 23 '13 at 22:11
    
Go back to what you had before, then var_dump( $imgID ) & update your question with what you get. –  TheDeadMedic Jun 24 '13 at 0:52
    
I updated the question with the result. –  JPollock Jun 24 '13 at 3:37
    
Okay, so the problem is that there aren't actually any images attached to the post (at least not in the database). Have you tried creating a new post & uploading & attaching another image? –  TheDeadMedic Jun 24 '13 at 5:07
    
Facepalm! Why didn't I think of that basic troubleshooting step?? Your answer (return $imgID ? key( $imgID ) : 0;) works with the new post. –  JPollock Jun 24 '13 at 6:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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