- This topic has 11 replies, 3 voices, and was last updated 2 years ago by Fernando.
-
AuthorPosts
-
March 21, 2022 at 2:59 pm #2162662Kumara
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!
March 21, 2022 at 4:42 pm #2162720YingStaffCustomer SupportHi Kumara,
Give Tom’s solution a try:
https://generatepress.com/forums/topic/custom-number-of-posts-without-breaking-pagination/#post-1583386March 21, 2022 at 5:02 pm #2162732KumaraHi 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.
March 21, 2022 at 6:40 pm #2162780Fernando Customer SupportHi 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. 🙂
March 21, 2022 at 7:10 pm #2162797KumaraHi 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.
March 21, 2022 at 8:21 pm #2162856Fernando Customer SupportI 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. 🙂
March 29, 2022 at 6:05 pm #2171764KumaraFernando,
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!
March 29, 2022 at 8:07 pm #2171831Fernando Customer SupportHi 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. 🙂
March 29, 2022 at 9:28 pm #2171867KumaraThanks 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 );
March 29, 2022 at 10:41 pm #2171894Fernando Customer SupportHi 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. 🙂
April 17, 2022 at 9:09 pm #2191844KumaraFernando,
Thanks so much for your help! That worked perfectly.
April 17, 2022 at 9:50 pm #2191856Fernando Customer SupportYou’re welcome Kumara! Glad to be of assistance! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.