-
-
Save tommcfarlin/1e69ad55ecdb13424a66253c7d7aa4df to your computer and use it in GitHub Desktop.
[WordPress] Custom Post Type Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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