Download the PHP package birgir/combined-query without Composer

On this page you can find all versions of the php package birgir/combined-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package combined-query

Combine Query

WordPress plugin - Combine Query

Build Status GitHub license Packagist

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 and here.

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 by Mike Schinkel.

I use the trick mentioned here to preserve the order of UNION sub queries.

This implementation supports combining N sub-queries.

Notice about the new 1.0.0 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

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

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

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 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:

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() ):
    ?><ul><li><a href=""></a></li></ul><?php
    wp_reset_postdata();
else:
    _e( 'Sorry no posts found!' );
endif;       

Example 2:

Here we want to display all foo posts with a date query, sorted by comment count and after that all bar posts sorted by comment count. Then we sort all by decreasing comment count.

//-----------------
// Sub query #1:
//-----------------
$args1 = [ 
    'post_type'           => 'foo',
    'orderby'             => 'comment_count',
    'order'               => 'desc',
    'posts_per_page'      => 100, // adjust to your needs
    'date_query'          => [
        [
            'after' => date('Y-m-d'),
        ],
        'inclusive'  => true,
    ]
];

//-----------------   
// Sub query #2:
//-----------------
$args2 = [
    'post_type'           => 'bar',
    'orderby'             => 'comment_count',
    'order'               => 'desc',
    'posts_per_page'      => 100, // adjust to your needs
];

//--------------------------- 
// Combined queries #1 + #2:
//---------------------------
$args = [
    'combined_query' => [        
        'args'                => [ $args1, $args2 ],
        'posts_per_page'      => 5,
        'paged'               => 2,
        'orderby'             => 'comment_count',
        'order'               => 'desc',
    ]
];

//---------
// Output:
//---------
// See example 1

Example 3:

Let's combine two meta queries and order by a common meta value:

//-----------------
// Sub query #1:
//-----------------
$args1 = [
   'post_type'      => 'cars',
   'posts_per_page' => 10,
   'orderby'        => 'title',
   'order'          => 'asc',
   'meta_query'     => [
        [
            'key'      => 'doors',
            'value'    => 0,
            'compare'  => '>=',
            'type'     => 'UNSIGNED'
        ],
    ],
];

//-----------------
// Sub query #2:
//-----------------
$args2 = [
   'post_type'      => 'post',
   'posts_per_page' => 10,
   'orderby'        => 'date',
   'order'          => 'desc',
   'tax_query' => [
        [
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => 'cars',
        ],
    ],
    'meta_query'     => [
        [
            'key'      => 'doors',
            'value'    => 0,
            'compare'  => '>=',
            'type'     => 'UNSIGNED'
        ],
    ],  
];

//------------------------------
// Order by a common meta value
//------------------------------

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

//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = [
    'combined_query' => [        
    'args'           => [ $args1, $args2 ],
    'posts_per_page' => 5,
    'orderby'        => 'meta_value_num',
    'order'          => 'DESC',
    ]
];

//---------
// Output:
//---------
// See example 1

remove_filter( 'cq_sub_fields', $callback );

Example 4:

We could also combine more than two sub queries, here's an example of four sub-queries:

 $args = [ 
    'combined_query' => [        
    'args' => [ $args1, $args2, $args3, $args4 ],
    ...
    ]
  ];

//---------
// Output:
//---------
// See example 1

Example 5:

The above examples are all for secondary queries. So let's add a query to the main home query:

add_action( 'pre_get_posts', function( \WP_Query $q ) {   

if( $q->is_home() && $q->is_main_query() ) {

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

    //-----------------
    // Original query #2:
    //-----------------    
    $args2 = $q->query;

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

    //-----------------------
    // Modify the Main query:
    //-----------------------
    $q->set( 'combined_query', $args['combined_query'] );
    }
} );

Changelog

(2022-03-03)

1.2.2 (2021-02-20)

1.2.1 (2020-09-14)

1.2.0 (2020-09-14)

1.1.1 (2020-09-04)

1.1.0 (2020-09-04)

1.0.5 (2016-05-08)

1.0.4 (2016-04-21)

1.0.3 (2016-04-21)

1.0.2 (2016-04-20)

1.0.1 (2015-11-09)

1.0.0 (2015-05-10)

0.1.3 (2015-05-09)

0.1.2 (2015-05-08)

0.1.1

0.1 Various plugin improvements, for example:

0.0.4

0.0.3

0.0.2


All versions of combined-query with dependencies

PHP Build Version
Package Version
Requires composer/installers Version ^1.0.0
php Version >=5.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package birgir/combined-query contains the following files

Loading the files please wait ....