Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active February 20, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/1e69ad55ecdb13424a66253c7d7aa4df to your computer and use it in GitHub Desktop.
Save tommcfarlin/1e69ad55ecdb13424a66253c7d7aa4df to your computer and use it in GitHub Desktop.
[WordPress] Custom Post Type Pagination
<?php
use WP_Query;
add_action('pre_get_posts', __NAMESPACE__ . '\\customPostTypePagination');
/**
* If we're on the 'acme-post-type' page, then adjusts pagination so there are only two posts per page. This will
* affect pagination so we can use built-in pagination links to easily page through the content.
*
* @param WP_Query the instance of the query object that contains information to populate the template.
*
* @return WP_Query an unmodified version of the query object or a modified version if we're on the acme post-type.
*/
function customPostTypePagination(WP_Query $query)
{
if (isset($query->query['post_type']) && 'acme-post-type' == $query->query['post_type']) {
$query->set('posts_per_page', 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment