PHPackages                             mehedimi/wp-query-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mehedimi/wp-query-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

mehedimi/wp-query-builder
=========================

A database query builder for WordPress

0.17(5mo ago)3814751MITPHPPHP &gt;=7.4

Since Aug 15Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/mehedimi/wp-query-builder)[ Packagist](https://packagist.org/packages/mehedimi/wp-query-builder)[ RSS](/packages/mehedimi-wp-query-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)Dependencies (8)Versions (14)Used By (1)

[![WP Query Builder](https://camo.githubusercontent.com/8909e35971e2fe2bbe142f9416730abeb94f381d1be90c5068f984036a2e9191/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f575025323051756572792532304275696c6465722e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d65686564696d6925324677702d71756572792d6275696c646572267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d416e2b656c6567616e742b71756572792b6275696c6465722b666f722b576f72645072657373266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d646f63756d656e742d646f776e6c6f6164)](https://camo.githubusercontent.com/8909e35971e2fe2bbe142f9416730abeb94f381d1be90c5068f984036a2e9191/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f575025323051756572792532304275696c6465722e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d65686564696d6925324677702d71756572792d6275696c646572267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d416e2b656c6567616e742b71756572792b6275696c6465722b666f722b576f72645072657373266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313235707826696d616765733d646f63756d656e742d646f776e6c6f6164)

WP Query Builder
----------------

[](#wp-query-builder)

WP Query Builder is package for developers, which can simplify query writing experience in WordPress.

- [Installation](#installation)
- [Running SQL Queries](#running-queries)
- [Running Database Queries](#running-database-queries)
    - [Retrieving All Rows From A Table](#retrieving-all-rows-from-a-table)
    - [Retrieving A Single Row](#retrieving-a-single-row)
    - [Aggregates](#aggregates)
- [Joins](#joins)
- [Basic Where Clauses](#basic-where-clauses)
    - [Where Clauses](#where-clauses)
    - [Additional Where Clauses](#additional-where-clauses)
- [Ordering, Grouping, Limit &amp; Offset](#ordering-grouping-limit-and-offset)
    - [Ordering](#ordering)
    - [Grouping](#grouping)
    - [Limit &amp; Offset](#limit-and-offset)
- [Truncating Tables](#truncating-tables)
- [Plugins](#plugins)
- [Database Transactions](#database-transactions)
- [Defining Relationships (On Demand)](#defining-relationships)
    - [One To One](#one-to-one)
    - [One To Many](#one-to-many)
    - [Custom Relations](#custom-relations)

Installation
------------

[](#installation)

```
composer require mehedimi/wp-query-builder
```

If you need extra feature of `WP Query Builder` then you may install `WP Query Builder Extension` Package

```
composer require mehedimi/wp-query-builder-ext
```

Then require the autoload file of composer into your theme or plugin file.

Running SQL Queries
-------------------

[](#running-sql-queries)

The `DB` class provides methods for each type of query: `select`, `insert`, `update`, `delete`, `statement` and `affectingStatement`.

#### Running A Select Query

[](#running-a-select-query)

To run a basic SELECT query, you may use the `select` method on the `DB` class:

```
$users = DB::select('select * from users where active = ?', [1]);
```

#### Running An Insert Statement

[](#running-an-insert-statement)

To execute an `insert` statement, you may use the `insert` method on the `DB` class. Like `select`, this method accepts the SQL query as its first argument and bindings as its second argument:

```
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Username']);
```

If you need to get the last inserted ID, you may use the `insertGetId` method:

```
$id = DB::table('users')->insertGetId(['name' => 'Username']);
```

#### Running An Update Statement

[](#running-an-update-statement)

```
$affected = DB::update(
        'update users set votes = 100 where name = ?',
        ['Omar']
    );
```

#### Running A Delete Statement

[](#running-a-delete-statement)

```
 $deleted = DB::delete('delete from users');
```

Running Database Queries
------------------------

[](#running-database-queries)

#### Retrieving All Rows From A Table

[](#retrieving-all-rows-from-a-table)

You may use the `table` method provided by the `DB` class to begin a query. The `table` method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally retrieve the results of the query using the `get` method:

```
DB::table('posts')->get();
```

#### Retrieving A Single Row

[](#retrieving-a-single-row)

If you just need to retrieve a single row from a database table, you may use the `first` method of `DB` class. This method will return a single `stdClass` object:

```
$user = DB::table('users')->where('name', 'John')->first();

return $user->email;
```

### Aggregates

[](#aggregates)

The query builder also provides a variety of methods for retrieving aggregate values like `count`, `max`, `min`, `avg`, and `sum`. You may call any of these methods after constructing your query:

```
$users = DB::table('users')->count();

$price = DB::table('orders')->max('price');
```

Of course, you may combine these methods with other clauses to fine-tune how your aggregate value is calculated:

```
$price = DB::table('orders')
                ->where('finalized', 1)
                ->avg('price');
```

#### Specifying A Select Clause

[](#specifying-a-select-clause)

You may not always want to select all columns from a database table. Using the `select` method, you can specify a custom "select" clause for the query:

```
$users = DB::table('users')
            ->select('name', 'email')
            ->get();
```

The `distinct` method allows you to force the query to return distinct results:

```
$users = DB::table('users')->distinct()->get();
```

Joins
-----

[](#joins)

#### Inner Join Clause

[](#inner-join-clause)

The query builder may also be used to add join clauses to your queries. To perform a basic "inner join", you may use the `join` method on a query builder instance. The first argument passed to the `join` method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. You may even join multiple tables in a single query:

```
