1

I am trying to modify the number of posts_per_page in the loop. This way:

function posts_per_page($query) {
    $query->query_vars['posts_per_page'] = 3;
}
add_filter('pre_get_posts', 'posts_per_page', 11);

The problem I have is that when I do so, then sticky posts appear twice in the loop: first one at the begining of the loop and second one in their original position. So in this case, the first page has 4 posts (3 of loop without sticky + the sticky post), and the sticky post will appear (again) later in its "page" with other 2 posts.

EDIT: SOLUTION

After doing quite much research I realised sticky posts appear always in the first page. If the original sticky post was already in the first page then only the sticky appears (this post only will appear one). I needed to know exacty how many posts will my query have, but $wp_query->found_posts was not counting the sticky posts. And if I did $wp_query->found_posts + get_option( 'sticky_posts' ) wouldnt be correct as it doesnt consider the 'sticky post from first page' I said before and also counts non-published sticky posts.

With $wp_query->posts I get the real number of posts in my first page, so:

$sticky = count($wp_query->posts) - get_option( 'posts_per_page' );

if ($sticky<0) {$sticky=0;}//In case there is only one page of results

Now $sticky will have the real number of sticky posts.

1

What you describe is not an issue but normal WordPress behaviour. When you mark a post as a sticky it will be at the top of the list and it will appear in it's original postition as well, unless the original position is on the first page. A sticky will appear at the top of the posts and in it's original position but it will not appear twice on the same page.

You might want to do something like this:

<?php
while (have_posts()) : the_post();
    if ( !in_array( $post->ID, $do_not_duplicate ) ) { // check IDs         
// display posts ...
        the_title();

//store id in "do not duplicate
$do_not_duplicate = $post->ID;
    }
endwhile;
?>

source with minor edit: http://wpengineer.com/1719/filter-duplicate-posts-in-the-loop/

  • It was throwing an error, I created $do_not_duplicate=[]; before the loop and $do_not_duplicate = $post->ID; to $do_not_duplicate[] = $post->ID; Im loading articles of next pages via ajax so the problem persists. Thanks – Alvaro Oct 6 '13 at 22:38
  • "sticky it will be at the top of the list and it will appear in it's original postition as well". This is not exactly correct, if the sticky post is in the first page, it wont appear twice, only one time as sticky. – Alvaro Oct 12 '13 at 11:58
  • Just did thorough testing and updated my answer. – user2019515 Oct 12 '13 at 13:36
0

More correct would be to use the set method.

function posts_per_page($query) {
    $query->set('posts_per_page',3);
}
add_filter('pre_get_posts', 'posts_per_page', 11);

Try that.

Note: I can't duplicate this multiple sticky issue. If that doesn't solve things edit your question with more detail. That may be theme specific or due to a plugin.

  • Thanks. The issue is still there. There are no plugins so it must be a problem somewhere else in the theme. Until I find the solution ill turn off sticky posts $query->set('ignore_sticky_posts',1); – Alvaro Oct 6 '13 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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