Site logo

Archived topic

Change Number of Posts Displayed on Subsequent Pages

5 replies · Started by Dan on February 22, 2023

Viewing posts 1–6 of 6

Hi there,

to show one more post on the paged instances of the archive, add this PHP Snippet:

add_action( 'pre_get_posts', 'sk_query_offset', 1 );
function sk_query_offset( &$query ) {
        if ( is_admin() ) {
            return;
        }

	// Before anything else, make sure this is the right query...
	if ( ! ( is_main_query() ) ) {
		return;
	}

        // Don't do anything to Elements.
        if ( 'gp_elements' === $query->get( 'post_type' ) ) {
            return;
        }

	// First, define your desired offset...
	$offset = -1;

	// Next, determine how many posts per page you want (we'll use WordPress's settings)
	$ppp = get_option( 'posts_per_page' );

	// Next, detect and handle pagination...
	if ( $query->is_paged ) {

		// Manually determine page query offset (offset + current page (minus one) x posts per page)
		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );

		// Apply adjust page offset
		$query->set( 'offset', $page_offset );

	}
	else {

		// This is the first page. Set a different number for posts per page
		$query->set( 'posts_per_page', $offset + $ppp );

	}
}

add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );
function sk_adjust_offset_pagination( $found_posts, $query ) {
        if ( is_admin() ) {
            return;
        }

	// Define our offset again...
	$offset = -1;

	// Ensure we're modifying the right query object...
	if ( is_main_query() ) {
		// Reduce WordPress's found_posts count by the offset...
		return $found_posts - $offset;
	}
	return $found_posts;
}

Hi Dan,

Try this snippet instead, set the posts per page to 6 at settings > reading, so the 1st page will only show 5 posts and the rest of the pages will show 6 posts.

add_action( 'pre_get_posts', 'yh_query_offset', 1 );
function yh_query_offset( &$query ) {
        if ( is_admin() ) {
            return;
        }

	// Before anything else, make sure this is the right query...
	if ( ! ( is_main_query() ) ) {
		return;
	}

        // Don't do anything to Elements.
        if ( 'gp_elements' === $query->get( 'post_type' ) ) {
            return;
        }

	// First, define your desired offset...
	$offset = -1;

	// Next, determine how many posts per page you want (we'll use WordPress's settings)
	$ppp = get_option( 'posts_per_page' );

	// Next, detect and handle pagination...
	if ( $query->is_paged ) {

		// Manually determine page query offset (offset + current page (minus one) x posts per page)
		$page_offset = $offset + ( ( $query->query_vars['paged']-1 ) * $ppp );

		// Apply adjust page offset
		$query->set( 'offset', $page_offset );

	}
	else {

		// This is the first page. Set a different number for posts per page
		$query->set( 'posts_per_page', $offset + $ppp );

	}
}

add_filter( 'found_posts', 'sk_adjust_offset_pagination', 1, 2 );
function sk_adjust_offset_pagination( $found_posts, $query ) {
        if ( is_admin() ) {
            return;
        }

	// Define our offset again...
	$offset = -1;
	//get posts per page from WP settings
	$ppp = get_option( 'posts_per_page' );
	$real_page_numbers =ceil(($found_posts - $ppp - $offset) / $ppp) + 1;

	// Ensure we're modifying the right query object...
	if ( is_main_query() && ! is_paged() ) {
		// Reduce WordPress's found_posts count by the offset...
		return $real_page_numbers * ($ppp + $offset);
	}
	 return $real_page_numbers * $ppp;
	
}

brilliant, that worked.

thanks Ying and David!

You are welcome :)

This archived topic is closed to new replies.