Download the PHP package stephenharris/wp-query-builder without Composer
On this page you can find all versions of the php package stephenharris/wp-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stephenharris/wp-query-builder
More information about stephenharris/wp-query-builder
Files in stephenharris/wp-query-builder
Package wp-query-builder
Short Description An expressive query builder for WordPress based on Laravel's Query Builder
License GPL-3.0-or-later
Informations about the package wp-query-builder
WP Query builder
An expressive query builder for WordPress based on Laravel's Query Builder. Wraps the $wpdb
global.
How to use in managed environments
If you wish to use this extension in a managed environment, simply install using composer
:
To use the Query builder
How to use in distributed plug-ins
Since there is no way to manage dependencies between plug-ins, bundling this library inside a plug-in will likely cause errors (possibly fatal errors) if used with another plug-in that also includes it.
The safe way to bundle this library inside your plug-in is to use Mozart. This copies it the codebase, but wraps it inside a custom namespace.
You'll need to handle autoloading of the library classes. They can be autoloaded according to PSR-4 from your Mozart destination directory.
Data Sanitisation
The purpose of this library is to provide an expressive and **safe*** way to run queries against your WordPress database (typically involving custom tables).
To this end all values provided are escaped, but note that column and table names are not yet escaped. In any case, even if they were you should be whitelisting any allowed columns/tables: otherwise using user-input, or other untrusted data to determine the column/table could allow an attacker to retrieve data they shouldn't or generate a map of your database.
Querying Results
Retrieving all rows from a table
Retrieve a single row
Retrieve a single column
To retrieve the first column in the returned results:
To retrieve a particular column
To retrieve a value
Basic Where clauses
Adds a WHERE
clause, matching records where the column value equals/not equals/
greater than/ less than / greater than or equals / less than equals (=
/, !=
,
>
, <
, >=
, <=
);
To add an OR
condition, use orWhere($column, $operator, $value)
.
Where column value IN
Adds a WHERE
clause, matching records which have a column value in the array provided:
Where column value IN
Adds a WHERE
clause, matching records which have a column value in the array provided:
Where column value BETWEEN
Adds a WHERE
clause, matching records which have a column value between two specified values:
Search for a field
Performs a LIKE
comparison on one ore more fields fields.
To search in multiple columns you can pass an array of columns:
Complex Where clauses
The andWhere
(and its alias where
), and orWhere
all also accept a WhereCause
instances.
This allows you to build more complex queries, such as nested WHERE
conditions.
Joining
There are four methods for joining:
Methods
leftJoin($table, $firstColumn, $operator, $secondColumn)
rightJoin($table, $firstColumn, $operator, $secondColumn)
innerJoin($table, $firstColumn, $operator, $secondColumn)
fullJoin($table, $firstColumn, $operator, $secondColumn)
Insert records
The Query::insert
method will insert a record with the given column / values
(given an array, where the column is the key). You must call Query::table
with
the name of the table specified.
Delete records
The Query::delete
method will execute a delete command for the table specified,
via the Query::table
or Query::from
command. The delete
method must be
called after any where conditions, as otherwise you will delete the entire table.
Error handling
Calling a method incorrectly (e.g. calling Query::get
without first calling
Query::table
, or passing a field to getColumn
that was not included in the
query Results) will results in a LogicException
(either BadMethodCallException
or InvalidArgumentException
).
If the SQL query fails then a WPQueryBuilder\QueryException
is thrown.