wordpress pagination fix after the_posts

I am using the_posts action to filter out some unwanted posts – this works fine, but the pagination after this is applied gets broken. If for example there is a page with 10 results before the_posts is applied, then after it gets applied the page displays only 6 posts for example, yet there is 100 posts in total and 20 of them should be excluded.

Pagination shows 1-10 pages when it should show 1-8.

Read More

So the question is – how to make pagination work as expected and display 10 posts per page and not to have pages with less than that.

Related posts

Leave a Reply

3 comments

  1. Instead of filtering posts inside the loop, you should try filtering them right after the loop. Query_posts() can solve the problem: http://codex.wordpress.org/Function_Reference/query_posts

    This is how you can “merge” the modifications on the main query to insert any filters without messing the pagination:

    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product' ) );
    query_posts( $args );