get_post_galleries()
Gets all galleries from the text of the specified post. Searches for [gallery] shortcodes in the text, processes them and returns an array of gallery image data.
When you need to get only the first gallery, use get_post_gallery().
Since WordPress 5.9, this function supports block editor (Gutenberg) galleries.
1 time — 0.009364 sec (very slow) | 50000 times — 538.27 sec (extremely slow) | PHP 7.0.4, WP 4.4.2
Hooks from the function
Returns
Array. A list of gallery images or multiple galleries. If the $html=true parameter then the array will contain the ready HTML code of each gallery.
Usage
get_post_galleries( $post, $html );
- $post(int/WP_Post) (required)
- ID or post object in whose text to find galleries.
- $html(boolean)
- Whether to return the gallery's ready HTML in the data or the gallery data as attachment IDs and image URLs.
Default: true
Examples
#1 Get all the gallery from the post content
This example shows how the function works. It is assumed that there are 2 galleries in the text of post 2179 - 2 shortcodes [gallery]:
$gal = get_post_galleries( 2179, false ); /* $gal will be equal to Array ( [0] => Array ( [ids] => 6790,6789,6788 [src] => Array ( [0] => http://wp-kama.ru/wp-content/uploads/2016/02/image12-80x80.png [1] => http://wp-kama.ru/wp-content/uploads/2016/02/image11-80x80.png [2] => http://wp-kama.ru/wp-content/uploads/2016/02/image10-80x80.png ) ) [1] => Array ( [ids] => 6762,6761,6760 [src] => Array ( [0] => http://wp-kama.ru/wp-content/uploads/2016/02/image008-80x80.jpg [1] => http://wp-kama.ru/wp-content/uploads/2016/02/image007-80x80.jpg [2] => http://wp-kama.ru/wp-content/uploads/2016/02/image006-80x80.jpg ) ) ) */
$gal = get_post_galleries( 25, true ); /*$gal will be equal to Array ( [0] => <div id='gallery-3' class='gallery galleryid-19 gallery-columns-3 gallery-size-thumbnail'><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image12'><img width="80" height="80" src="/wp-content/uploads/2016/02/image12-80x80.png" class="attachment-thumbnail size-thumbnail" alt="image12" /></a> </div></figure><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image11'><img width="80" height="80" src="/wp-content/uploads/2016/02/image11-80x80.png" class="attachment-thumbnail size-thumbnail" alt="image11" /></a> </div></figure><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image10'><img width="80" height="80" src="/wp-content/uploads/2016/02/image10-80x80.png" class="attachment-thumbnail size-thumbnail" alt="image10" /></a> </div></figure> </div> [1] => <div id='gallery-4' class='gallery galleryid-19 gallery-columns-3 gallery-size-thumbnail'><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image008'><img width="80" height="80" src="/wp-content/uploads/2016/02/image008-80x80.jpg" class="attachment-thumbnail size-thumbnail" alt="image008" /></a> </div></figure><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image007'><img width="80" height="80" src="/wp-content/uploads/2016/02/image007-80x80.jpg" class="attachment-thumbnail size-thumbnail" alt="image007" /></a> </div></figure><figure class='gallery-item'> <div class='gallery-icon landscape'> <a href='/article/sajt-na-konstruktore-wix/image006'><img width="80" height="80" src="/wp-content/uploads/2016/02/image006-80x80.jpg" class="attachment-thumbnail size-thumbnail" alt="image006" /></a> </div></figure> </div> ) */
#2 Example of parsing block editor galleries (Gutenberg)
Suppose we have the following post content:
Then if we use this function we get:
$data = get_post_galleries( 2928, true ); $data = array_map( 'esc_html', $data ); print_r( $data ); /* Array ( [0] => <figure> <figure class="wp-block-image size-large"> <img src="https://example.com/wp-content/uploads/2025/01/photo-101.jpg" alt="" class="wp-image-101" /> </figure> <figure class="wp-block-image size-large"> <img src="https://example.com/wp-content/uploads/2025/01/photo-102.jpg" alt="" class="wp-image-102" /> </figure> <figure class="wp-block-image size-large"> <img src="https://example.com/wp-content/uploads/2025/01/photo-103.jpg" alt="" class="wp-image-103" /> </figure> </figure> [1] => <figure> <figure class="wp-block-image size-large"> <a href="https://example.com/wp-content/uploads/2025/01/photo-201.jpg"> <img src="https://example.com/wp-content/uploads/2025/01/photo-201.jpg" alt="" class="wp-image-201" /> </a> <figcaption>Caption 201</figcaption> </figure> <figure class="wp-block-image size-large"> <a href="https://example.com/wp-content/uploads/2025/01/photo-202.jpg"> <img src="https://example.com/wp-content/uploads/2025/01/photo-202.jpg" alt="" class="wp-image-202" /> </a> <figcaption>Caption 202</figcaption> </figure> </figure> [2] => <figure> <figure class="wp-block-image size-thumbnail is-style-rounded"> <a href="https://example.com/?attachment_id=301"> <img src="https://example.com/wp-content/uploads/2025/01/photo-301-150x150.jpg" alt="" class="wp-image-301" /> </a> </figure> <figure class="wp-block-image size-thumbnail is-style-rounded"> <a href="https://example.com/?attachment_id=302"> <img src="https://example.com/wp-content/uploads/2025/01/photo-302-150x150.jpg" alt="" class="wp-image-302" /> </a> </figure> <figure class="wp-block-image size-thumbnail is-style-rounded"> <a href="https://example.com/?attachment_id=303"> <img src="https://example.com/wp-content/uploads/2025/01/photo-303-150x150.jpg" alt="" class="wp-image-303" /> </a> </figure> <figure class="wp-block-image size-thumbnail is-style-rounded"> <a href="https://example.com/?attachment_id=304"> <img src="https://example.com/wp-content/uploads/2025/01/photo-304-150x150.jpg" alt="" class="wp-image-304" /> </a> </figure> </figure> ) */
$data = get_post_galleries( 2928, false ); print_r( $data ); /* Array ( [0] => Array ( [ids] => 101,102,103 [src] => Array() ) [1] => Array ( [ids] => 201,202 [src] => Array() ) [2] => Array ( [ids] => 301,302,303,304 [src] => Array() ) ) */
Changelog
| Since 3.6.0 | Introduced. |