[wp-trac] [WordPress Trac] #12799: Allow gallery shortcode to accept a maximum number of items

WordPress Trac noreply at wordpress.org
Tue May 5 12:07:54 UTC 2026


#12799: Allow gallery shortcode to accept a maximum number of items
-------------------------------------------------+-------------------------
 Reporter:  dtorbert                             |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Gallery                              |     Version:  2.9
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch changes-requested close    |     Focuses:
  good-first-bug has-unit-tests                  |
-------------------------------------------------+-------------------------

Comment (by micahele):

 I opened a refreshed PR for this ticket:
 https://github.com/WordPress/wordpress-develop/pull/11710

 The PR adds a `limit` attribute to the gallery shortcode, allowing
 galleries to cap the maximum number of images displayed, e.g. `[gallery
 limit="2"]`.

 A few implementation notes:
 - Values less than `1` are treated as unlimited, matching the default
 behavior.
 - The default value is `-1`, so existing galleries are unaffected.
 - For normal galleries and `exclude`, the limit is passed into the
 attachment query.
 - For `include` / `ids`, the limit is applied after fetching because
 `get_posts()` ignores `numberposts` when `include` is used.

 The PR also includes PHPUnit coverage for default/no limit behavior,
 `ids`, `include`, `exclude`, and `orderby="rand"` cases.

 Tests run:
 - `php -l src/wp-includes/media.php`
 - `php -l tests/phpunit/tests/media.php`
 - `vendor/bin/phpunit tests/phpunit/tests/media.php
 --filter=test_gallery_shortcode`
 - `git diff --check -- src/wp-includes/media.php
 tests/phpunit/tests/media.php`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/12799#comment:25>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list