Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Customize posts-per-page based on some condition.
<?php
// add filter for front-end requests
if (!is_admin() && !is_feed()) {
add_filter('parse_request', 'my_add_posts_per_page');
}
// add our condition-checking filter
function my_add_posts_per_page() {
add_filter('pre_get_posts', 'my_posts_per_page');
}
// customize the posts_per_page,
function my_posts_per_page($query) {
// we only want our filter to run on the initial query, so we remove it here
remove_filter('pre_get_posts', 'my_posts_per_page');
// set your condition - can be whatever you want
if (is_category('example')) {
// set the number of posts per page
$query->set('posts_per_page', 100);
}
}
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.