Skip to content

Show a zone's posts with a Query Loop block - #233

Merged
GaryJones merged 1 commit into
Automattic:developfrom
faisalahammad:feature/217-zone-query-loop-block
Nov 25, 2026
Merged

GaryJones merged 1 commit into
Automattic:developfrom
faisalahammad:feature/217-zone-query-loop-block

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Sites with block themes had no way to show a zone without custom code or the legacy widget. This adds a "Zone Posts" variation of the Query Loop block with a zone picker in the block settings. The server swaps the block's query for the zone's query, so the front end and the editor preview both list the zone's posts in zone order, with the same rules as z_get_zone_query().

Fixes #217

Changes

src/class-zones.php

Before:

public function get_query( $zone, $args = array() ) {
	// ...build $args...
	return new WP_Query( $args );
}

After:

public function get_query( $zone, $args = array() ) {
	return new WP_Query( $this->get_query_args( $zone, $args ) );
}

public function get_query_args( $zone, $args = array() ) {
	// ...same defaults as before...
	return $args;
}

Why: The block needs the zone's query args, not a WP_Query. Splitting them out keeps one source of truth for zone order, post status and post types.

src/class-block.php (new)

public function filter_query_vars( $query, $block ) {
	if ( ! isset( $block->context['query'][ self::ZONE_KEY ] ) ) {
		return $query;
	}

	return $this->zones->get_query_args(
		absint( $block->context['query'][ self::ZONE_KEY ] ),
		array_intersect_key( $query, array_flip( self::KEPT_ARGS ) )
	);
}

Why: query_loop_block_query_vars handles the front end and rest_post_query handles the editor preview. The REST filter is hooked on every site, so the preview works even when zones only use a custom post type. Only Query Loops with a zoninatorZone key are touched. Only the paging args (per page, offset, page) are kept, so the zone order always wins. With no zone picked, the block shows no posts.

js/zoninator-block.js (new)

Registers the variation (namespace: zoninator/zone-posts) and adds a "Zone" panel with a select that loads zones from /zoninator/v1/zones. The block only shows the Items per page, Offset and Max pages controls, since order and filters come from the zone. Loading and error states are announced to screen readers (VisuallyHidden label, Notice). Plain JS on window.wp, so no build step is needed.

Other

  • src/class-zoninator.php: wires the hooks and builds Block, like the other classes.
  • docs/theme-developers.md and AGENTS.md: document the block.
  • tests/Integration/ZoninatorZoneBlockTest.php: covers zone order, drafts left out, page size, no zone, other Query Loops left alone, the editor preview REST request (with and without a zone, zone 0, logged out), the second page, the filter being registered, and the editor script.

Testing

Test 1: Add the block

  1. Create a zone with a few published posts and one draft.
  2. Create a page, insert "Zone Posts", open the Zone panel and pick the zone.
  3. Publish and view the page.

Result: the editor preview and the front end list the zone's published posts in zone order. The draft is not shown.

Test 2: Reorder the zone

  1. In Zones, drag the posts into a new order and save.
  2. Reload the page.

Result: the block follows the new order.

Test 3: Normal Query Loop

  1. Insert a normal Query Loop on another page.

Result: it works as before, with no Zone panel.

Unit and integration tests pass on single site and multisite. Also checked by hand on a WP 7.1, PHP 8.3 sandbox with Twenty Twenty-Five.

Known limit: the editor preview always reads /wp/v2/posts. If a zone also holds pages or another post type, the preview only shows its posts, but the front end shows them all.

Screenshots

New block, so there is no before state.

Zone Posts block in the editor

@faisalahammad
faisalahammad requested a review from a team as a code owner November 24, 2026 19:38

@GaryJones GaryJones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this, Faisal. A Query Loop variation is the right shape: themes keep control of the markup, and splitting out get_query_args() means the block and z_get_zone_query() follow the same rules. I checked the editor side against WordPress 6.4, our minimum, and its Post Template block already passes extra query keys such as zoninatorZone to the REST request, so the preview works there as well as on trunk.

A few changes before this can go in. The inline comments have the details and suggestions.

1. Version. 0.12.0 shipped without this, and the next release is 1.0.0. The new @since 0.12.0 tags and the "Since 0.12.0" in the docs need to say 1.0.0.

2. Always hook the preview filter. The variation always previews through /wp/v2/posts, but the filter is only added for post types that support zones. Hook rest_post_query unconditionally instead.

3. Tests. The front end is well covered, but the REST path needs more, because the new parameter now works on every /wp/v2/posts request, including logged-out ones. Please add:

  • A request without zoninatorZone is left alone. Request /wp/v2/posts with a post outside any zone and assert the normal date order comes back. This is the REST twin of test_other_query_loops_are_left_alone().
  • zoninatorZone=0 returns no posts. This matches test_block_without_a_zone_shows_no_posts() on the front end.
  • A logged-out request only sees published posts. test_editor_preview_lists_zone_posts_in_zone_order() runs as an editor. Run a copy with no user and a private post in the zone, and assert only the published posts come back. That pins the boundary that matters for a public parameter.
  • The filter is always registered. Once it moves to rest_post_query, assert has_filter( 'rest_post_query', array( \Zoninator(), 'zone_block_rest_query' ) ), so a site that only enables zones for a custom post type still gets a filtered preview.
  • The second page. Render with a page size of 2 and $_GET['query-1-page'] = 2, and assert only the third zone post shows. That proves the offset and paging survive the query swap.

