Question

I need to retrieve the URLs for the images inside a gallery, what's the appropriate SQL query for this, based on the post ID?

I currently have something like the following which I found on via google, but I don't know how to access the global WP variable from outside Wordpress - what do I need to do?

function getGallery($id)
{
 global $wpdb;

 //SQL query to retrieve all attachment of mime type image/jpeg from the given post
 $querystr = "
     SELECT ID, post_name, guid, meta_value
     FROM $wpdb->posts wposts
     INNER JOIN $wpdb->postmeta meta ON meta.post_id = wposts.ID AND meta.meta_key = '_wp_attachment_metadata'
     WHERE wposts.post_type = 'attachment'
     AND wposts.post_parent = '".$id."'
     AND wposts.post_mime_type = 'image/jpeg' 
 ";

 //get result set from the query
 $pictures = $wpdb->get_results($querystr, ARRAY_A);

 //return resultset
 return $pictures;
}
Was it helpful?

Solution

I believe the function is wp_get_attachment_url(). There also is one for thumbnails. See wp-includes/media.php.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top