Offset a WordPress loop with pagination

Last modified January 19, 2020
.* :☆゚

If you want to offset a WordPress loop, you may just simply add an offset argument to the query parameters. If you want pagination with that however, it is slightly trickier and requires a bit of math.

Luckily, WordPress makes it super easy to handle, making offsetting pagination relatively stress-free.

Copy the code below and try changing the offset parameter. Don’t forget to change get_query_var('page') to get_query_var('paged') if you are using this on your front page and you should be good to go!

<?php

  $number_of_posts_per_page = 9;
  $initial_offset = 1;
  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
  // Use paged if this is not on the front page

  $number_of_posts_past = $number_of_posts_per_page * ($paged - 1);
  $offset = $initial_offset + (($paged > 1) ? $number_of_posts_past : 0);

// WP_Query arguments
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => $number_of_posts_per_page,
    'paged' => $paged,
    'offset' => $offset
  );

  // the query
  $query = new WP_Query( $args );

    // start the loop
    if ( $query->have_posts() ) {
      the_title(sprintf('<h3><a href="%s" >', get_permalink()),'</a></h3>');
    }

  } // end of the loop

  //display pagination
  the_posts_navigation();

  }
  else {
  // no posts found
  }

// restore the post data
wp_reset_postdata();

?>