WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 4 months ago

#37056 new enhancement

paginate_links should be a method on WP_Query

Reported by: jfarthing84 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.6
Component: Query Keywords:
Focuses: Cc:
PR Number:

Description

How do we paginate custom or subqueries when paginate_links relies upon the global $wp_query variable?

It's easy. We make paginate_links a method on the WP_Query object. Then the function paginate_links can rely upon the method. Likewise with get_the_posts_pagination, with a way to pass query context.

I'll get around to a patch when I can.

Change History (2)

#1 @xavivars
3 years ago

I'd even go further, and have a function (or static method) so you provide the the base URL, the current and total pages, and other stuff like the format to use on the pages,... and then have that method used by the current paginate_links() method (to keep backwards compatiblity).

That would make it much easier to reuse the logic of the pagination in other places (like plugins, or Timber) relying on core code, so no more ugly code needs to be written to simulate that behaviour.

That would also make it much easier to unit test (no need to setup too much WordPress global stuff, but relying on input parameters only.

#2 @GunGeekATX
3 years ago

You can pass total and current to paginate_links() to bypass WP_Query vars.

Example:

paginate_links( array( 'current' => 2, 'total' => 18, ' ) );
Note: See TracTickets for help on using tickets.