Skip to content

Fix pagination reset issue for inherited queries by using get_query_var#51

Open
agarciar wants to merge 1 commit into
a8cteam51:trunkfrom
agarciar:fix/inherited-query-pagination
Open

Fix pagination reset issue for inherited queries by using get_query_var#51
agarciar wants to merge 1 commit into
a8cteam51:trunkfrom
agarciar:fix/inherited-query-pagination

Conversation

@agarciar
Copy link
Copy Markdown

@agarciar agarciar commented Jun 13, 2025

This PR addresses Issue #50, where pagination state is lost when using “Load More” or “Infinite Scroll” with Update URL enabled on archive pages that rely on inherited queries.

The fix uses WordPress’s native get_query_var('paged') to determine the current page when dealing with inherited queries, falling back to $_GET for non-inherited ones.

Summary by CodeRabbit

  • Bug Fixes
    • Improved pagination handling to ensure the correct page number and query are used based on the context, leading to more accurate navigation in inherited and non-inherited scenarios.

Replaced $_GET-based page detection with get_query_var('paged') when the query is inherited. This ensures the correct current page is preserved when navigating back using the browser’s back button, preventing duplicated posts and broken pagination flow.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 13, 2025

Walkthrough

The logic for determining the current page number in the render method of the Plugin class has been updated. Now, when the query context indicates inheritance, the method retrieves the page number from the WordPress paged query variable, defaulting to 1 if not present, and uses the global $wp_query object. If inheritance is not indicated, the method continues to derive the page number from the $_GET superglobal using a constructed page key and creates a new WP_Query instance. The $page_parameter variable is set accordingly based on the inheritance context. No changes were made to method signatures.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/Plugin.php (1)

254-256: Tighten sanitisation of $_GET input

Casting to (int) still permits negative values. Using absint() (or filter_input() with FILTER_VALIDATE_INT) guarantees a non-negative integer and avoids pointless DB queries:

-		: ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
+		: ( empty( $_GET[ $page_key ] ) ? 1 : absint( $_GET[ $page_key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5cf2700 and 4963a60.

📒 Files selected for processing (1)
  • src/Plugin.php (1 hunks)
🔇 Additional comments (1)
src/Plugin.php (1)

257-259: Solid choice of $wp_query & dynamic parameter key

Using the global $wp_query when inheriting and falling back to a fresh WP_Query for custom contexts keeps the behaviour consistent with core. Nice work.

Comment thread src/Plugin.php
Comment on lines +254 to +256
$page = $inherit
? ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 )
: ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

❓ Verification inconclusive

Avoid duplicate get_query_var() calls & cover front-page edge-case

Two identical get_query_var( 'paged' ) calls are executed and the logic silently drops to 1 when WordPress sets paged = 0 (static front-page). Both issues are easy to eliminate:

-	$page = $inherit
-		? ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 )
+	$paged = absint( get_query_var( 'paged' ) );
+	if ( 0 === $paged ) {              // front-page uses `page`
+		$paged = absint( get_query_var( 'page' ) );
+	}
+	$page = $inherit
+		? max( 1, $paged )
 		: ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

• One function call instead of two.
• Handles the paged = 0 scenario.
• Uses absint()/max() for defensive safety.


Refine pagination logic: eliminate duplicate get_query_var() calls and handle static front-page

Introduce a single, sanitized $paged variable, fall back to page when on a static front-page, and ensure a minimum of 1:

• Declare and sanitize $paged once.
• If $paged is zero (static front-page), pull from get_query_var('page').
• Use max(1, $paged) to enforce a floor of 1.
• Remove the duplicate get_query_var('paged') invocation.

-       $page = $inherit
-           ? ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 )
+       $paged = absint( get_query_var( 'paged' ) );
+       if ( 0 === $paged ) {                        // static front-page uses `page`
+           $paged = absint( get_query_var( 'page' ) );
+       }
+       $page = $inherit
+           ? max( 1, $paged )
        : ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$page = $inherit
? ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 )
: ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$paged = absint( get_query_var( 'paged' ) );
if ( 0 === $paged ) { // static front-page uses `page`
$paged = absint( get_query_var( 'page' ) );
}
$page = $inherit
? max( 1, $paged )
: ( empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
🤖 Prompt for AI Agents
In src/Plugin.php around lines 254 to 256, the pagination logic calls
get_query_var('paged') twice and does not handle the case when 'paged' is zero
on a static front-page. Refactor by assigning get_query_var('paged') once to a
variable, then if that value is zero, assign get_query_var('page') to it. Use
max(1, absint($paged)) to ensure the page number is at least 1. Replace the
original conditional with this sanitized variable and remove the duplicate
get_query_var('paged') calls.

@robrobsn
Copy link
Copy Markdown
Contributor

@agarciar Thank you for the PR. The latest version 1.0.17 fixes the issue. #57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants