Editing built in Gallery shortcode to filter by categories

There are various ways to run another WP_Query with a tax query and collect the ID’s into the include attribute of the gallery shortcode.

Let’s try another approach, without running a secondary WP_Query.

Native gallery: Support taxonomy and term attributes

We introduce the following custom gallery shortcode attributes:


Here’s a demo plugin that supports that through the post_gallery, shortcode_atts_gallery filters and the pre_get_posts action:

<?php
/**
 * Plugin Name: Native Gallery Shortcode With Taxonomy And Term Filter Support
 * Description: Add support for 
 * Plugin URI:  http://wordpress.stackexchange.com/a/257801/26350
 */

namespace WPSE\Q257743;

class Gallery
{
    private $term;
    private $taxonomy;

    public function init()
    {
        add_filter( 'post_gallery', [$this, 'post_gallery'], 10, 2 );               
    }

    public function post_gallery( $html, $attr )
    {
        // Check user input for term and taxonomy
        if( empty( $attr['term'] ) || empty( $attr['taxonomy'] ) )
            return $html;

        // Validate taxonomy
        if( ! taxonomy_exists( $attr['taxonomy'] ) )
            return esc_html__( "Taxonomy doesn't exists!", 'wpse-257743' );

        // Validate term
        if( ! term_exists( $attr['term'] , $attr['taxonomy'] ) )
            return esc_html__( "Term doesn't exists!", 'wpse-257743' );

        $this->term     = $attr['term'];
        $this->taxonomy = $attr['taxonomy'];

        // Activate customization
        add_filter( 'shortcode_atts_gallery',   [$this, 'gallery_atts'] );
        add_action( 'pre_get_posts',            [$this, 'pre_get_posts'] );

        return $html;
    }

    public function gallery_atts( $pairs )
    {
        // Set include to a temporary non empty value (that becomes 0)
        $pairs['include'] = ' ';

        // Only run once per gallery
        remove_filter( current_filter(), __FUNCTION__ );

        return $pairs;
    }

    public function pre_get_posts( \WP_Query $q )
    {
        // Make sure it's attachments query
        if( 'attachment' !== $q->get( 'post_type' ) )
            return;

        $post__in = $q->get( 'post__in' );

        // Target queries with post__in (include)       
        if ( empty( $post__in ) )
            return;

        // Check for our temporary 0 value
        if( 1 === count( $post__in ) && 0 != $post__in[0] )
            return;

        $q->set( 'post__in', null );

        // Set the taxonomy query:
        $q->set( 'tax_query', 
            [
                [
                    'taxonomy'  => $this->taxonomy,
                    'field'     => 'slug',
                    'terms'     => $this->term,
                ]
            ]
        );

        // Adjust the number of images to e.g. max 100
        $q->set( 'posts_per_page', 100 );

        // Only run once per gallery
        remove_action( current_action(), __FUNCTION__ );
    }

}

add_action( 'init', [new Gallery, 'init' ] );

Note that here we added a limit of 100 images.

Hope you can test and adjust it further to your needs, and e.g. change it so it can support multiple terms.