PHPackages                             birgir/combined-query - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. birgir/combined-query

ActiveWordpress-plugin[Utility &amp; Helpers](/categories/utility)

birgir/combined-query
=====================

Modify the WP\_Query to support the combined\_query parameter. Combine multiple WP\_Query queries into a single query.

1.2.2(5y ago)711.3k10[1 issues](https://github.com/birgire/wp-combine-queries/issues)MITPHPPHP &gt;=5.4

Since May 10Pushed 4y ago4 watchersCompare

[ Source](https://github.com/birgire/wp-combine-queries)[ Packagist](https://packagist.org/packages/birgir/combined-query)[ RSS](/packages/birgir-combined-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (10)Used By (0)

Combine Query
=============

[](#combine-query)

WordPress plugin - Combine Query

[![Build Status](https://camo.githubusercontent.com/6fe6ea5d98c212f9607f0c8e6f3cd489942ef9f2e2f14f62b7ba87c8331974ad/68747470733a2f2f7472617669732d63692e6f72672f626972676972652f77702d636f6d62696e652d717565726965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/birgire/wp-combine-queries)[![GitHub license](https://camo.githubusercontent.com/a615a8283894dcb582244c6e2760d65f37219ac68e76452a356e76acb103ee9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f626972676972652f77702d636f6d62696e652d717565726965732e737667)](https://github.com/birgire/wp-combine-queries/blob/master/LICENCE)[![Packagist](https://camo.githubusercontent.com/3b3dba9bca7970a32c20163cc13267861d3ff06eb9f024b857739592da7f65c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6269726769722f636f6d62696e65642d71756572792e737667)](https://packagist.org/packages/birgir/combined-query)

### Description

[](#description)

This experimental plugin allows you to combine multiple `WP_Query` queries into a single one, using the `combined_query` attribute.

This started as an answer on Stackoverflow, see [here](http://stackoverflow.com/questions/23555109/wordpress-combine-queries/) and [here](http://wordpress.stackexchange.com/questions/159228/combining-two-wordpress-queries-with-pagination-is-not-working/).

The idea behind this plugin is to combine the SQL queries for each `WP_Query()` query with `UNION` or `UNION ALL`.

I first noticed this technique in a [great answer on WordPress Development](http://wordpress.stackexchange.com/a/912/26350) by Mike Schinkel.

I use the trick mentioned [here](http://stackoverflow.com/a/7587423/2078474) to preserve the order of `UNION` sub queries.

This implementation supports combining `N` sub-queries.

### Notice about the new 1.0.0 version

[](#notice-about-the-new-100-version)

This version is a total rewrite of the plugin.

The `WP_Combine_Query` class has been removed in favour of simply using the `combined_query` attribute of the `WP_Query` class.

Now the plugin only supports PHP versions 5.4+.

### Settings

[](#settings)

The supported settings for the `combined_query` attribute:

```
'combined_query' => [
	'args'           => [ $args1, $args2, ... ], // Array (default [])
	'union'          => 'UNION',                 // String Possible values are UNION or UNION ALL (default UNION)
	'posts_per_page' => 10,                      // Integer 1,2,...
	'offset'         => 0,                       // Integer 0,1,...
	'orderby'        => 'meta_value_num',        // String (post_name, ..., name, ..., none, meta_value, meta_value_num )
	'order'          => 'DESC',                  // String (ASC,DESC)
]

```

If you want to remove duplicated posts use `UNION`, else use `UNION ALL`.

### Custom filters

[](#custom-filters)

There are two custom filters currently available:

```
// Modify combined ordering:
add_filter( 'cq_orderby', function( $orderby ) {
    return $orderby;
});

// Modify sub fields:
add_filter( 'cq_sub_fields', function( $fields ) {
    return $fields;
});

```

To keep the order by arguments arg1, arg2, ... use:

```
'combined_query' => [
    ...
    'orderby' => 'none',
    ...
]

```

or

```
add_filter( 'cq_orderby', '__return_empty_string' );
$query = new WP_Query( $args );
remove_filter( 'cq_orderby', '__return_empty_string' );

```

### Installation

[](#installation)

Upload the plugin to the plugin folder and activate it.

To install dependencies with Composer (not required):

```
composer install

```

or

```
php composer.phar install

```

within our folder. See [here](https://getcomposer.org/doc/00-intro.md) for more information on how to install Composer.

Then play with the examples below, in your theme or in a plugin.

Have fun ;-)

### Example 1:

[](#example-1)

Here we want to display the first published page in an alphabetical order and then the three oldest published posts:

```
//-----------------
// Sub query #1:
//-----------------
$args1 = [
	'post_type'      => 'page',
	'posts_per_page' => 1,
	'orderby'        => 'title',
	'order'          => 'asc',
];

//-----------------
// Sub query #2:
//-----------------
$args2 = [
	'post_type'      => 'post',
	'posts_per_page' => 3,
	'orderby'        => 'date',
	'order'          => 'asc',
];

//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
	'combined_query' => [
		'args'           => [ $args1, $args2 ],
		'union'          => 'UNION',
		'posts_per_page' => 4,
		'orderby'        => 'none',
	]
];

//---------
// Output:
//---------
$q = new WP_Query( $args );
if( $q->have_posts() ):
	?>
