WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

WordPress Version: 3.3.2

Assumption, My post in category 7 have 9 posts during October 2012.

by accessing


I am able to view all in one page. No issue, however, I create custom page category-7.php in theme folder.

//listed category per page = 6
$query = 'posts_per_page=6&cat=7';
$year = get_query_var('year');
if($year > 0)
    //build query string
    $query .= '&year='.$year;
$month = get_query_var('monthnum');
if($month > 0)
    $query .= '&monthnum='.$month;
$paged = get_query_var('paged');
if($paged > 0)
    $query .= '&paged='.$paged;

Now I access again


The bottom got Next Page option, but when I click it to


I got "404 - Page not found!"

The only solution I can do is go to WP-Admin, Reading Settings change the "Blog pages show at most" variable to 6, then I got the correct output.

However, as I research in the wordpress page, by added query_posts parameter, it should be work correctly.

Please advise. Thanks!

share|improve this question
up vote 1 down vote accepted

The problem is that you're stomping the main query by using query_posts(). Don't do that.

Instead, filter the main query via pre_get_posts:

function wpse82113_filter_pre_get_posts( $query ) {
    if ( is_category( 7 ) && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '6' );
add_action( 'pre_get_posts', 'wpse82113_filter_pre_get_posts' );
share|improve this answer
Hi, this code is required add to functions.php, as I am using premium wordpress theme, it will affect when I do the theme upgrade. Is it possible way I can do in category-7.php, so it is independence? – Shiro Jan 21 '13 at 1:31
Using a commercial Theme or not should make no difference. You can put this in a Child Theme functions.php file, or you can use a custom, site-functionality Plugin, and put it there. But a pre_get_posts filter callback should never be put in the template file, because by the time the template is loaded, the query has already been run, and the posts have already been fetched. – Chip Bennett Jan 21 '13 at 1:35
Thanks Chip Bennett, I learn a lot from you. As you said never put in the template file, but in the beginner you mention can put in child Theme file, so which one is better? I am still new in WordPress, do you have any more tutorial or learning site regarding this? Thanks! – Shiro Jan 21 '13 at 1:52
In a Child Theme, you still wouldn't put it in the template file, but rather in the Child Theme's functions.php file. But I would go with a site-functionality Plugin as the better solution. Here's a tutorial for creating a site functionality Plugin. – Chip Bennett Jan 21 '13 at 2:38
Usually, when referring to WordPress Themes, a template file is a file used to render the front-end output, as found in the Template Hierarchy. Anything else is either a template-part file (called from within a template file), or else a functional file (functions.php, or any file included in functions.php, that defines Theme functions/functionality). – Chip Bennett Jan 21 '13 at 5:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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