Changing posts per page on home page breaks pagination on home only

Question

I’m pretty much a beginner here and have a bug in my website. I have a grid layout of 4 x 4 with a static post as my first one on the home page. It displays on the homepage only using <?php if(is_home() && !is_paged()): ?>

This all works fine.. I have set the WP admin to 12 post per page so need to limit the posts on home page to 11 by using this in my functions.php

/** Set home page only to 11 posts, rest of site is set to 12 in admin */

add_action( 'pre_get_posts',  'set_posts_per_page'  );
function set_posts_per_page( $query ) {

  global $wp_the_query;

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_home && !is_paged() ) ) {
    $query->set( 'posts_per_page', 11 );
  }

  return $query;
}

This all works correctly.. however my "last page" button in the pagination nav adds on about 70 dead pages. From 780 to 850…. it all displays correctly everywhere else on the website.. just the home page is miscalculating the number of pages by +70.

Thanks in advance for the help – Jason

P.S. It’s definitely this code that is causing the problem.. when i remove it the pagination is correct.

0
Jason 2 years 2020-12-21T08:10:49-05:00 0 Answers 9 views 0

Leave an answer

Browse
Browse