[Support request] Function stopped working

Home Forums Support [Support request] Function stopped working

Home Forums Support Function stopped working

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #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/

    #747267
    Tom
    Lead Developer
    Lead Developer

    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 );
        }
    }
    #747276
    Margot

    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

    #747301
    Tom
    Lead Developer
    Lead Developer

    Hmm, so it stopped working on the frontend of the site, or it started to affect the Media Library again?

    #747842
    Margot

    It stopped working on the frontend. The media library is working fine.

    #748257
    Tom
    Lead Developer
    Lead Developer

    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?

    #748769
    Margot

    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! πŸ™‚

    #748904
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #749004
    Margot

    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!

    #749267
    Tom
    Lead Developer
    Lead Developer

    Were any other functions or plugins added between the time it was working and now? The code itself looks fine to me.

    #749793
    Margot

    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.

    #750160
    Tom
    Lead Developer
    Lead Developer

    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.

    #751475
    Margot

    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?

    #751556
    Tom
    Lead Developer
    Lead Developer

    Hmm, I’m stumped. The function works nicely for me.

    How are you adding it? Child theme?

    #751705
    Margot

    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?

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