4. Spinner and error accessibility. The spinner has no accessible name, and the error is a plain paragraph that nobody hears. There's a suggestion inline.

5. Docs.

  • docs/theme-developers.md: "Since 1.0.0", and the preview paragraph reworded for the rest_post_query change (suggestion inline).
  • AGENTS.md: the class-block.php line mentions rest_{post_type}_query; please change it to rest_post_query.
  • README.md: step 4 under the description still points people who don't want to write code at the widget only. One sentence there about the Zone Posts block would help block theme users find it.

Comment thread src/class-block.php Resolved
Comment thread src/class-block.php Resolved
Comment thread src/class-zoninator.php Resolved
Comment thread src/class-zoninator.php Resolved
Comment thread js/zoninator-block.js Resolved
Comment thread docs/theme-developers.md Resolved
Comment thread docs/theme-developers.md Resolved
@GaryJones GaryJones added this to the 1.0.0 milestone Nov 24, 2026
faisalahammad added a commit to faisalahammad/zoninator that referenced this pull request Nov 24, 2026
- Mark the block's new code @SInCE 1.0.0
- Hook rest_post_query for the editor preview on every site
- Announce the zone picker's loading and error states
- Add REST, logged-out, filter and paging tests
- Update the block docs, AGENTS.md and README.md

Refs Automattic#233
@faisalahammad

Copy link
Copy Markdown
Contributor Author

Thanks for the review, Gary. All points are done in 01b6089. I also merged develop to fix the conflict in AGENTS.md.

  1. Version: the new tags and the docs now say 1.0.0.
  2. rest_post_query is hooked in the constructor, and the loop in init() is gone.
  3. Tests: I added the five you listed. They cover no zone param, zoninatorZone=0, a logged-out request with a private post, has_filter(), and the second page.
  4. The error uses Notice, and the spinner has a VisuallyHidden label.
  5. Docs: I used your wording in theme-developers.md, changed the AGENTS.md line to rest_post_query, and added one sentence about the block to step 4 in README.md.

Lint, PHPCS and the integration tests pass on single site and multisite (344 tests).

@GaryJones
GaryJones force-pushed the feature/217-zone-query-loop-block branch from 01b6089 to f68e4e8 Compare November 25, 2026 00:32
@GaryJones

GaryJones commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Screenshots from a local test site (WordPress 7.3-alpha, Twenty Twenty-Five) running f68e4e8. The sample zone, "Homepage lead", holds six posts in an order that differs from date order, with a draft in third place.

The zone on the Zones screen

Zones screen with six posts in the Homepage lead zone, including a draft in position 3

Front end, logged out

The Zone Posts block lists the published posts in zone order and leaves out the draft. The normal Query Loop underneath lists the same site by date, for comparison.

Front end with the Zone Posts block in a three-column grid, above a normal Query Loop in date order

Editor, with the block selected

The preview matches the front end, and the Zone panel shows the picker.

Block editor with the Zone Posts block selected and the Zone panel open in the sidebar

Inserter

Block inserter searched for "Zone", showing the Zone Posts variation

@GaryJones

Copy link
Copy Markdown
Contributor

Faisal, I've force-pushed to this branch, so your local copy will be out of date. To pick it up:

git fetch origin
git reset --hard origin/feature/217-zone-query-loop-block

What changed:

  • Rebased onto develop. develop had moved on again, and AGENTS.md conflicted with the Abilities line in the file tree. I kept both lines.
  • Squashed into one commit, f68e4e8. The repo only allows merge commits, so the feature commit, the merge of develop and the review fixes would all have landed in develop as they were. You're still the author. Your message is the same apart from one added sentence about rest_post_query.
  • Changed one test. test_logged_out_editor_preview_only_lists_published_posts is now test_editor_preview_leaves_out_private_posts_the_user_can_read, and it runs as an editor. WordPress never returns private posts to logged-out users, so the old test passed even without the zone query's published-only rule. Editors can read private posts, so now only that rule keeps the post out. I checked that the test fails when the rule is removed.

The integration tests pass locally on single site and multisite.

@GaryJones GaryJones added the type: enhancement New feature or request label Nov 25, 2026
Sites using block themes had no way to show a zone without code or the
legacy widget. This adds a "Zone Posts" variation of the Query Loop
block with a zone picker. The server swaps the block's query for the
zone's query, so the front end and the editor preview list the zone's
posts in zone order, with the same rules as z_get_zone_query().

Zones::get_query_args() is split out of get_query() so the block can
reuse the same query args. The editor preview always reads
/wp/v2/posts, so its filter is hooked on rest_post_query whichever
post types support zones.

Closes Automattic#217
@GaryJones
GaryJones force-pushed the feature/217-zone-query-loop-block branch from f68e4e8 to b916974 Compare November 25, 2026 00:49

@GaryJones GaryJones left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, Faisal. All the feedback is in, and CI is green on the rebased commit.

@GaryJones
GaryJones merged commit 1be6090 into Automattic:develop Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show a zone's posts with a block

2 participants