disinfor

Wordpress Custom Cat SQL Query

Jul 22nd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. <?php
  2.        
  3.             $post_cat = $category->taxonomy; // This will equal a custom category type.
  4.             $post_term = $category->term_id; // This will equal the page's category id.
  5.                 // Sets the global $wpdb
  6.                 global $wpdb;
  7.                 // Starts the db query
  8.                 $query = "
  9.                 SELECT DISTINCT ID, guid, post_parent FROM $wpdb->term_taxonomy, $wpdb->terms, $wpdb->posts p where p.post_type = 'attachment'
  10.                                AND p.post_mime_type LIKE 'image/%'  
  11.                                AND p.post_status = 'inherit'
  12.                                AND p.post_parent IN
  13.                                        (SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = $post_term)
  14.                                AND $wpdb->term_taxonomy.taxonomy = '$post_cat'
  15.                                AND $wpdb->terms.term_id = $post_term
  16.                 ";
  17.            
  18.             $results =  $wpdb->get_results( $query );
  19.  
  20.             if ( $results ) {
  21.             shuffle($results);
  22.                     foreach ( (array) $results as $image ) {
  23.                         $url = get_attachment_link($image->post_parent);
  24.                         $thumb = wp_get_attachment_image( $image->ID, 'thumbnail', false );
  25.                         $alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
  26.                         echo '<li style="display:inline-block;"><a href="' . get_permalink($image->post_parent) .'">' . $thumb . '</a></li>'."\n";
  27.                     }
  28.                 }
  29.         ?>
Advertisement
Add Comment
Please, Sign In to add comment