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

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

wp-forge/wp-query-builder
=========================

A lightweight and efficient SQL query builder for WordPress.

1.0.4(1y ago)3196.6k—4.8%1GPL-2.0-or-laterPHPPHP &gt;=5.4

Since May 3Pushed 1y ago1 watchersCompare

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

READMEChangelog (4)DependenciesVersions (5)Used By (0)

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

[](#wp-query-builder)

**A lightweight and efficient SQL query builder for WordPress.**

This is a fork of the original project () which appears to be unmaintained.

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

[](#installation)

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

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

```

Documentation
-------------

[](#documentation)

### Initialization

[](#initialization)

The init method takes a string argument. You can use that later to use actions/filters specific to your query builder instance.

Without argument.

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

With argument

```
$query = \WP_Forge\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 selects 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();
```

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.4% 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 ~524 days

Total

4

Last Release

634d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae89dc177002c2f67341cde2d525e3f3c5b1d6aaaf07df15257a284f001c5551?d=identicon)[wpscholar](/maintainers/wpscholar)

---

Top Contributors

[![sultann](https://avatars.githubusercontent.com/u/5239470?v=4)](https://github.com/sultann "sultann (15 commits)")[![dfwood](https://avatars.githubusercontent.com/u/400872?v=4)](https://github.com/dfwood "dfwood (2 commits)")[![wpscholar](https://avatars.githubusercontent.com/u/890951?v=4)](https://github.com/wpscholar "wpscholar (2 commits)")[![mamatharao05](https://avatars.githubusercontent.com/u/149479917?v=4)](https://github.com/mamatharao05 "mamatharao05 (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/wp-forge-wp-query-builder/health.svg)

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

###  Alternatives

[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.

704.7k](/packages/sultann-wp-query-builder)[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)
