Is there a function that will let me get the post id of the first child (of the same post type, not attachment)? I could always just run a loop via wp_query but that seems like overkill. :/
The goal here is to build a function that displays the first attached image. My function goes through the post_thumbnail and the attached images of the parent post fine, but stops working when the only images reside in child posts.
Edit: Here is my current working function. It works, but its not pretty. How can I clean it up?
/****************************************/
/* Grab first image of a post
/****************************************/
function surfbird_grab_album_image() {
global $post, $posts;
$albumtitle = get_the_title();
if(has_post_thumbnail()) :
return the_post_thumbnail('small');
else :
//Grab image if parent post has one
$args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'numberposts' => 1,
);
$attachments = get_posts($args);
if ($attachments) :
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'small', false, false);
echo apply_filters( 'the_title', $albumtitle );
}
else :
//Search the child posts and grab the first image we find
$albumgroups = get_posts( array( 'post_type' => 'album', 'post_parent' => get_the_ID(), 'orderby' => 'menu_order', 'numberposts' => -1 ) );
foreach( $albumgroups as $post ) : setup_postdata($post);
$postID = $post->ID;
$args = array(
'numberposts' => 1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment'
);
$attachments = get_children( $args );
if ($attachments) {
foreach($attachments as $attachment) {
return wp_get_attachment_image($attachment->ID, 'small', false, false);
}
}
endforeach;
wp_reset_query();
endif;
endif;
wp_reset_postdata();
}