Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: query all images in a posts media library

Howdy! I am looking for a way to list all of the image files in a posts' media library.

What I mean by this is if a file has been uploaded while creating or editing a post, is the file associated with the post in some way, and can I create a list from this data.

I think that the next_image_link() / previous_image_link(); template tag is as close as I have found.

I think that this should be close:

$query = 'SELECT * FROM `wp_posts` 
WHERE `post_parent` = \''.$_GET['post_id'].'\' 
AND  `post_mime_type` = \'image/jpeg\' 
ORDER BY `menu_order` ASC';

thanks.

like image 892
superUntitled Avatar asked Feb 04 '23 09:02

superUntitled


1 Answers

In wordpress terminology, every image you uploaded to particular post is called attachment. To list all attachment, you can use get_children() function:

$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=10' );

$counter=0;
foreach( (array) $images as $attachment_id => $attachment )
{
   $counter++;
   echo "<a href='".wp_get_attachment_link( $attachment_id ) . "'>image $counter</a><br />";
}

The algorithm is something like that.

like image 122
ariefbayu Avatar answered Feb 24 '23 04:02

ariefbayu