Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-pre-get-posts.php Secret
Last active Feb 20, 2019

Embed
What would you like to do?
[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
You can’t perform that action at this time.