PHPackages                             sultann/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. sultann/wp-query-builder

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

sultann/wp-query-builder
========================

WP Query Builder is a lightweight and efficient SQL Query Builder based on wpdb for WordPress. It supports complicated query generation.

v1.0.2(5y ago)704.7k14[2 issues](https://github.com/sultann/wp-query-builder/issues)[1 PRs](https://github.com/sultann/wp-query-builder/pulls)MITPHPPHP &gt;=5.4

Since May 3Pushed 2y agoCompare

[ Source](https://github.com/sultann/wp-query-builder)[ Packagist](https://packagist.org/packages/sultann/wp-query-builder)[ RSS](/packages/sultann-wp-query-builder/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (4)Used By (0)

[ ![](https://camo.githubusercontent.com/75987b0cde5d1651947253eaef5534aeafb38a742afd366773363eebefb1328f/68747470733a2f2f706c7567696e657665722e636f6d2f77702d636f6e74656e742f7468656d65732f706c7567696e657665722f696d616765732f706c7567696e657665722d6c6f676f2e737667)](http://pluginever.com)

WP Query Builder
================

[](#wp-query-builder)

WP Query Builder is a lightweight and efficient SQL Query Builder based on wpdb for WordPress. It supports complicated query generation.

[![Build Status](https://camo.githubusercontent.com/4abb248c352a7a8d7aacb37036286b67de139194f71c2e813ddb3b82f03d36da/68747470733a2f2f7472617669732d63692e6f72672f73756c74616e6e2f77702d71756572792d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/sultann/wp-query-builder)[![Packagist](https://camo.githubusercontent.com/a9716c25f864c16f2fad3f4b57523ff536fdaacf1c950192115dedd68ff27fb3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73756c74616e6e2f77702d71756572792d6275696c6465722e737667)](https://packagist.org/packages/sultann/wp-query-builder)[![Packagist](https://camo.githubusercontent.com/0667a171b91f1f82521a1a9aaa1942d24e25fb72dc2fc62433a4406b3c07d31c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73756c74616e6e2f77702d71756572792d6275696c6465722e737667)](https://github.com/sultann/wp-query-builder/blob/master/LICENSE)[![GitHub release](https://camo.githubusercontent.com/27a07918b1f289382819c7f092abf43f865464e963e76816a7434d6fd008fd61/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73756c74616e6e2f77702d71756572792d6275696c6465722e737667)](https://github.com/sultann/wp-query-builder/releases)

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

[](#installation)

WP Query Builder follows `PSR-4` autoloading and can be installed using composer:

```
$ composer require sultann/wp-query-builder

```

Documentation 💡
---------------

[](#documentation-)

### Initialize

[](#initialize)

Initialize query builder. init method takes a string argument using that later you can do action/filter based on your requirement. without argument.

```
$query = \PluginEver\QueryBuilder\Query::init();
```

with argument

```
$query = \PluginEver\QueryBuilder\Query::init('query_users');
```

### Select

[](#select)

This will build the query, execute and returns all users from users table with applying table prefix automatically. by default it select all(\*) but you can define what to select from the query; If you are selecting all then you can omit the select statement.

```
$results = $query->select('*')
                 ->from('users')
                 ->get();
```

Select specific column

```
$results = $query->select('ID')
                 ->from('users')
                 ->get();
```

Select multiple column

```
$results = $query->select('user_login, user_email')
                 ->from('users')
                 ->get();
```

### Where conditions

[](#where-conditions)

For the next few examples, lets assume a larger dataset so that the queries make sense.

```
$results = $query->select('*')
                 ->from('users')
                 ->where('user_email', 'like', '%gmail.com')
                 ->get();
```

Notice how omitting the operator in the first condition -&gt;where('user\_url', '') makes this default to =. By default all where conditions are defined with the and operator.

Different where operators:

```
$results = $query->select('*')
                 ->from('users')
                 ->where('user_email', 'like', '%gmail.com')
                 ->orWhere('user_email', 'like', '%yahoo.com')
                 ->get();
```

There are few more builtin Where conditions available

- `andWhere()`
- `whereIn()`
- `whereNotIn()`
- `whereNull()`
- `whereNotNull()`
- `orWhereNull()`
- `orWhereNotNull()`
- `whereBetween()`
- `whereNotBetween()`
- `whereDateBetween()`
- `whereRaw()`

#### Where scopes

[](#where-scopes)

Allow you to group conditions:

```
$results = $query->select('*')
                 ->from('posts')
                ->where('post_status', 'publish')
                ->where(function($q)
                {
                    $q->where('menu_order', '>', 21);
                    $q->where('menu_order', '1')
                ->get();
```

### Ordering

[](#ordering)

Ordering data:

```
$results = $query->select('*')
                 ->from('posts')
                ->order_by('post_title', 'DESC')
                ->get();
```

### Limiting data

[](#limiting-data)

Limit and offset:

```
$results = $query->select('*')
                 ->from('posts')
                ->limit(20, 10)
                ->get();
```

Only limit

```
$results = $query->select('*')
                 ->from('posts')
                ->limit(20)
                ->get();
```

Offset as separate

```
$results = $query->select('*')
                 ->from('posts')
                ->limit(20)
                ->offset(10)
                ->get();
```

### Pagination

[](#pagination)

shortcut of limit and offset

```
$results = $query->select('*')
                 ->from('posts')
                ->page(1, 20)//page number & page size
                ->get();
```

### Find

[](#find)

find item with column value

```
$results = $query->select('*')
                 ->from('posts')
                 ->find(1, 'ID');
```

### First

[](#first)

Get first item from the posts table

```
$results = $query->select('*')
                 ->from('posts')
                 ->first();
```

### Last

[](#last)

Get last item from the posts table

```
$results = $query->select('*')
                 ->from('posts')
                 ->last();
```

### Counting

[](#counting)

count total rows

```
$results = $query->select('*')
                 ->from('posts')
                 ->count();
```

### toSql

[](#tosql)

Out the query instead of executing

```
$results = $query->from('posts as p')
                ->join('users as u',  'p.post_author', 'u.ID')
                ->join('usermeta um', function($q) {
                      $q->where('um.meta_key', 'first_name');
                      $q->where('um.met_value', 'like', '%sultan%');
                  })
                ->toSql();
```

### Update

[](#update)

Update a row

```
$results = $query->table('posts')
                 ->where('ID', 20)
                 ->update(['post_title' => 'updated']);
```

### Delete

[](#delete)

Delete a row

```
$results = $query->from('posts')
                ->where('ID', 20)
                ->delete();
```

### Search

[](#search)

Search a value from columns

```
$results = $query->from('posts')
                 ->search('Hello Word', array('post_title', 'post_content')) // it will search Hello & World both
                 ->delete();
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/sultann/wp-query-builder/blob/master/LICENSE) for more information.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Total

2

Last Release

2164d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/888ecb9bec980eb4758575606e394842d4552fe72897e5183af8bb656035fba7?d=identicon)[sultann](/maintainers/sultann)

---

Top Contributors

[![sultann](https://avatars.githubusercontent.com/u/5239470?v=4)](https://github.com/sultann "sultann (16 commits)")[![dfwood](https://avatars.githubusercontent.com/u/400872?v=4)](https://github.com/dfwood "dfwood (2 commits)")[![saimonh3](https://avatars.githubusercontent.com/u/13016829?v=4)](https://github.com/saimonh3 "saimonh3 (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

wordpressdatabasequery builder

### Embed Badge

![Health badge](/badges/sultann-wp-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/sultann-wp-query-builder/health.svg)](https://phpackages.com/packages/sultann-wp-query-builder)
```

###  Alternatives

[10quality/wp-query-builder

Wordpress Query Builder class library for custom models and data querying.

4012.2k](/packages/10quality-wp-query-builder)[opis/database

A database abstraction layer over PDO, that provides a powerful and intuitive query builder, bundled with an easy to use schema builder

10184.2k3](/packages/opis-database)[codesvault/howdy-qb

Mysql Query Builder for WordPress

371.2k1](/packages/codesvault-howdy-qb)[williarin/wordpress-interop

Interoperability library to work with WordPress database in third party apps

6610.9k2](/packages/williarin-wordpress-interop)[cubettech/lacassa

Cassandra based query builder for laravel.

358.5k](/packages/cubettech-lacassa)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
