Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // add a filter to the gallery shortcode
- add_filter( 'shortcode_atts_gallery', 'filter_gallery_by_tag',10,3 );
- function filter_gallery_by_tag( $out, $pairs, $atts ) {
- $post = get_post();
- if ( !$post || !isset( $atts['tag_id'] ) ) {
- return $out;
- }
- // check if the current post has the tag id
- if ( has_term( $atts['tag_id'], 'post_tag', $post ) ) {
- $posts_per_page = ( isset( $atts['posts_per_page'] ) ) ? intval( $posts_per_page ) : 6;
- $args = array(
- 'posts_per_page' => $posts_per_page,
- 'fields' => 'ids',
- 'tag_id' => $atts['tag_id'],
- 'meta_query' => array(
- array( 'key' => '_thumbnail_id' )
- ),
- );
- // get posts with post thumbnails that have the same tag id
- $related_posts = get_posts( $args );
- if ( $related_posts ) {
- $thumb_ids = array();
- foreach ( $related_posts as $related ) {
- $thumb_ids[] = get_post_thumbnail_id( $related );
- }
- $out['include'] = $thumb_ids;
- }
- }
- return $out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement