Skip to main content
1 of 6
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

What you want to do is not possible with pagination. I've been thinking about it and came up with this.

This is how I will do it

  • Get the current page you are on with $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  • Run a custom query in which you get the first post, which will most probably be the newest one.

  • Do a check to see if you are on page 1, if yes, execute this custom query, if not, do execute this custom query

OK, this is part one. Here is the code

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if( 1 == $paged ) {

    $args1 = array(
        'post_type' => 'my_post_type',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'ignore_sticky_posts' => -1, // remove sticky post
        'tax_query' => array(
            array(
                'taxonomy' => 'my_taxo',
                'field' => 'slug',
                'terms' => array('slug1', 'slug2', 'slug3')
            )
        )
    );

    $post1 = new WP_Query( $args1 );

    if ( $post1->have_posts() ) : 

        while ( $post1->have_posts() ) : $post1->the_post(); 
    
            //YOUR LOOP ELEMENTS GOES HERE
    
        endwhile; 
        wp_reset_postdata(); 
    endif; 
    
}
?>

Now come the tough part. offset is one of the anti-user-friendly parameters to work with. I believe that you are on the homepage from what I can gather. So I'm going to handle this as if you are.

Here is how this will work

  • First of all, I would completely drop the custom query and revert back to the default loop on the homepage. It will makes things more easier and is the more correct way of doing things. Also you will not need any other parameters to set for pagination to work correctly, you can just use default pagination

  • Use pre_get_posts to alter the main query accordingly to set your term on the homepage

  • Together with this, still inside pre_get_posts set your offset

  • You'll also need to subtract your offset from found_posts otherwise your pagination will not work correctly

So, on your homepage you will have

<?php
    
        if ( have_posts() ) :
            // Start the Loop.
            while ( have_posts() ) : the_post();
        
                ///<---YOUR LOOP--->
        
            endwhile;
                
                //<---YOUR PAGINATION--->   
                
            else : 

                //NO POSTS FOUND ORSOMETHING    
                
            endif; 
            
    ?>

Now, the following code will go into functions.php. This is a heavily modified version of code found in this answer from its_me,my own code and your code

function tax_and_offset_homepage( $query ) {
    $offset = 1;
    $ppp = get_option( 'posts_per_page' );
    $paged = $query->query_vars[ 'paged' ];

    if( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', 'my_post_type' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'ignore_sticky_posts', '-1' );
        $tax_query => array(
            array(
                'taxonomy' => 'my_taxo',
                'field' => 'slug',
                'terms' => array('slug1', 'slug2', 'slug3')
            )
        )
        $query->set( 'tax_query', $tax_query );
        if( !is_paged() ) {

            $query->set( 'offset', $offset );

        } else {

            $paged_offset = $offset + ( ($paged - 1) * $ppp );
            $query->set( 'offset', $paged_offset );

        }
    }
}
add_action( 'pre_get_posts', 'tax_and_offset_homepage' );

function homepage_offset_pagination( $found_posts, $query ) {
    $offset = 1;

    if( $query->is_home() && $query->is_main_query() ) {
        return( $found_posts - $offset );
    }
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );

This should give you a basic idea on how to solve your problem. Please feel free to modify all these code to suite your needs

Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209