term_taxonomy_id ) $terms_query = sprintf( 'SELECT term_id, slug FROM %1$s WHERE ( slug IN ( %2$s ) ) ORDER BY term_id', $wpdb->terms, $slugs ); $_terms_taxonomy_query = sprintf( 'SELECT term_taxonomy_id, term_id FROM %1$s WHERE ( taxonomy=\'attachment_category\' ) ORDER BY term_id', $wpdb->term_taxonomy ); $query = sprintf( 'SELECT tt.term_taxonomy_id, t.term_id, t.slug FROM ( %1$s ) as tt JOIN ( %2$s ) as t ON ( tt.term_id = t.term_id ) ORDER BY term_taxonomy_id', $_terms_taxonomy_query, $terms_query, $slugs ); $results = $wpdb->get_results( $query ); self::$gallery_terms = array(); foreach ( $results as $result ) { self::$gallery_terms[ absint( $result->term_taxonomy_id ) ] = $result->slug; } unset( $results ); self::$gallery_terms = array_flip( self::$gallery_terms ); // Build an array of ( term_taxonomy_id => array( IDs of items assigned to the term ) $term_taxonomy_ids = implode( ',', array_values( self::$gallery_terms ) ); $query = sprintf( 'SELECT object_id, term_taxonomy_id FROM %1$s WHERE ( term_taxonomy_id IN ( %2$s ) ) ORDER BY RAND()', $wpdb->term_relationships, $term_taxonomy_ids ); $results = $wpdb->get_results( $query ); foreach ( $results as $result ) { self::$random_galleries[ absint( $result->term_taxonomy_id ) ][] = absint( $result->object_id ); } unset( $results ); } // Convert the parameter value to a sanitized slug value $random_slug = esc_sql( sanitize_term_field( 'slug', $shortcode_attributes['random_category'], 0, 'attachment_category', 'db' ) ); // Make sure the parameter matches a value in the galleries if ( ! $random_key = ( isset( self::$gallery_terms[ $random_slug ] ) ? self::$gallery_terms[ $random_slug ] : 0 ) ) { $shortcode_attributes['numberposts'] = 0; return $shortcode_attributes; } // Limit the result set if numberposts is present if ( $numberposts = ( isset( $shortcode_attributes['numberposts'] ) ? absint( $shortcode_attributes['numberposts'] ) : 0 ) ) { if ( $numberposts < count( self::$random_galleries[ $random_key ] ) ) { $ids = array_slice( self::$random_galleries[ $random_key ], 0, $numberposts, true ); } else { $ids = self::$random_galleries[ $random_key ]; } } else { $ids = self::$random_galleries[ $random_key ]; } $shortcode_attributes['ids'] = implode( ',', $ids ); unset( $shortcode_attributes['random_category'] ); unset( $shortcode_attributes['numberposts'] ); return $shortcode_attributes; } // mla_gallery_attributes_filter } // Class MLARandomGalleriesExample /* * Install the filters at an early opportunity */ add_action('init', 'MLARandomGalleriesExample::initialize'); ?>