WP Query with offset breaks wp_pagenavi or any pagination

I am working on a custom loop using WP_Query using the offset parameter. The problem is that as soon as I add the offset, it breaks the pagination, displaying the same links no matter the page number.

?php

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
  'post_type' = 'notice',
  'posts_per_page' = 6,
  'offset' = 1,
  'paged' = $paged
);

$the_query = new WP_Query( $query_args );
?

?php if ( $the_query-have_posts() ) : ?

?php while ( $the_query-have_posts() ) : $the_query-the_post(); ?

    h3a href="?php the_permalink(); ?"?php the_title() ?/a/h3

?php endwhile; ?

  ?php wp_pagenavi( array( 'query' = $the_query ) ); ?

?php endif; ?

I tried following the code here:

https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

But adding this code to my theme breaks any related queries.

Any ideas on how I could make this work?

Topic offsets wp-query pagination Wordpress

Category Web


I had the same issue.

Check out if this works for you:


<?php
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $per_page = 9;
  $default_offset = 4;

  if ($paged == 1) {
    $offset = $default_offset;
  } else {
    $offset = (($paged - 1) * $per_page) + $default_offset;
  }

  $args = array(
    'post_type' => 'post',
    'posts_per_page' => $per_page,
    'order'   => 'DESC',
    'offset' => $offset,
    'paged' => $paged
  );

  $loop = new WP_Query($args);

  while ($loop->have_posts()) : 
    $loop->the_post();
?>
  ... HTML
<?php 
  endwhile; 
  wp_pagenavi(array('query' => $loop)); wp_reset_postdata();
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.