How to adjust found_posts so that it accounts for offset and pagination

So far this is the code I have in my index.php file:

<?php 

        // Custom code to offset posts and setup pagination
        $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

        $ppp = get_option( 'posts_per_page' );      

        if ( $page == 1 )
            $offset = 6;
        else
            $offset = 6 + ( $page - 1 ) * $ppp;

        $args = array(
          'posts_per_page' => $ppp,
          'offset'     =>  $offset,
        );
        $offset_query = new WP_Query ( $args );

?>

At this point everything works fine except that there is an extra page with no posts on the site. Apparently this occurs because found_posts doesn’t account for offset and so found_posts thinks there are more posts and adds an extra page.

So how would I deduct the offset from the found_posts value so that the extra page doesn’t occur. Or, how would I just get rid of the extra page that is showing up.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are going about this the hard way, and the less efficient way. You already have a Loop on the page. You should be able to use that and that alone.

if (have_posts()) {
  while (have_posts()) {
    the_post();
    if (6 < $wp_query->current_post) {
      // formatting for your first six posts
    } else {
      // formatting for the other posts
    }
  }
}

That’s it. No new query needed. Pagination will work with no extra effort.

Caveats:

  1. “Sticky” posts will be included in your first six posts.
  2. Your post count will be the same for all posts so on page one you
    will have six differently formatted posts plus your posts_per_page
    minus 6 other posts. Other pages will just have posts_per_page posts.

If you want an extra six on the first (front) page you will need this in your theme’s functions.php:

function special_offset_pregp_wpse_105496($qry) {
  if ($qry->is_main_query()) {
    $ppg = get_option('posts_per_page');
    $offset = 2;
    if (!$qry->is_paged()) {
      $qry->set('posts_per_page',$offset);
    } else {
      $offset = $offset + ( ($qry->query_vars['paged']-1) * $ppg );
      $qry->set('posts_per_page',$ppg);
      $qry->set('offset',$offset);
      add_filter('found_posts', 'special_offset_pregp_fp_wpse_105496', 1, 2 );
    }
  }
}
add_action('pre_get_posts','special_offset_pregp_wpse_105496');

function special_offset_pregp_fp_wpse_105496($found_posts) {
  return $found_posts - 2;
}

Method 2

I think your searching for this solution

http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

The Problem

Offsets are useful because they can allow a developer to skip a certain number of WordPress posts before starting output.

Unfortunately, many developers find out that hard way that setting an offset value in their custom WordPress queries has the nasty and potentially serious side-effect of breaking pagination.

There is a very good reason for this however… the offset argument is actually the value WordPress uses to handle pagination itself. If a developer sets a manual offset value, pagination will not function because that value will override WordPress’s automatic adjustment of the offset for a given page.

The Solution

In order to use an offset in WordPress queries without losing WordPress’s pagination features, you will need to manually handle some basic pagination calculations. This can accomplished with the following two hooks:

pre_get_posts – This hook allows tweaking of queries before they are run. Here we need to ensure that our offset is applied only to the first page.

found_posts – Allows correction of WordPress’s query result count. This allows us to ensure WordPress takes our offset into account for pages other than the first page.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x