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 almost finished with this one, just a small problem left. Hope you can help me.

I've got two very simple, custom loops. One for sticky posts, and one for non-sticky. I've manage to get the sticky posts to only show up on page 1, and not on the rest of the pages (when using pagination), which is just what I want. The problem is this: If I use posts_per_page set to 8, and have 1 sticky post in my database, I get 9 posts on the first page, and 8 on the rest of the pages.

I've managed to change the posts_per_page value with this script in between the loops:

$postcount = $wp_query->post_count; 
$postnumber = 8; 
if ( $paged != True ){ $postnumber = $postnumber - $postcount; }

and it almost work. If I have 1 sticky post and post 15-9 on the first page, and then click the next button, WP skips post #8 and only shows 7-1 on the next page.

Here is my code:

<!-- Sticky -->
<?php if ( $paged != True ): ?>
    <?php $wp_query = new WP_Query(array('post__in' => get_option('sticky_posts'), 'category_name' => Work)); ?>
        <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
            <?php get_template_part( 'loop', 'index' ); ?>
        <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>
<?php endif ?>

<?php 
$postcount = $wp_query->post_count; 
$postnumber = 8; 
if ( $paged != True ){ $postnumber = $postnumber - $postcount; }
?>

<!-- Non-Sticky -->
<?php $wp_query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'category_name' => Work, 'posts_per_page' => $postnumber, 'paged' => $paged)); ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <?php get_template_part( 'loop', 'index' ); ?>
    <?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
share|improve this question
    
Is this in your index.php or a custom template? –  Stephen Harris Apr 16 '12 at 10:30
    
Thanks for your answer. The above code is in the index.php file. And then i use <?php get_template_part( 'loop', 'index' ); ?>to load how the content is shown. I Also have single.php, archive.php, page.php and so on to show a diffrent layout there. –  Keat Apr 16 '12 at 12:48
    
I would think that this would be a common problem. No one ever stumbled upon this before? –  Keat Apr 16 '12 at 18:25

2 Answers 2

well, I had the same need and I simply added the sticky posts from the first loop to an array...

$stickyposts[] = get_the_ID();

and then used in the second loop

'posts_per_page' => 8-count($stickyposts)

it seems to work.

share|improve this answer

The problem is that on your first page you ask for, lets say, 7 'normal' posts. So it displays the latest 7 posts: e.g. 15-9.

On page two you are telling WordPress I want 8 posts per page and I want the second page. The internal logic is that if you have 8 posts per page and you are on the second page then you want the 9th to 16th posts (in this case the 9th post is 7 since its ordered by date).

You can manually set the offset to correct this, but it requires knowing how many 'normal' posts appeared on the first page. In any case...

Regarding having sticky posts at the top - WordPress should do this for you. That is, if you omit the post__not_in attribute then sticky posts should appear at the top, and you'll have the same number of posts for each page. Unless this is for a page template - you shouldn't event need to run your own query.

Regarding the code you've posted:

  • Avoid using the variable $wp_query - this is what WordPress calls the 'main query' for the page.
  • For checking if you are on the first or subsequent pages use is_paged()
  • It's generally better to use the tax_query rather than 'category_name'.
  • If you can, avoid editing the loop inside the template - instead use the pre_get_posts hook to make changes to the query before it gets there. See this post and this Codex.

Hope that helps :)

share|improve this answer
    
Hi Stephen and thanks for your detailed answer. I fixed all that you mentioned regarding the code. But if I just use the standard WP loop, and set Post per page in WP settings/read to 8, and have 1 sticky post in the db, the first page still shows 9 posts, the rest shows 8. –  Keat Apr 17 '12 at 19:49

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.