concept-image / wp-query
Fluent WP_Query builder for Acorn themes: every WP_Query argument, explicit tax/meta/date clause trees, single-SQL terminals, eager loading (meta, terms, thumbnails, authors, ACF relations), Polylang/WooCommerce/SearchWP compatibility and data-only listing filters.
Requires
- php: >=8.4
- concept-image/wp-hook-attributes: ^0.1
- roots/acorn: ^6.4
Requires (Dev)
- brain/monkey: ^2.6
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^4.0|^5.0
- php-stubs/acf-pro-stubs: ^6.5
- szepeviktor/phpstan-wordpress: ^2.0
Suggests
- concept-image/wp-custom-post-types: Post type and taxonomy classes whose static name() this package resolves, with PostType::query(): ^4.2
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.1.35
- 0.1.34
- 0.1.33
- 0.1.32
- 0.1.31
- 0.1.30
- 0.1.29
- 0.1.28
- 0.1.27
- 0.1.26
- 0.1.25
- 0.1.24
- 0.1.23
- 0.1.22
- 0.1.21
- 0.1.20
- 0.1.19
- 0.1.18
- 0.1.17
- 0.1.16
- 0.1.15
- 0.1.14
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.11
- 0.0.10
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- 0.0.1-beta.1
- 0.0.1-beta
This package is auto-updated.
Last update: 2026-12-03 08:46:12 UTC
README
A fluent WP_Query builder for Acorn themes
For Acorn (Sage) themes: every WP_Query argument, explicit tax / meta / date clause trees, one SQL query per call, eager loading for cards (no N+1), Polylang / WPML / WooCommerce / SearchWP
compatibility, #[Scope] methods, and data-only listing filters (taxonomy and custom field with faceted counts, numeric and
date ranges, search, geo query).
Results stay WP_Post objects: every query goes through WP_Query, its filters and the object cache.
Installation
composer require concept-image/wp-query
Requires PHP 8.4 and roots/acorn ^6.4. The service provider is discovered by Acorn. Publish the config only to
change a value: wp acorn vendor:publish --tag=wp-query-config.
With concept-image/wp-custom-post-types 4.2+, every post type class has X::query(), and archives declare their
query on the class (implements HasArchiveQuery); the template's View Composer reads it with X::archive().
In 30 seconds
use App\CustomPostTypes\News;
use App\Taxonomies\NewsCat;
use ConceptImage\WpQuery\PostQuery\Builder;
$news = News::query()
->whereTax(NewsCat::class, ['tech'])
->whereNumberGreaterEqual('reading_time', 5)
->publishedAfter('-1 year')
->withThumbnail() // what the cards read:
->withTerms(NewsCat::class) // one query per relation,
->withAcf(['related' => fn (Builder $q) => $q->withThumbnail()]) // not one per card
->paginate(12);
foreach ($news as $post) { /* WP_Post, caches primed */ }
echo $news->getPagination();
News::query()->whereMetaIn('level', ['a', 'b'])->count(); // int, no post loaded
News::query()->postIn($ids)->orderBy('post__in')->all(); // empty $ids → no post, no SQL
News::query()->search($keyword)->engine('default')->get(); // SearchWP when active
On a 24-card archive page (thumbnail, terms with term meta, author, an ACF relationship and its posts' thumbnails), a
classic WP_Query loop runs 166 SQL queries; the builder with eager loading runs 16, whatever the page size, and
renders the page 7× faster (benchmark).
Documentation
| Page | Covers |
|---|---|
| Getting started | Install, config, X::query() vs Builder::query(), defaults, terminal methods |
| Building queries | Post types, status, meta and tax clauses, relations and nesting, dates, order, search, every WP_Query argument, withArgs(), when(), scopes |
| Results and pagination | QueryResults, PaginatedResults, pageVar(), out-of-range pages, AJAX |
| Eager loading | with…(), nested relations, ACF fields, what each costs, Prime |
| Archives | Archive queries on the post type class (wp-custom-post-types 4.2+), X::archive(), migrating a listing composer |
| Filters | TaxonomyFilter and MetaFilter (faceted counts), MetaOrder, MetaRange, DateRange, Search, GeoFilter, the FilterRegistry in components, AJAX archive replay, custom filters |
| Geo queries | geoQuery(), distance order, geocoders |
| Compatibility | Polylang / WPML, WooCommerce, SearchWP |
| Performance | What is cached, persistent object cache, benchmark |
| Recipes | Filtered archive, store locator, upcoming events, related posts, second list, AJAX load more, homepage blocks, WooCommerce, SearchWP, member directory, faceted catalog |
| IDE completion | wp acorn query:ide-helper for #[Scope] methods |
IDE completion
Builder methods complete as is. #[Scope] methods go through __call(): run wp acorn query:ide-helper after adding
one; it writes _ide_helper_wp_query.php (git-ignore it) for PhpStorm and Intelephense. See IDE.
AI files
The package ships a Claude Code skill and rules for sites: wp acorn vendor:publish --tag=wp-query-ai (or
wp-starter-theme's wp acorn starter:ai) publishes them to the Bedrock root's .claude/.
Development
composer ci # Pint, Larastan (level max), Pest
See CHANGELOG and UPGRADE. Inspired by Pollora/Query (MIT).
Credits
- Banner icon: Molecule icons created by Magnific - Flaticon
License
MIT. See LICENSE.