You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+
## Overview
6
+
7
+
HM Query Loop is a WordPress plugin that extends the core Query Loop block with advanced controls for managing multiple query loops on a single page. It provides three main features: posts per page override for inherited queries, hide on paginated pages, and exclude already displayed posts.
8
+
9
+
## Development Commands
10
+
11
+
### Build and Development
12
+
-`npm run start` - Start development build with watch mode
13
+
-`npm run build` - Create production build (required before testing)
14
+
-`npm run lint:js` - Lint JavaScript files
15
+
-`npm run lint:css` - Lint CSS/SCSS files
16
+
-`npm run format` - Format all files
17
+
18
+
### Testing
19
+
-`npm run wp-env start` - Start WordPress test environment (ports 8888 dev, 8889 tests)
20
+
-`npm run test:e2e` - Run Playwright end-to-end tests
21
+
-`npm run test:e2e:debug` - Run tests in debug mode
22
+
-`npm run test:e2e:watch` - Run tests in watch mode (reruns on changes)
23
+
-`npm run wp-env stop` - Stop WordPress environment
24
+
25
+
**Important**: Always run `npm run build` before running tests, as tests run against built assets.
26
+
27
+
## Architecture
28
+
29
+
### Block Extension Approach
30
+
The plugin uses WordPress block filters to extend the `core/query` block without creating a custom block variant. This allows it to work with any Query Loop block while preserving core functionality.
31
+
32
+
### Context System
33
+
The plugin exposes a `hm-query-loop/settings` context object from `core/query` to `core/post-template`:
34
+
```js
35
+
{
36
+
perPage: number |undefined, // Custom posts per page value
37
+
hideOnPaged: boolean, // Whether to hide on paginated pages
38
+
excludeDisplayed: boolean // Whether to exclude displayed posts
39
+
}
40
+
```
41
+
42
+
Context is registered in both JavaScript (via `blocks.registerBlockType` filters in src/index.js) and PHP (via `filter_block_metadata` in hm-query-loop.php).
43
+
44
+
### Dual Query Modification Strategy
45
+
46
+
The plugin handles two different query scenarios:
47
+
48
+
**For Inherited Queries** (uses main WP_Query):
49
+
-`pre_render_block` - Captures block attributes, checks pagination visibility, re-runs main query with modified args
50
+
- Main query is modified before rendering to apply settings
51
+
-`render_block` - Returns empty string if block should be hidden on paginated pages
52
+
53
+
**For Non-Inherited Queries** (custom WP_Query):
54
+
-`query_loop_block_query_vars` filter - Passes block attributes into WP_Query vars
55
+
- This filter only fires for non-inherited queries, which is why the dual approach is necessary
56
+
57
+
### Post Tracking
58
+
-`the_posts` filter tracks displayed post IDs across all query loops on a page
59
+
- Global `$displayed_post_ids` array accumulates IDs from rendered query loops
60
+
- Subsequent query loops with `excludeDisplayed` enabled filter out tracked IDs via `post__not_in`
61
+
62
+
### Editor Preview Synchronization
63
+
In src/index.js, a `useEffect` hook syncs the `hmQueryLoop.perPage` attribute to `query.perPage` to reflect the override in the editor preview. The `withPostTemplateStyles` filter adds inline CSS to hide excess posts in the editor beyond the `perPage` limit.
64
+
65
+
## Key Files
66
+
67
+
-`hm-query-loop.php` - Main plugin file with all PHP hooks and query modification logic
68
+
-`src/index.js` - Block filters for adding inspector controls and editor preview behavior
69
+
-`tests/e2e/posts-per-page.spec.js` - E2E tests for posts per page functionality
70
+
-`tests/e2e/fixtures.js` - Playwright test fixtures for WordPress admin
71
+
72
+
## Testing Environment
73
+
74
+
Tests use `@wordpress/env` with WordPress 6.7.1, configured in `.wp-env.json`. The environment includes TwentyTwentyFour and TwentyTwentyFive themes. Tests run on port 8889 and use Playwright with `@wordpress/e2e-test-utils-playwright`.
75
+
76
+
## Important Implementation Notes
77
+
78
+
- The plugin modifies queries without creating database entries or custom post types
79
+
- All settings are stored as block attributes in post content
80
+
- The `$original_paged` global preserves the original pagination state when blocks override it
81
+
- Hidden blocks (via `hideOnPaged`) still track their post IDs for exclusion purposes
82
+
- The plugin works with both FSE templates and classic posts/pages
0 commit comments