Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 1.93 KB

File metadata and controls

54 lines (38 loc) · 1.93 KB

No Offset Pagination for WordPress

This is an experiment trying to implement keyset pagination aka No offset pagination as it was proposed by Markus Winand ( @fatalmind ) on http://use-the-index-luke.com/no-offset to WordPress

Do not use offset for pagination. Learn why.

Template tags usage

The plugin provides custom template tag for displaying next/prev navigation on archive pages (eg.: index.php or archive.php).

no_offset_pagination();

It should be used instead of standard wp_link_pages(); or twentyfourteen_paging_nav(); in case you are working with default Twentyfourteen template.

Custom plugin / functions.php usage

You can also take advantage of this plugin in your custom development efforts in your plugins or theme functions.php file.

You just have to define extra query_vars for WP_Query.

(Please note: you still must have this plugin installed before nooffset param is taken into consideration)

$args = array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 10,
  'nooffset' => array( 'next' => $last_displayed_post_id ) //this is the plugin's specific query_vars definition
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach ( $posts as $post ) {
  ...
}

For listing previous posts, you have to get the most recent post's ID ( the one displayed as first on your page ) and refer it from nooffset array this way:

$args = array(
  ...
  'nooffset' => array( 'prev' => $most_recent_displayed_post_id ),
  ...
);
...

SQL Queries produced by the plugin

If you are interested how does the produces SQL queries look like, visit the wiki