Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have one page in my template where I would like to set a custom posts_per_page. Here is the code I have used:

<?php
global $query_string;
query_posts($query_string . '&posts_per_page=4'); 
if ( have_posts() ) : while ( have_posts() ) : the_post();
...

Now this code limits only 4 items per page and shows pageinate_links below as I have written. However clicking on any other page will lead to a 404.

If I take out the global and query_posts line then it works fine.

This is the paginate_links function I am using:

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages,
    'prev_text' => 'Previous',
    'next_text' => 'Next',  
) );
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Use a pre_get_posts action in your functions.php with conditional tags, and remove the call to query_posts:

function wpa62751_pre_get_posts( $query ) {
    if ( is_category( 'my-category' ) && is_main_query() )
        $query->set( 'posts_per_page', 4 );
}
add_action( 'pre_get_posts', 'wpa62751_pre_get_posts' );
share|improve this answer
add comment

Note - The recommended way is pointed out by @Milo, Use of pre_get_post is always a good choice over query_posts

However, you can make it work with pagination by passing paged parameter.

Example -

global $wp_query;
$args = array_merge( 
                $wp_query->query, // old query
                array( 
                    'posts_per_page' => 4,
                    'paged' => get_query_var('paged')
                    //  'paged' => get_query_var('page')
                ) 
        );  
query_posts( $args );
share|improve this answer
    
I have tried your solution, however I still recieve page 404 on any other page other than the first page. Why would using pre_get_post be better than query_posts? –  Imran Aug 23 '12 at 17:40
    
@Imran See These,Answers –  amit Aug 23 '12 at 18:01
add comment

Your Answer

 
discard

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.