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

WordPress Trac noreply at wordpress.org
Sun Mar 1 10:35:02 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                                 |
-------------------------------------------------+-------------------------

Comment (by rahultank):

 Code review for ticket #12799 — gallery shortcode limit attribute

 **Environment tested:**
 - WordPress version: trunk (latest develop branch)
 - PHP version: 8.3.30

 **Findings:**

 Confirmed the `limit` attribute does not exist in the current
 `gallery_shortcode()` function (`src/wp-includes/media.php`, line 2711).
 The `shortcode_atts()` call (line 2747) lists all accepted attributes:
 order, orderby, id, itemtag, icontag, captiontag, columns, size, include,
 exclude, link — `limit` is absent. The feature request remains valid.

 **Issues with the existing patch approach:**
 The patch applies a PHP-level `array_slice()` after the database query has
 already fetched all images. This is inefficient — all rows are loaded into
 memory first, then discarded.

 **Suggested better approach:**
 Add `limit` to `shortcode_atts()` defaults as 0 (no limit), then pass it
 as `posts_per_page` to the `get_posts()`/`get_children()` query so the
 database handles the limit:

 In shortcode_atts defaults: 'limit' => 0

 In get_posts()/get_children() args: 'posts_per_page' => ( $atts['limit'] >
 0 ) ? (int) $atts['limit'] : -1

 This way the limit is applied at the SQL level (more efficient), and works
 correctly for both the include, exclude, and default attachment query
 paths.

 **Existing patch status:**
 The patch does not apply cleanly to current trunk as the function has
 evolved significantly since it was written. A fresh patch using the
 posts_per_page approach would be a better candidate for commit.

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


More information about the wp-trac mailing list