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.

  • Why are you creating a new query? Can you explain what you are doing? – s_ha_dum Jul 5 '13 at 22:17
  • I have a a featured posts section and want to offset the rest of the posts so that it doesn't duplicate, so i made a query. Then used that query in The Loop to get my posts. – Artha Jul 6 '13 at 14:49
  • How are your featured posts identified? I suspect you may be doing this the difficult way. – s_ha_dum Jul 6 '13 at 14:54
  • the featured posts are just the 6 newest posts - wsidn.tk – Artha Jul 6 '13 at 14:56
up vote 3 down vote accepted

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;
}
  • Let me try what you said and see if it works. Since div doesn't work in the PHP script would I do <?php if() : ?> ... <php endif ?>. – Artha Jul 6 '13 at 16:56
  • Yes. ?> before HTML and <?php after HTML and before the PHP. – s_ha_dum Jul 6 '13 at 16:59

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.

  • I have seen this post, but really have no idea how or where to implement it. I believe you have to put the given code into a plugin and then do something with $wpdb but I really don't know. Any help would be appreciated. – Artha Jul 6 '13 at 14:50
  • If you go - the link at below you will find two code there, use that code into your functions.php and it will solved your issue. – Shahinul Islam Jul 6 '13 at 14:54
  • so i just copy that code into functions.php and that's it? – Artha Jul 6 '13 at 14:56
  • You need modify your offset value. Please take your time and read the total link tutorial, all is there i think its very easy. – Shahinul Islam Jul 6 '13 at 14:59

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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