Skip to main content
edited body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
deleted 3907 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
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 );

ALL TOGETHERNOTE: This piece of code broke pagination on the search page. This is now fixed, see the updated code

This is how your complete query will look like that should go into functions.php

function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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 = $found_posts - $offset );$offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );

 

ORIGINAL ANSWERALL TOGETHER

What you want to do is not possible with pagination. However there are a work around

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, onThis is how 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 codecomplete query will look like that should 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() && !is_admin()) {
        $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 );
    $ppp = get_option('posts_per_page');
  if( !is_paged() )$offset {
= 1;
    if (!$query->is_paged()) {
      $query->set( 'offset''posts_per_page', $offset + $ppp);
 
        } else {
 
            $paged_offset$offset = $offset + ( ($paged $query- >query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set( 'offset', $paged_offset $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 = $found_posts - $offset );$offset;
    }
    return $found_posts;
}
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

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 );

ALL TOGETHER

This is how your complete query will look like that should go into functions.php

function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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 );

 

ORIGINAL ANSWER

What you want to do is not possible with pagination. However there are a work around

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

NOTE: This piece of code broke pagination on the search page. This is now fixed, see the updated code

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

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

ALL TOGETHER

This is how your complete query will look like that should go into functions.php

function tax_and_offset_homepage( $query ) {
  if ($query->is_home() && $query->is_main_query() && !is_admin()) {
    $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 );
    $ppp = get_option('posts_per_page');
    $offset = 1;
    if (!$query->is_paged()) {
      $query->set('posts_per_page',$offset + $ppp);
    } else {
      $offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
      $query->set('posts_per_page',$ppp);
      $query->set('offset',$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() ) {
        $found_posts = $found_posts - $offset;
    }
    return $found_posts;
}
add_filter( 'found_posts', 'homepage_offset_pagination', 10, 2 );
deleted 38 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

As you are not replying to my comments and not giving me any feedback, I still believe you are on the homepage and will also treat this as such. So this is my new solution

As you are not replying to my comments and not giving me any feedback, I still believe you are on the homepage. So this is my new solution

I still believe you are on the homepage and will also treat this as such. So this is my new solution

Add edit
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading
deleted 19 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
Loading