- This topic has 15 replies, 2 voices, and was last updated 4 years, 5 months ago by
Tom.
-
AuthorPosts
-
December 3, 2018 at 10:43 am #747014
Margot
I’m having trouble with a function that stopped working recently. Not 100% sure but I think it might have stopped working after a recent GP update. The function is:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 ); function tu_change_posts_per_page( &$query ) { if ( ! is_main_query() || is_admin() ) { return; } if ( ! $query->is_paged && ( $query->is_archive || $query->is_category ) ) { $query->set( 'posts_per_page', 21 ); } }
This is the same function that was corrected on this thread> https://generatepress.com/forums/topic/issues-with-media-library-due-to-gp/
December 3, 2018 at 5:14 pm #747267Tom
Lead DeveloperLead DeveloperHmm, nothing in the update would affect that function.
Try this:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 ); function tu_change_posts_per_page( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( ! $query->is_paged && ( $query->is_archive || $query->is_category ) ) { $query->set( 'posts_per_page', 21 ); } }
December 3, 2018 at 5:31 pm #747276Margot
Just tested that out. It didn’t change anything. Is there something that could be overriding it? This is on the child theme functions.php
December 3, 2018 at 6:29 pm #747301Tom
Lead DeveloperLead DeveloperHmm, so it stopped working on the frontend of the site, or it started to affect the Media Library again?
December 4, 2018 at 7:01 am #747842Margot
It stopped working on the frontend. The media library is working fine.
December 4, 2018 at 6:05 pm #748257Tom
Lead DeveloperLead DeveloperSo the link you provided is to the second page of results, but the function is set to not work when viewing paginated results (
! $query->is_paged
).Are you wanting it to apply to paged results as well?
December 5, 2018 at 6:49 am #748769Margot
I want the function to work for pages 2 – β of blog results. So everything except for page 1 should be affected and this would be across tags and categories results as well.
Related question:
Is it a better practice to have the function applied to page 1 only and let the rest of the page results be the “Blog pages show at most” value? Or vice versa?ps. Thank you for bearing with me! π
December 5, 2018 at 9:36 am #748904Tom
Lead DeveloperLead DeveloperIn that case, you would do this:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 ); function tu_change_posts_per_page( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( $query->is_paged && ( $query->is_archive || $query->is_category ) ) { $query->set( 'posts_per_page', 21 ); } }
So that code would apply to pages 2 – β, while the first page would use your original posts per page option.
I don’t think either way is better practice, really. It’s all the same π
December 5, 2018 at 11:19 am #749004Margot
Hmm.. I just tried cutting and pasting it (and clearing cache) but I’m not seeing any change. I’m still at 19 posts for the paginated pages instead of 21.
Thanks for your input about the best practices question!
December 5, 2018 at 6:11 pm #749267Tom
Lead DeveloperLead DeveloperWere any other functions or plugins added between the time it was working and now? The code itself looks fine to me.
December 6, 2018 at 7:26 am #749793Margot
No added or changed functions since then. I think it changed after I updated a few plugins as well as GP. Tried deactivating the plugins that were updated recently (Webcraftic Clearfy and The SEO Framework) but saw no change.
December 6, 2018 at 4:44 pm #750160Tom
Lead DeveloperLead DeveloperI just tried it on my test site and the function is working correctly.
It’s possible that you have a plugin conflicting with it.
Try adjusting it to this:
add_action( 'pre_get_posts', 'tu_change_posts_per_page', 999 ); function tu_change_posts_per_page( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( $query->is_paged && ( $query->is_archive || $query->is_category ) ) { $query->set( 'posts_per_page', 21 ); } }
If that doesn’t work, you’ll need to deactivate your plugins one by one to check for conflicts.
December 8, 2018 at 7:04 am #751475Margot
Hi again! I pasted the snippet above and then deactivated every plugin I have except for GP Premium. I’m still not seeing the function being applied to my paginated pages. Is there something else I’m missing or that I need to check out?
December 8, 2018 at 9:36 am #751556Tom
Lead DeveloperLead DeveloperHmm, I’m stumped. The function works nicely for me.
How are you adding it? Child theme?
December 8, 2018 at 3:42 pm #751705Margot
Yeah this is really strange. I am using a child theme and the function is pasted into the child theme’s functions.php
Wondering if I should try removing all other child theme functions other than that one to see if there’s something else conflicting with it?
-
AuthorPosts
- You must be logged in to reply to this topic.