Skip to content

Fix: Post ID ordering not working on frontend#151

Open
ryanwelcher wants to merge 4 commits intotrunkfrom
fix/order-by-post-id
Open

Fix: Post ID ordering not working on frontend#151
ryanwelcher wants to merge 4 commits intotrunkfrom
fix/order-by-post-id

Conversation

@ryanwelcher
Copy link
Owner

Closes #148

Problem

When "Post Order By" is set to "Post ID" in the AQL block, posts display in correct order in the editor but fall back to date ordering on the frontend. Query Monitor confirms WP_Query uses ORDER BY post_date instead of ORDER BY ID.

Root Cause

  • UI passes lowercase 'id' value to backend
  • REST API (editor) automatically normalizes 'id' → 'ID'
  • WP_Query (frontend) is case-sensitive and requires uppercase 'ID'
  • Unrecognized lowercase 'id' causes fallback to default date ordering

Solution

Created OrderBy trait that normalizes the orderBy parameter:

  • Accepts lowercase 'id' from block attributes
  • Converts to uppercase 'ID' for WP_Query compatibility
  • Ensures consistent behavior across editor and frontend

Changes

  • Add includes/Traits/OrderBy.php with normalization logic
  • Update Query_Params_Generator.php to use OrderBy trait
  • Update query-loop.php to process orderBy parameter
  • Add comprehensive e2e tests (6 tests, 100% passing)
  • Add test documentation and blueprint setup
  • Update CLAUDE.md with architecture notes

Testing

E2e tests verify:

  • UI control displays Post ID option
  • Block attributes set correctly
  • Editor preview orders by ID
  • Frontend orders by ID (not date)
  • Ascending and descending order work
  • Switching between order types works

Tests confirmed:

  • ✅ 6/6 pass with fix
  • ❌ 2/6 fail without fix (reproduces bug)

Fixes #148

## Problem
When "Post Order By" is set to "Post ID" in the AQL block, posts display
in correct order in the editor but fall back to date ordering on the
frontend. Query Monitor confirms WP_Query uses `ORDER BY post_date`
instead of `ORDER BY ID`.

## Root Cause
- UI passes lowercase 'id' value to backend
- REST API (editor) automatically normalizes 'id' → 'ID'
- WP_Query (frontend) is case-sensitive and requires uppercase 'ID'
- Unrecognized lowercase 'id' causes fallback to default date ordering

## Solution
Created OrderBy trait that normalizes the orderBy parameter:
- Accepts lowercase 'id' from block attributes
- Converts to uppercase 'ID' for WP_Query compatibility
- Ensures consistent behavior across editor and frontend

## Changes
- Add `includes/Traits/OrderBy.php` with normalization logic
- Update `Query_Params_Generator.php` to use OrderBy trait
- Update `query-loop.php` to process orderBy parameter
- Add comprehensive e2e tests (6 tests, 100% passing)
- Add test documentation and blueprint setup
- Update CLAUDE.md with architecture notes

## Testing
E2e tests verify:
- UI control displays Post ID option
- Block attributes set correctly
- Editor preview orders by ID
- Frontend orders by ID (not date)
- Ascending and descending order work
- Switching between order types works

Tests confirmed:
- ✅ 6/6 pass with fix
- ❌ 2/6 fail without fix (reproduces bug)
@ryanwelcher ryanwelcher added this to the 4.4.0 milestone Feb 13, 2026
@ryanwelcher ryanwelcher self-assigned this Feb 13, 2026
@ryanwelcher ryanwelcher added the bug Something isn't working label Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Post ID order doesn't work on frontend, uses published date sorting instead

1 participant

Comments