Skip to main content
Solved
Source Link
Alvaro
  • 2.6k
  • 2
  • 28
  • 29

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.

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.

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.

Tweeted twitter.com/#!/StackWordPress/status/386963232420003840
Source Link
Alvaro
  • 2.6k
  • 2
  • 28
  • 29

Sticky post appears twice

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.