Skip to content

Instantly share code, notes, and snippets.

@alexkingorg
Created January 12, 2014 17:31
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 alexkingorg/8387783 to your computer and use it in GitHub Desktop.
Save alexkingorg/8387783 to your computer and use it in GitHub Desktop.
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