[Resolved] Follow up: Different number of posts on ‘paged’ archives

Home Forums Support [Resolved] Follow up: Different number of posts on ‘paged’ archives

Home Forums Support Follow up: Different number of posts on ‘paged’ archives

Viewing 15 posts - 16 through 30 (of 36 total)
  • Author
    Posts
  • #2049871
    Pete

    No, in the general settings for wp, the Reading settings, number of blog posts to show.

    #2051428
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Try adding this:

    add_filter( 'generate_elements_custom_args', function( $args ) {
        $args['suppress_filters'] = true;
    
        return $args;
    } );

    It should tell Elements to ignore the pre_get_posts filter.

    Let us know 🙂

    #2051482
    Pete

    No joy unfortunately Tom.

    #2052595
    Tom
    Lead Developer
    Lead Developer

    Can you confirm that the problem goes away if you remove that pre_get_posts filter?

    If it does, can you share it here so I can take a closer look at exactly what you’re adding?

    Thanks!

    #2052633
    Pete

    When I remove the code from the original question, the problem goes away.

    #2053396
    Tom
    Lead Developer
    Lead Developer

    Is this the exact code?:

    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_category('clubs') || 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', 'sk_adjust_offset_pagination', 1, 2 );
    function sk_adjust_offset_pagination( $found_posts, $query ) {
    
    	// Define our offset again...
    	$offset = -3;
    
    	// Ensure we're modifying the right query object...
    	if ( $query->is_category('clubs') && is_main_query() ) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $found_posts - $offset;
    	}
    	return $found_posts;
    }

    If so, can you comment out the add_action line and see if it fixes it? If not, can you try commenting out the add_filter line?

    Just trying to narrow down exactly which function is causing the conflict.

    Thanks!

    #2055184
    Pete

    Yes, the exact code, and no luck with both fixes.

    #2056287
    Tom
    Lead Developer
    Lead Developer

    So the issue only goes away if you remove both functions? Not only one of them?

    #2057166
    Pete

    Ok…
    1. I’ve added your filter
    2. removing add_action stops the solution, but also stops(doesn’t create) the problem – no difference
    3. removing add_filter gives me the solution (different paged) but also creates the problem – no map or dropdown.

    add_filter( 'generate_elements_custom_args', function( $args ) {
    
        $args['suppress_filters'] = true;
    
        return $args;
    
    } );
    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_category('clubs') || 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', 'sk_adjust_offset_pagination', 1, 2 );
    function sk_adjust_offset_pagination( $found_posts, $query ) {
    
    	// Define our offset again...
    	$offset = -3;
    
    	// Ensure we're modifying the right query object...
    	if ( $query->is_category('clubs') && is_main_query() ) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $found_posts - $offset;
    	}
    	return $found_posts;
    }
    #2058155
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, this is the issue we’re trying to debug, correct?: https://generatepress.com/forums/topic/different-number-of-posts-on-paged-archives/#post-2048561

    If so, which one of the above 3 methods fixed the Hooks not displaying? Removing add_action or add_filter?

    I ask just so we can pinpoint which function is causing the Hooks issue. Then we might be able to tweak the problem function so it ignores Elements.

    #2058183
    Pete

    Just to confirm, this is the issue we’re trying to debug, correct?: https://generatepress.com/forums/topic/different-number-of-posts-on-paged-archives/#post-2048561

    Correct

    If so, which one of the above 3 methods fixed the Hooks not displaying? Removing add_action or add_filter?

    None of the above 3 fixed the Hooks not displaying. The original post solved the different number of posts on different pages BUT nothing has solved the problem of the Hooks not displaying.

    I’ll make a screencast to show you

    #2058861
    Tom
    Lead Developer
    Lead Developer

    Oh, so the Hooks not displaying issue wasn’t caused by the original different number of posts solution? I though that the solution itself was causing the Hooks to not appear.

    #2058869
    Pete

    Youtube explanation

    #2060199
    Tom
    Lead Developer
    Lead Developer

    Thanks for the video!

    It looks like the first function is causing the issue.

    Let’s try this as the entire snippet instead of what you have now:

    add_action( 'pre_get_posts', 'sk_query_offset', 1 );
    function sk_query_offset( &$query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Before anything else, make sure this is the right query...
    	if ( ! ( $query->is_category('clubs') || is_main_query() ) ) {
    		return;
    	}
    
            // Don't do anything to Elements.
            if ( 'gp_elements' === $query->get( 'post_type' ) ) {
                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', 'sk_adjust_offset_pagination', 1, 2 );
    function sk_adjust_offset_pagination( $found_posts, $query ) {
            if ( is_admin() ) {
                return;
            }
    
    	// Define our offset again...
    	$offset = -3;
    
    	// Ensure we're modifying the right query object...
    	if ( $query->is_category('clubs') && is_main_query() ) {
    		// Reduce WordPress's found_posts count by the offset...
    		return $found_posts - $offset;
    	}
    	return $found_posts;
    }

    Let me know 🙂

    #2060392
    Pete

    Thanks Tom. that did the trick. Thanks for persevering. Happy new year!

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