Advertisement
Guest User

Untitled

a guest
Mar 11th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // add a filter to the gallery shortcode
  2. add_filter( 'shortcode_atts_gallery', 'filter_gallery_by_tag',10,3 );
  3.  
  4. function filter_gallery_by_tag( $out, $pairs, $atts ) {
  5.  
  6. $post = get_post();
  7.  
  8. if ( !$post || !isset( $atts['tag_id'] ) ) {
  9. return $out;
  10. }
  11.  
  12. // check if the current post has the tag id
  13. if ( has_term( $atts['tag_id'], 'post_tag', $post ) ) {
  14.  
  15. $posts_per_page = ( isset( $atts['posts_per_page'] ) ) ? intval( $posts_per_page ) : 6;
  16.  
  17. $args = array(
  18. 'posts_per_page' => $posts_per_page,
  19. 'fields' => 'ids',
  20. 'tag_id' => $atts['tag_id'],
  21. 'meta_query' => array(
  22. array( 'key' => '_thumbnail_id' )
  23. ),
  24. );
  25.  
  26. // get posts with post thumbnails that have the same tag id
  27. $related_posts = get_posts( $args );
  28.  
  29.  
  30. if ( $related_posts ) {
  31.  
  32. $thumb_ids = array();
  33.  
  34. foreach ( $related_posts as $related ) {
  35. $thumb_ids[] = get_post_thumbnail_id( $related );
  36. }
  37.  
  38. $out['include'] = $thumb_ids;
  39. }
  40. }
  41.  
  42. return $out;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement