Skip to content
Corex

QueryBuilder

Corex\Database\QueryBuilder · class · Core layer

A fluent, chainable query that produces a safe, capped WP_Query args array and (via the executor) a Collection of Models. It is a pure arg-builder — it never instantiates WP_Query (that is the executor’s single job), which keeps the cap/binding logic unit-testable. Covers the complex cases a real app needs: declared-field meta queries with AND/OR relation, taxonomy queries, date ranges, numeric/string ordering by meta, search, pagination, and eager-loaded belongs-to relations (no N+1, in the executor). Every variable value is passed as data into the relevant *_query clause — never interpolated — so WP_Query prepares it.

__construct(string $modelClass, QueryExecutor $executor)

Section titled “__construct(string $modelClass, QueryExecutor $executor)”

where(string $field, mixed $value, string $compare): self

Section titled “where(string $field, mixed $value, string $compare): self”

orWhere(string $field, mixed $value, string $compare): self

Section titled “orWhere(string $field, mixed $value, string $compare): self”

Add a declared-field meta condition under an OR relation with the others.

whereMeta(string $key, mixed $value, string $compare, string $type): self

Section titled “whereMeta(string $key, mixed $value, string $compare, string $type): self”

A meta condition on a raw meta key (not a declared model field), with an explicit comparison and SQL type (CHAR/NUMERIC/DATE/…). For nested/advanced meta_query.

whereBetween(string $field, int|float $min, int|float $max): self

Section titled “whereBetween(string $field, int|float $min, int|float $max): self”

A declared-field numeric range (inclusive), e.g. salary between 50k and 90k.

whereTax(string $taxonomy, int|string|array $terms, string $field, string $operator): self

Section titled “whereTax(string $taxonomy, int|string|array $terms, string $field, string $operator): self”

A taxonomy condition. Terms may be a single term or a list; matched by term_id (default) or another field (slug, name).

whereDate(array|string|null $after, array|string|null $before, bool $inclusive): self

Section titled “whereDate(array|string|null $after, array|string|null $before, bool $inclusive): self”

Restrict by the post date. Either bound may be null; dates are any strtotime-able string (e.g. ‘2026-01-01’) or a WP date array.

Free-text search across post title/content (WP_Query s).

orderBy(string $field, string $direction): self

Section titled “orderBy(string $field, string $direction): self”

orderByNumeric(string $field, string $direction): self

Section titled “orderByNumeric(string $field, string $direction): self”

Order by a declared meta field numerically (meta_value_num), e.g. price or salary, so “100” sorts after “20” instead of lexically before it.

Page the results: sets the per-page size (capped) and the page, and enables the found-rows count so total/pages are available. Opt-in — unpaged queries keep no_found_rows true for speed.