[Resolved] Different number of posts on first page

Home Forums Support [Resolved] Different number of posts on first page

Home Forums Support Different number of posts on first page

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2162662
    Kumara

    Hello,

    I’d like to show 19 results on the first archive page and 22 for every page after that. ‘Blog pages show at most’ is set to 19 and I’m using this code for the other pages

    add_filter('pre_get_posts', 'change_paged_number_archive');
    function hange_paged_number_archive($query){
        if (is_paged() && $query->is_archive) {
            $query->set('posts_per_page', 22);
        }
    	elseif (is_paged() && $query->is_home) {
            $query->set('posts_per_page', 22);
        }
    	elseif ( is_paged() && $query->is_search) {
            $query->set( 'posts_per_page', 22 );
    	}
        return $query;
    }
    

    Seems to work but the second page is always missing the first 3 results. Is there a better way to accomplish this? Or a fix for the offset on the second page? Thanks so much!

    #2162720
    Ying
    Staff
    Customer Support
    #2162732
    Kumara

    Hi Ying, Thanks so much for your suggestion. I’ve followed Tom’s solution. The design works correctly but unfortunately I’m still missing the first 3 entries on the second page after clearing caches.

    #2162780
    Fernando
    Customer Support

    Hi Kumara,

    What does it look like from your end?

    From my end, it seems that there are no missing “entries”.

    See: https://share.getcloudapp.com/Z4u7Lo6P

    Also tested for searches: https://share.getcloudapp.com/JruOb44Y

    Can you try clearing you browser’s cache or viewing the page in incognito? Can you also try viewing it from another device?

    Kindly let us know how it goes. 🙂

    #2162797
    Kumara

    Hi Fernando, thanks for taking a look at this! That’s actually what I’m seeing on my end too. The first entry on that second page should actually be episode 102 rather than 99, the first page ends with episode 103.

    #2162856
    Fernando
    Customer Support

    I see. Thank you for explaining.

    If that’s the case, can you remove this code you have:

    add_filter('pre_get_posts', 'change_paged_number_archive');
    function hange_paged_number_archive($query){
        if (is_paged() && $query->is_archive) {
            $query->set('posts_per_page', 22);
        }
    	elseif (is_paged() && $query->is_home) {
            $query->set('posts_per_page', 22);
        }
    	elseif ( is_paged() && $query->is_search) {
            $query->set( 'posts_per_page', 22 );
    	}
        return $query;
    }

    And replace it with this:

    add_action( 'pre_get_posts', 'sk_query_offset', 1 );
    function sk_query_offset( $query ) {
    
    	// Before anything else, make sure this is the right query...
    	if ( is_admin() || ! ( $query->is_home() || $query->is_main_query() ) ) {
    		return;
    	}
    
    	// First, define your desired offset...
    	$offset = -3;
    
    	// 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 );
    
    	}
    }

    Saw this from WordPress Codex: https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

    Then, in the Reading settings (Tools > Reading), set the blog pages show at most to 22.

    And retain this part:

    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 19;
    
        if( $query->is_home() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );

    or replace it with this from the article:

    add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
    
        //Define our offset again...
        $offset = -3;
    
        //Ensure we're modifying the right query object...
        if ( $query->is_home() ) {
            //Reduce WordPress's found_posts count by the offset... 
            return $found_posts - $offset;
        }
        return $found_posts;
    }

    If the other entries are still missing, can you try disabling other custom functions you have temporarily so we could see if another function is causing this?

    Hope this helps! Kindly let us know how it goes. 🙂

    #2171764
    Kumara

    Fernando,

    Thanks so much! This is working well and resolves my main issue. It has unfortunately created one conflict. I’m using WP Show Posts to display related posts at the bottom of each post. I have the number of posts in the plugin set to 4 but this code seems to over ride that and it is now displaying 19 posts. Settings>Reading ‘blog pages show at most’ is set to 22 posts.

    Thanks again for your help!

    #2171831
    Fernando
    Customer Support

    Hi Kumara,

    May I know the full PHP code you have now? I’ll try to see how we can resolve this issue.

    Hope to hear from you soon. 🙂

    #2171867
    Kumara

    Thanks Fernando,

    Here is what I have currently

    add_action( 'pre_get_posts', 'sk_query_offset', 1 );
    function sk_query_offset( $query ) {
    
    	// Before anything else, make sure this is the right query...
    	if ( ! ( $query->is_archive() || $query->is_main_query() ) ) {
    		return;
    	}
    
    	// First, define your desired offset...
    	$offset = -3;
    
    	// 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', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
    
        //Define our offset again...
        $offset = -3;
    
        //Ensure we're modifying the right query object...
        if ( $query->is_archive() ) {
            //Reduce WordPress's found_posts count by the offset... 
            return $found_posts - $offset;
        }
        return $found_posts;
    }
    
    function my_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = 19;
    
        if( $query->is_archivee() && $query->is_main_query() ) {
            if( !is_paged() ) {
    
                return( $first_page_ppp + ( $found_posts - $first_page_ppp ) * $first_page_ppp / $ppp );
    
            } else {
    
                return( $found_posts - ($first_page_ppp - $ppp) );
    
            }
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'my_offset_pagination', 10, 2 );
    #2171894
    Fernando
    Customer Support

    Hi Kumara,

    Thank you for adding the code you have. Can you try this instead?:

    add_action( 'pre_get_posts', 'sk_query_offset', 1 );
    function sk_query_offset( $query ) {
    
    	// Before anything else, make sure this is the right query...
    	if (  ! $query->is_archive() || ! $query->is_main_query()  ) {
    		return;
    	}
    
    	// First, define your desired offset...
    	$offset = -3;
    
    	// 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', 'myprefix_adjust_offset_pagination', 1, 2 );
    function myprefix_adjust_offset_pagination($found_posts, $query) {
    
        //Define our offset again...
        $offset = -3;
    
        //Ensure we're modifying the right query object...
        if ( $query->is_archive() ) {
            //Reduce WordPress's found_posts count by the offset... 
            return $found_posts - $offset;
        }
        return $found_posts;
    }

    Kindly let us know how it goes. 🙂

    #2191844
    Kumara

    Fernando,

    Thanks so much for your help! That worked perfectly.

    #2191856
    Fernando
    Customer Support

    You’re welcome Kumara! Glad to be of assistance! 🙂

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.