0

By default, site is set to show 4 posts per page, but on a particular category (id 76) archive I want it to show 12. I've included this code in functions.php but it's not working. What am I missing here:

add_action('pre_get_posts','filter_archive');
function filter_archive( $query ) {
    if ( ! is_admin && is_category('76') && $query->is_main_query() ) {
        $query->set('posts_per_page', '12');    
    }
    return $query;
}
2

You've got a typo there, should be: is_admin(). You should enable debugging by setting define('WP_DEBUG', true); in wp-config.php to see such errors. Also there's no need of returning anything in action hook.

add_action( 'pre_get_posts','filter_archive', 1 );
function filter_archive( $query ) {
    if ( ! is_admin() && is_category('76') && $query->is_main_query() )
        $query->set('posts_per_page', '12');
}

Sidenote: personally I prefer to rely on slugs than on IDs in such cases. If the website needs to be set up from scratch on new database, I have more flexibility. I also avoid inconsitencies between development instance and production instance which have (and should have) different databases.

| improve this answer | |
  • Thanks, of course! I figured there had to be something simple I was overlooking. – John Chandler Feb 26 '15 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.