WordPress.org

Make WordPress Core

Opened 22 months ago

Closed 22 months ago

Last modified 19 months ago

#25246 closed defect (bug) (wontfix)

Make WP_Query (pagination) to load after template is loaded

Reported by: alexvorn2 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.6
Component: Query Keywords:
Focuses: Cc:

Description

The problem is that the pagination is calculated before the template is loaded, so sometimes the page/2 does not contain the posts requested or does not display the posts at all.

On other hand this will help to load multiple posts types on the main query with not breaking the pagination, adjust number of posts per page depending on custom posts type or main post type - on different archive pages:

Ex: on category.php page to display only 5 posts at most but on index.php - 20 posts at most.

Of course we can use some custom actions that will change the value of the pre_get_posts but my ticket is for modification of the WP_Query itself so the pagination will be calculated like on Page Templates does.

Change History (8)

comment:1 follow-up: @markoheijnen22 months ago

I'm not sure what you are requesting. pre_get_posts seems to be the proper filter for this things.

comment:2 in reply to: ↑ 1 @alexvorn222 months ago

Replying to markoheijnen:

I'm not sure what you are requesting. pre_get_posts seems to be the proper filter for this things.

As I mentioned in the ticket is that we can use the filter pre_get_posts to modify the value but I this seems not the good thing when we have a custom property in the arguments of a custom query, example:

The Query

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array( 
    'posts_per_page' => 3,
    'paged'          => $paged
);

$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<p>' . get_the_title() . '</p>';
	}
}

So if we have only 4 posts and we want to use the posts_per_page property to show 3 posts it works, but we will go to page/2 to show the last 1 post, it will show as a 404 error page instead.

So that's why I opened this ticket, I request this.

Last edited 22 months ago by alexvorn2 (previous) (diff)

comment:3 follow-up: @johnbillion22 months ago

  • Keywords close added

If you're doing a new WP_Query() within the page then that query is unrelated to the URL of the page you're on. The two are not and should not be connected. The 404 you get from your example is generated by the main query, which is separate from your secondary query.

The pre_get_posts filter is what you need here.

Suggesting close as invalid.

comment:4 in reply to: ↑ 3 @helen22 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Replying to johnbillion:

The pre_get_posts filter is what you need here.

Indeed.

comment:5 follow-up: @alexvorn222 months ago

Ok, but I'm sure 100% this will get attention again later :)

comment:6 in reply to: ↑ 5 @markoheijnen22 months ago

Replying to alexvorn2:

Ok, but I'm sure 100% this will get attention again later :)

I doubt that unless you provide a better example or a patch. In this case just use pre_get_posts and don't touch pages at all. Nothing can go wrong then unless you want pagination on the custom query you have in your theme. That is almost unfixable.

comment:8 @SergeyBiryukov19 months ago

  • Version changed from trunk to 3.6
Note: See TracTickets for help on using tickets.