SHARE
TWEET

WP: get images from unique parents

ulfben Dec 30th, 2011 142 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.    A function I use to quickly get a total of $limit-images (attachments), each from a unique parent post.
  4.    Heavily used over at http://game.hgo.se/
  5. */
  6. function ulfben_get_post_images(
  7.         $number_of_posts = 5,
  8.         $category_filter = '66', //comma delimited list of category IDs
  9.         $order_by = 'RAND()', //post_date DESC or what have you.
  10.         $size = 'thumbnail' //'thumbnail', 'medium', 'large' or 'full' or an array of width and height, e.g. array(32,32)  
  11.  ){
  12.         global $wpdb;
  13.         $limit = (intval($number_of_posts) <= 0) ? 1 : $number_of_posts;
  14.         $images = $wpdb->get_results(
  15.                 "SELECT SQL_SMALL_RESULT DISTINCT $wpdb->posts.post_parent, $wpdb->posts.ID
  16.                 FROM $wpdb->posts
  17.                 WHERE $wpdb->posts.post_parent IN (
  18.                                 SELECT SQL_SMALL_RESULT DISTINCT $wpdb->posts.ID
  19.                         FROM $wpdb->posts              
  20.                                 LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  21.                                 LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  22.                         WHERE $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN({$wpdb->escape($category_filter)})
  23.                         AND $wpdb->posts.post_type = 'post'
  24.                         AND $wpdb->posts.post_status = 'publish'
  25.                         AND $wpdb->posts.post_password = ''            
  26.                 )              
  27.                 AND $wpdb->posts.post_type = 'attachment'
  28.                 AND $wpdb->posts.post_mime_type IN ('image/jpeg', 'image/gif', 'image/png')
  29.                 ORDER BY {$order_by}
  30.                 LIMIT {$limit};"
  31.         );     
  32.         $selection = array();
  33.         foreach($images as $img){
  34.                 $imgurl = wp_get_attachment_image_src($img->ID, $size);
  35.                 if($imgurl === false){continue;} //the image doesn't exist?    
  36.                 $img_width = $imgurl[1];
  37.                 $img_height = $imgurl[2];
  38.                 $imgurl = $imgurl[0];          
  39.                 $selection[] = array(
  40.                         'post_url' => get_permalink($img->post_parent),
  41.                         'post_title' => wp_specialchars(get_the_title($img->post_parent)),
  42.                         'post_id' => $img->post_parent,
  43.                         'img_id' => $img->ID,
  44.                         'img_src' => $imgurl,
  45.                         'width' => $img_width,
  46.                         'height' => $img_height
  47.                 );     
  48.         }
  49.         return $selection;
  50. }
  51. function ulfben_print_post_images(
  52.         $number_of_posts = 5, //number of posts
  53.         $image_attributes = '',                      
  54.         $category_filter = '66', //comma delimited list of category IDs
  55.         $order_by = 'RAND()', //{$wpdb->posts}.post_date DESC or what have you.
  56.         $size = 'thumbnail' //'thumbnail', 'medium', 'large' or 'full' or an array of width and height, e.g. array(32,32)
  57.         ){                                     
  58.     $images = ulfben_get_post_images($number_of_posts, $category_filter, $order_by, $size);
  59.         foreach($images as $image){
  60.                 echo "<a href='{$image['post_url']}' title='".esc_attr($image->post_title)."'><img src='{$image['img_src']}' alt='{$info}' {$image_attributes}/></a>"; 
  61.         }
  62. }
  63. ?>
RAW Paste Data
Top