Why does “Blog pages show at most” interfere with my custom wp_query?

I’m very new to WordPress, especially when it comes to creating a custom loop as per below. This works fine, however I’m perplexed why “Blog pages show at most” (under Settings > Reading) is interfering with the query.

In the example below, I have post_per_page set at 10. If I leave the default “Blog pages show at most” setting as 10 posts it works fine because they both match and calculates the pages correctly. However if I change the “posts_per_page” to 5, I get a few extra pages added to the pagination, which display “page not found” when clicked.

Read More

Is it possible to override this setting from the admin? I thought creating a custom wp_query would override this anyway. What am I doing wrong?

I’m also using wp_pagenavi for the pagination as you can see in the example below, and have a custom post type of “listing”. I’m using WordPress 3.1.3.

<?php $custom_query = new WP_Query( array( 'post_type' => 'listing', 'posts_per_page' => 10, 'paged' => get_query_var('paged') ) ); ?>  

<?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>  

    <div id="post-<?php the_ID(); ?>">   
        // stuff here
    </div>

<?php endwhile; endif; ?>

//wp_pagenavi 
<?php 
    if (function_exists('wp_pagenavi')) {
    wp_pagenavi( array( 'query' => $custom_query ) ); } 
?>

<?php wp_reset_postdata(); ?>

Related posts

Leave a Reply

4 comments

  1. <?php
    global $query_string;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $custom_query = new WP_Query( array( 'post_type' => 'listing', 'posts_per_page' => 10, 'paged' => $paged ) );
    ?>
    

    add the code $paged =...

  2. “Blog pages show at most” is more or less exactly what it seems like. It is the number of posts that will show per page for paginated archive listings. posts_per_page overrides that setting for particular queries.

    You see more than the posts_per_page value because sticky posts are shuffled around and tacked onto to beginning of the result set causing the first page to have posts_per_page + “sticky posts count” number of posts. This is odd odd, I admit, but it derives from the equally odd decision to store sticky posts as a serialized array in $wpdb->options instead of as entries in $wpdb->postmeta.

    To prevent the sticky posts juggling, pass 'ignore_sticky_posts' => true as an argument to your WP_Query.

  3. What “Blog Pages show at most” does is work out how many pages your site actually has when it shows the maximum posts per page. It then sends users to an error page if they go to a page which has no posts.