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'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.

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(); ?>
share|improve this question
    
I've noticed something else. Currently I am using archive-listing.php as the page with has_archive set to true in the post type settings. So I switched has_archive to false and set up a page template instead with the same code. Everything works fine. Not overridden by the admin setting at all but there's some strange behaviour with wp_pagenavi. If I have 5 pages of results, I can enter any number as the page (for example 100) into the URL and it will say page 100 of 5. –  Andrew Jun 24 '11 at 3:40

4 Answers 4

<?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 =...

share|improve this answer
    
Thanks, I added this (tried also to copy + paste) but when I change the posts_per_page to 5, it's still being overridden by the admin setting and adding a few more pages to the pagination. –  Andrew Jun 24 '11 at 3:03

see: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

share|improve this answer
1  
Thanks, tried this also but didn't seem to have any affect. Setting from admin is still overriding my posts_per_page setting and adding extra pages to the pagination. –  Andrew Jun 24 '11 at 3:04

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.

share|improve this answer

"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.

share|improve this answer

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.