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

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

simettric/wp-query-builder
==========================

A query builder for WordPress WP\_Query, inspired by Doctrine Query Builder

173182[1 PRs](https://github.com/Simettric/WPQueryBuilder/pulls)PHP

Since Jul 27Pushed 7y ago3 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

WPQueryBuilder
==============

[](#wpquerybuilder)

A query builder for WordPress WP\_Query, inspired by the Doctrine Query Builder

[![Build Status](https://camo.githubusercontent.com/cc2dd57bd074d57e1d9db03d9146088bd6d7b03b9643e4ec45614e32f8e27175/68747470733a2f2f7472617669732d63692e6f72672f53696d6574747269632f575051756572794275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Simettric/WPQueryBuilder)

[![SensioLabsInsight](https://camo.githubusercontent.com/d3d7202bd6e65ec83259515814c128b3618ce57a7a79af38f6568a7e12e26449/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f36333438303134322d653164642d343063382d616337632d3234646438323433343239372f6269672e706e67)](https://insight.sensiolabs.com/projects/63480142-e1dd-40c8-ac7c-24dd82434297)

INSTALLATION
============

[](#installation)

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

```

USAGE
=====

[](#usage)

### META QUERIES

[](#meta-queries)

Retrieve any post type where post meta color value equals to blue OR size meta value equals to XL

```
       $builder = new Builder();
       $wp_query = $builder->createMainMetaQuery("OR")
                            ->addMetaQuery(MetaQuery::create('color', 'blue'))
                            ->addMetaQuery(MetaQuery::create('size', 'XL'))
                            ->getWPQuery();

```

Retrieve any post type where post meta price is equal or greater than 10 OR size meta value equals to XL

```
       $builder = new Builder();
       $wp_query = $builder->createMainMetaQuery("AND")
                            ->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC'))
                            ->addMetaQuery(MetaQuery::create('size', 'XL'))
                            ->getWPQuery();

```

Retrieve any post type where post meta price is equal or greater than 10 AND (size meta value equals to XL OR post meta color value equals to blue)

```
       $builder = new Builder();
       $builder->createMainMetaQuery("AND")
               ->addMetaQuery(MetaQuery::create('price', 10, '>=', 'NUMERIC'));

       $condition = new MetaQueryCollection('OR');
       $condition->add(MetaQuery::create('color', 'blue'))
                 ->add(MetaQuery::create('size', 'XL'));

       $wp_query = $builder->addMetaQueryCollection($condition)
                           ->getWPQuery();

```

### TAXONOMY QUERIES

[](#taxonomy-queries)

Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy AND in under 'sweet' in "custom" taxonomy

```
       $builder = new Builder();
       $wp_query = $builder->createMainTaxonomyQuery("AND")
                            ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools')))
                            ->addTaxonomyQuery(TaxonomyQuery::create('custom', 'slug', array('sweet))
                            ->getWPQuery();

```

Retrieve the contents under ("pets" OR "tools") values in the "category" taxonomy BUT exclude contents in their children

```
       $builder = new Builder();
       $wp_query = $builder->createMainTaxonomyQuery("AND")
                            ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), false))
                            ->getWPQuery();

```

Retrieve the contents those are NOT under ("pets" OR "tools") values in the "category" taxonomy

```
       $builder = new Builder();
       $wp_query = $builder->createMainTaxonomyQuery("AND")
                            ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools'), true, 'NOT IN'))
                            ->getWPQuery();

```

You can have nested relations too

```
      $builder = new Builder();

      $collection = new TaxonomyQueryCollection('OR');
      $collection->add(TaxonomyQuery::create('tag', 'slug', array('cats')));
      $collection->add(TaxonomyQuery::create('custom', 'slug', array('sweet')));

      $wp_query = $builder->createMainTaxonomyQuery("AND")
                           ->addTaxonomyQuery(TaxonomyQuery::create('category', 'slug', array('pets', 'tools')))
                           ->addTaxonomyQueryCollection($collection)
                           ->getWPQuery();

```

### POST TYPES

[](#post-types)

Retrieve all PAGES

```
       $builder = new Builder();
       $wp_query = $builder->addPostType(Builder::POST_TYPE_PAGE)->getWPQuery();

```

Retrieve all CUSTOM POST TYPE

```
       $builder = new Builder();
       $wp_query = $builder->addPostType('your_custom')->getWPQuery();

```

Retrieve all CUSTOM POST TYPE and PAGES

```
       $builder = new Builder();
       $wp_query = $builder->addPostType('your_custom')
                           ->addPostType(Builder::POST_TYPE_PAGE)
                           ->getWPQuery();

```

### SEARCH

[](#search)

Search contents

```
        $builder = new Builder();

        $wp_query = $builder->search("search query")->getWPQuery();

```

### IN and NOT IN

[](#in-and-not-in)

Retrieve contents with ID in array of IDS

```
        $builder = new Builder();

        $wp_query = $builder->inPostIDs(array(1,2,3))->getWPQuery();

```

Retrieve contents with ID not in array of IDS

```
        $builder = new Builder();

        $wp_query = $builder->notInPostIDs(array(1,2,3))->getWPQuery();

```

### ORDERBY

[](#orderby)

Order contents by title descending

```
        $builder = new Builder();

        $wp_query = $builder->setOrderBy("title")->getWPQuery();

```

Order contents by date, ascending

```
        $builder = new Builder();

        $wp_query = $builder->setOrderBy("date")
                            ->setOrderDirection("ASC")
                            ->getWPQuery();

```

Order contents by title descending and date, ascending

```
        $builder = new Builder();

        $wp_query = $builder->addOrderBy("title", "DESC")
                            ->addOrderBy("date", "ASC")
                            ->getWPQuery();

```

Order contents by custom meta

```
        $builder = new Builder();

        $wp_query = $builder->setOrderByMeta("color", "DESC")->getWPQuery();

```

Order contents by custom numeric meta

```
        $builder = new Builder();

        $wp_query = $builder->setOrderByMeta("price", "ASC", true)
                            ->getWPQuery();

```

### LIMITS AND OFFSETS

[](#limits-and-offsets)

Retrieve only 10 contents

```
        $builder = new Builder();

        $wp_query = $builder->setLimit(10)->getWPQuery();

```

Retrieve 20 contents starting from the 10th position

```
        $builder = new Builder();

        $wp_query = $builder->setLimit(20)->setOffset(10)->getWPQuery();

```

### RETRIEVING

[](#retrieving)

Get the WPQuery object

```
        $builder = new Builder();

        $wp_query = $builder->getWPQuery();

```

Get the Posts array

```
        $builder = new Builder();

        $posts = $builder->getPosts();

```

Get the WPQuery parameters array

```
        $builder = new Builder();

        $params = $builder->getParameters();

        $query = new WP_Query($params);

```

Get an array containing only the post IDs. This is useful when you want to return all records without pagination from a large recordset in order to avoid memory issues.

```
        $builder = new Builder();

        $ids = $builder->getPostIDsOnly();

        $builder = new Builder();

        $wp_query = $builder->inPostIDs($ids)->getWPQuery();

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85% 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.

### Community

Maintainers

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

---

Top Contributors

[![asiermarques](https://avatars.githubusercontent.com/u/149459?v=4)](https://github.com/asiermarques "asiermarques (34 commits)")[![jeslopcru](https://avatars.githubusercontent.com/u/2398253?v=4)](https://github.com/jeslopcru "jeslopcru (5 commits)")[![iwillhappy1314](https://avatars.githubusercontent.com/u/1455683?v=4)](https://github.com/iwillhappy1314 "iwillhappy1314 (1 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

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