WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#29484 closed defect (bug) (invalid)

"posts_per_page" also restricts the number of images in a gallery

Reported by: Avantart Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.2
Component: Gallery Keywords:
Focuses: ui Cc:
PR Number:

Description

When you define the number of posts on a page, f.e. the homepage, galleries in the posts also show only this number of images.
I use galleries with defined IDs.

For example: the gallery consists of 12 images, but the posts_per_page is set to 5 for the homepage in functions.php, so only the first 5 images are shown

posts_per_page should not have any impact on the number of images in a gallery

Check here:

you see 5 images in gallery
http://www.wockensolle.de/page/2/

on single page you see 12 images:
http://www.wockensolle.de/2014/08/27/der-2-tag-in-riga/

the setting in the functions.php of my theme:

function custom_posts_per_page($query){
    if(is_home()){
    $query->set('posts_per_page',5);
    }
    if(is_search()){
        $query->set('posts_per_page',+5);
    }
    if(is_archive()){
        $query->set('posts_per_page',10);
} 
} 
add_action('pre_get_posts','custom_posts_per_page');

the gallery shortcode in the post:

[gallery link="file" columns="2" ids="25386,25387,25388,25389,25390,25391,25392,25393,25394,25395,25396,25397"]}}}

Change History (1)

#1 @SergeyBiryukov
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Yes, pre_get_posts runs for all queries.

To avoid unexpected results, you should make sure you're only changing the value for the main query on front-end, and return early otherwise:

if ( is_admin() || ! $query->is_main_query() ) {
	return;
}
Note: See TracTickets for help on using tickets.