Archived topic
Function stopped working
15 replies · Started by Margot on December 3, 2018
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/
Hmm, 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 );
}
}
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
Hmm, so it stopped working on the frontend of the site, or it started to affect the Media Library again?
It stopped working on the frontend. The media library is working fine.
So 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?
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! :)
In 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 :)
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!
Were any other functions or plugins added between the time it was working and now? The code itself looks fine to me.
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.
I 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.
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?
Hmm, I'm stumped. The function works nicely for me.
How are you adding it? Child theme?
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?