Search by

concept-image / wp-query

concept-image

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.

Package info

gitlab.com/concept-image-public/wp-query

Issues

pkg:composer/concept-image/wp-query

Statistics

Installs: 1 164

Dependents: 4

Suggesters: 1

Stars: 0

1.2.0 2026-12-03 09:45 UTC

README

WP Query

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

PageCovers
Getting startedInstall, config, X::query() vs Builder::query(), defaults, terminal methods
Building queriesPost types, status, meta and tax clauses, relations and nesting, dates, order, search, every WP_Query argument, withArgs(), when(), scopes
Results and paginationQueryResults, PaginatedResults, pageVar(), out-of-range pages, AJAX
Eager loadingwith…(), nested relations, ACF fields, what each costs, Prime
ArchivesArchive queries on the post type class (wp-custom-post-types 4.2+), X::archive(), migrating a listing composer
FiltersTaxonomyFilter and MetaFilter (faceted counts), MetaOrder, MetaRange, DateRange, Search, GeoFilter, the FilterRegistry in components, AJAX archive replay, custom filters
Geo queriesgeoQuery(), distance order, geocoders
CompatibilityPolylang / WPML, WooCommerce, SearchWP
PerformanceWhat is cached, persistent object cache, benchmark
RecipesFiltered archive, store locator, upcoming events, related posts, second list, AJAX load more, homepage blocks, WooCommerce, SearchWP, member directory, faceted catalog
IDE completionwp 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

License

MIT. See LICENSE.