Site logo

Archived topic

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

35 replies · Started by Pete on December 15, 2021

Viewing posts 16–30 of 36

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

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 :)

No joy unfortunately Tom.

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!

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

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!

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

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

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;
}

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

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.

Youtube explanation

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 :)

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

This archived topic is closed to new replies.