Home
Softono
b

birgire

Professional software vendor delivering innovative solutions on the Softono platform. Specialized in both open-source and proprietary software development.

Total Products
2

Software by birgire

wp-combine-queries
Open Source

wp-combine-queries

Combine Query ================= WordPress plugin - Combine Query [![Build Status](https://travis-ci.org/birgire/wp-combine-queries.svg?branch=master)](https://travis-ci.org/birgire/wp-combine-queries) [![GitHub license](https://img.shields.io/github/license/birgire/wp-combine-queries.svg)](https://github.com/birgire/wp-combine-queries/blob/master/LICENCE) [![Packagist](https://img.shields.io/packagist/v/birgir/combined-query.svg)](https://packagist.org/packages/birgir/combined-query) ### 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 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](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: 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><?php while( $q->have_posts() ): $q->the_post(); ?><li><a href="<?php the_permalink();?>"><?php the_title();?></a></li><?php endwhile; ?></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) - Fixed: Wrong plugin URI in header (Props: therealgilles) 1.2.2 (2021-02-20) - Fixed: Fixes #14 regarding subsequent queries. (Props: @Suranex) 1.2.1 (2020-09-14) - Fixed: Readme. 1.2.0 (2020-09-14) - Added: Support for ordering by 'none'. - Added: Test cases. - Added: Support for adding parameters (posts_per_page, offset, orderby, order) inside combined_query args. - Fixed: Hooks handling for pre_get_posts example. - Adjusted: Examples. 1.1.1 (2020-09-04) - Added: Example how to keep the order by arguments arg1, arg2, ... - Adjusted: UNION ALL test 1.1.0 (2020-09-04) - Added: Ticket #19 - Add test cases for argument order workaround. (Props: therealgilles) - Cleanup: phpcs 1.0.5 (2016-05-08) - Fixed: Ticket #8 - Fallback for those who don't use Composer. - Improved: Removed an explicit call to $GLOBALS['wpdb'] through the use keyword. - Improved: Simplified the namespace to only CombinedQuery. 1.0.4 (2016-04-21) - Fixed: Adjusted the paged bug that sneaked in with verion 1.0.2 yesterday. - Improved: Simplified the example that uses get_query_var() that can now handle default as an input parameter. 1.0.3 (2016-04-21) - Fixed: Ticket #7 - Not able to set the "UNION ALL" union option - Improved: Inline docs 1.0.2 (2016-04-20) - Fixed: Ticket #6 - Escape % in the Generator class. (Props: @DArcMattr) - Improved: Inline docs 1.0.1 (2015-11-09) - Fixed: Remove vendor dependency and let the user install it via 'composer install' (Props: @pdufour) - Fixed: Ignore sticky posts in the EmptyQuery class 1.0.0 (2015-05-10) - ** Total Plugin Rewrite ** - Closed: Ticket #3 - Added: New classes Main, EmptyQuery and Generator. - Added: Support for 'combined_query' attribute of the WP_Query class. - Added: Support only for PHP 5.4+ - Added: Autoload via Composer. - Added: New filter 'cq_sub_fields' instead of 'cq_sub_fields' - Added: New filter 'cq_orderby' instead of 'cq_orderby' 0.1.3 (2015-05-09) - Added: Support for ignory_sticky_posts. - Fixed: Minor 0.1.2 (2015-05-08) - Added: Support for the GitHub Updater. - Added: New filter 'wcq_sub_fields' - Added: New filter 'wcq_orderby' - Added: New example for meta value ordering - Fixed: Ordering didn't work correctly. 0.1.1 - Changed: Coding style and autoloading (Props: @egill) 0.1 Various plugin improvements, for example: - Added: orderby in the combined query. - Added: posts_per_page in the sub queries. - Added: offset in the sub queries. - Added: paged in the sub queries. - Removed: sublimit in the combined query, use posts_per_page instead in sub queries. - Fixed: Issue #1 related to max_num_pages (Props: @hellofantastic). 0.0.4 - Added: support for offset in the combined query 0.0.3 - Added: GPL2+ License part - Removed: Dropped namespace + anonymous function for wider PHP support. 0.0.2 - Added: Input parameter 'union' with possible values UNION and UNION ALL. - Fixed: Empty paged resulted in a sql error. (Props: Robert Hue)

Database WordPress Themes & Plugins
70 Github Stars
geo-query
Open Source

geo-query

WordPress plugin: Geo Query ================= [![Build Status](https://travis-ci.org/birgire/geo-query.svg?branch=master)](https://travis-ci.org/birgire/geo-query) [![GitHub license](https://img.shields.io/github/license/birgire/geo-query.svg)](https://github.com/birgire/geo-query/blob/master/LICENCE) [![Packagist](https://img.shields.io/packagist/v/birgir/geo-query.svg)](https://packagist.org/packages/birgir/geo-query) ### Description This plugin adds a support for the `geo_query` part of the `WP_Query` and `WP_User_Query`. Supports geo data stored in post/user meta or in a custom table. It uses the Haversine SQL implementation by Ollie Jones (see [here](http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/)). The plugin works on PHP 5.3+. It supports the GitHub Updater. Activate the plugin and you can use the `geo_query` parameter in all your `WP_Query` and `WP_User_Query` queries. Few examples are here below, e.g. for the Rest API. ### 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 example below, in your theme or in a plugin. Have fun ;-) ### Example - Basic `WP_Query` usage: Here's an example of the default input parameters of the `geo_query` part: $args = [ 'post_type' => 'post', 'posts_per_page' => 10, 'ignore_sticky_posts' => true, 'orderby' => [ 'title' => 'DESC' ], 'geo_query' => [ 'lat' => 64, // Latitude point 'lng' => -22, // Longitude point 'lat_meta_key' => 'geo_lat', // Meta-key for the latitude data 'lng_meta_key' => 'geo_lng', // Meta-key for the longitude data 'radius' => 150, // Find locations within a given radius (km) 'order' => 'DESC', // Order by distance 'distance_unit' => 111.045, // Default distance unit (km per degree). Use 69.0 for statute miles per degree. 'context' => '\\Birgir\\Geo\\GeoQueryHaversine', // Default implementation, you can use your own here instead. ], ]; $query = new WP_Query( $args ); ### Example - Rest API usage: Here's a modified example from @florianweich: add_filter( 'rest_query_vars', function ( $valid_vars ) { return array_merge( $valid_vars, [ 'geo_location' ] ); } ); add_filter( 'rest_post_query', function( $args, $request ) { $geo = json_decode( $request->get_param( 'geo_location' ) ); if ( isset( $geo->lat, $geo->lng ) ) { $args['geo_query'] = [ 'lat' => (float) $geo->lat, 'lng' => (float) $geo->lng, 'lat_meta_key' => 'geo_lat', 'lng_meta_key' => 'geo_lng', 'radius' => ($geo->radius) ? (float) $geo->radius : 50, ]; } return $args; }, 10, 2 ); Test it with e.g.: https://example.com/wp-json/wp/v2/posts?geo_location={"lat":"64.128288","lng":"-21.827774","radius":"50"} One can use `rest_{custom-post-type-slug}_query` filter for a custom post type. ### Example - Basic `WP_User_Query` usage: Here's an example from @acobster: $args = [ 'role' => 'subscriber', 'geo_query' => [ 'lat' => 47.236, 'lng' => -122.435, 'lat_meta_key' => 'geo_lat', 'lng_meta_key' => 'geo_lng', 'radius' => 1, 'context' => '\\Birgir\\Geo\\GeoQueryUserHaversine', ], ]; $query = new WP_User_Query( $args ); ### Example - Basic `WP_Query` usage for fetching lat/lng data from a custom table with Haversine formula: $args = array( 'post_type' => 'post', 'posts_per_page' => 10, 'ignore_sticky_posts' => true, 'orderby' => array( 'title' => 'DESC' ), 'geo_query' => array( 'table' => 'custom_table', // Table name for the geo custom table. 'pid_col' => 'pid', // Column name for the post ID data 'lat_col' => 'lat', // Column name for the latitude data 'lng_col' => 'lng', // Column name for the longitude data 'lat' => 64.0, // Latitude point 'lng' => -22.0, // Longitude point 'radius' => 1, // Find locations within a given radius (km) 'order' => 'DESC', // Order by distance 'distance_unit' => 111.045, // Default distance unit (km per degree). Use 69.0 for statute miles per degree. 'context' => '\\Birgir\\Geo\\GeoQueryPostCustomTableHaversine', // Custom table implementation, you can use your own here instead. ), ); $query = new WP_Query( $args ); Check the unit test method `test_custom_table_in_wp_query()` as a more detailed example. ### Notes on the parameters: - The plugin assumes we store the latitudes and longitudes as custom fields ( post meta), so we need to tell the query about meta keys with the `'lat_meta_key'` and `'lng_meta_key'` parameters. - Skipping the `'radius'` parameter means that no distance filtering will take place. - If we use the `'order'` parameter within the `'geo_query'`, then it will be prepended to the native `'orderby'` parameter. - The `'distance_unit'` parameter should be `69.0` for distance in statute miles, else `111.045` for distance in kilometers. - If we want to use the optimized Haversine version by Ollie Jones, we use: 'context' => '\\Birgir\\Geo\\GeoQueryHaversineOptimized' Notice that on our current plugin setup (i.e. fetching data from the LONGTEXT post meta fields) this isn't more performant than the default `GeoQueryHaversine` class. A future work could be to use a custom table with indexes, for the optimization to work. - If we create our own implementation of the Haversine formula, for example the `GeoQueryCustom` class, we just have to make sure it implements the `GeoQueryInterface` interface: 'context' => 'GeoQueryCustom' ### Feedback Any suggestions are welcomed. ### Changelog 0.2.3 (2024-10-29) - Fix: Deprecated creation of dynamic property. Props @lukasbesch 0.2.2 (2024-01-05) - Bump composer/installers to ^2.0.0. Props @wujekbogdan 0.2.1 (2023-07-26) - Fix deprecated notice in PHP 8.1 #24 0.2.0 (2020-04-25) - Support for fetching points from a custom table and doing Haversine formula in WP_Query. 0.1.1 (2019-01-23) - Fixed #14. Fixed the user query ordering. Props @baden03 0.1.0 (2018-08-06) - Added support for user queries. Props @acobster - Fixed Travis issue. Travis runs successfully for PHP 7.2, 7.1,7,5.6 when installed via Composer and also 5.4 when installed without Composer. Skip the 5.4 Composer check for now. 0.0.7 (2018-06-27) - Fixed #10. Use ^1.0.0 for composer installer. Props @wujekbogdan 0.0.6 (2017-11-16) - Fixed #6. Support floating point radius. Props @wujekbogdan - Added integration tests. 0.0.5 (2017-02-26) - Added fallback for those that don't use Composer - Removed the vendor directory 0.0.4 (2017-02-26) - Fixed #4. Props @billzhong . 0.0.3 (2015-04-29) - Fixed #2. Fixed a typo. Props @Ben764 and @con322. 0.0.2 (2015-03-10) - Added: Support for the GitHub Updater. - Updated: README.md - Changed: Use distance_value instead of distance_in_km in SQL, since we can use miles by changing the distance_unit parameter. 0.0.1 - Init

Maps & Location WordPress Themes & Plugins
68 Github Stars