Coder Social home page Coder Social logo

birgire / geo-query Goto Github PK

View Code? Open in Web Editor NEW
67.0 6.0 11.0 98 KB

WordPress plugin: Geo Query - Modify the WP_Query/WP_User_Query to support the geo_query parameter. Uses the Haversine SQL implementation by Ollie Jones. With a Rest API example and support for an existing custom table .

License: MIT License

PHP 89.29% Shell 10.71%
wp-query wordpress distance geo-query wordpress-plugin haversine-formula

geo-query's Issues

pre_get_posts not working

function override_search_query( $query ) {
	if ( ! is_admin() && $query->is_search() ) {
			$query->set( 'geo_query', array(
				'lat'                =>  $_GET['latitude'],       
				'lng'                =>  $_GET['longitude'],             
				'lat_meta_key'       =>  'hb_opts_lat_coordinate',                  
				'lng_meta_key'       =>  'hb_opts_long_coordinate',                       
				'order'              =>  'DESC',                           
				'distance_unit'      =>  111.045,                          
				'context'            => '\\Birgir\\Geo\\GeoQueryHaversine', 
			) );
		}
}
add_action( 'pre_get_posts', 'override_search_query' ); 


What is wrong ?

Setting up but posts displaying as normal or white screen

I am trying to install/setup a query using this plugin :)

So far I have downloaded & activated it and setup my is below

However when using the following nothing really happens all posts display as normal (not the ones within the radius)

$args = array(
'post_type'          => 'lostbox_items',    
'posts_per_page'     => -1,
'orderby'            => array( 'title' => 'DESC' ),
'geo_query' => array(
    'lat'                =>  51.5286416,                                // Latitude point
    'lng'                =>  -0.1015987,                               // Longitude point
    'lat_meta_key'       =>  '_lbx_lat',                          // Meta-key for the latitude data
    'lng_meta_key'       =>  '_lbx_lng',                          // Meta-key for the longitude data 
    'radius'             =>  1,                               // Find locations within a given radius (km)
    'order'              =>  'DESC',                            // Order by distance
    'distance_unit'      =>  111.045,                           // Default distance unit (km per degree)
    'context'            => 'GeoQueryCustom', // Default implementation, you can use your own here      instead.
    ),
);
$query = new WP_Query( $args );

If I use the below I get a white screen below my header

'context'            => '\\Birgir\\Geo\\GeoQueryHaversine'

Debugging it I get this error when using the above line

Catchable fatal error: Object of class wpdb could not be converted to string in /Applications/MAMP/htdocs/website/wp-content/plugins/geo-query-master/src/GeoQueryHaversine.php on line 37

Installation within a theme

Hi just wondering if this can be installed within a theme is it as simple as just using require once in function with the path to geo-query.php?

Also when do a search is it as simple as?

    'lat'                =>  $POST['current_location_lat'],                                // Latitude point
    'lng'                =>  $POST['current_location_lng'],                               // Longitude point
    'lat_meta_key'       =>  'my_lat',                          // Meta-key for the latitude data
    'lng_meta_key'       =>  'my_lng',                          // Meta-key for the longitude data 
    'radius'             =>  $POST['radius'], 

geo-query is not compatible with composer/installers newer than ~1.0.0

I install WordPress with Composer (thank's to the awesome Bedrock tool). Bedrock already uses composer/installers v ^1.4. geo-query depends on composer/installers too, it requires version ~1.0.0 that causes a conflict.

When trying to add geo-query to my project Composer complains that:

Problem 1
    - Installation request for birgir/geo-query ^0.0.6 -> satisfiable by birgir/geo-query[0.0.6].
    - birgir/geo-query 0.0.6 requires composer/installers ~1.0.0 -> satisfiable by composer/installers[1.0.x-dev, v1.0.0, v1.0.1, v1.0.10, v1.0.11, v1.0.12, v1.0.13, v1.0.14, v1.0.15, v1.0.16, v1.0.17, v1.0.18, v1.0.19, v1.0.2, v1.0.20, v1.0.21, v1.0.22, v1.0.23, v1.0.24, v1.0.25, v1.0.3, v1.0.4, v1.0.5, v1.0.6, v1.0.7, v1.0.8, v1.0.9] but these conflict with your requirements or minimum-stability.

Does it really mean that geo-query requires composer/installers that's not newer than ~1.0.0? Or is it just an oversight in the composer.json file? I suppose that it's safe to upgrade that dependency.

Retrieve distance information for an item

Hi, is there any way to retrieve information about the distance between two coordinates, eg "3.4 km" between Coordinate A & Coordinate B? It would be nice to display this to a user who is seeing results ordered by distance.

So far looking at the code, that value might be saved in distance_value but I think a function of some kind is required to extract it for display in a loop of items.

Using in conjunction with multiple meta queries in REST url

Is it possible to use this query in conjunction with other meta queries with a relation of OR?

For example, I need to pull in a bunch of listings based on their lat and long values, but also need to include all listings with a listing_type of 'National' and 'State-wide'.

I currently query the National and State-wide listings via this filter in the url:

'?filter[meta_query][relation]=OR&filter[meta_query][0][key]=state_wide_state&filter[meta_query][0][value]=' + this.state + '&filter[meta_query][1][key]=cause_type&filter[meta_query][1][value]=National'

Is there a way I can use this geo-query as another meta_query or combine them in some way?

Many thanks

Reuben

0 results when radius < 1

When radius is between 0 and 1 then the query doesn't give any results. Is it a bug, or is it a desired behavior?

Add support for pre_get_posts

Sample code:

add_action( 'pre_get_posts', 'override_main_query' );

function override_main_query( $query ) {
    if ( $query->is_search && $query->is_main_query() ) {
        $query->set('geo_query', array(
	        'lat'                =>  get_lat_long('lat'),                               
	        'lng'                =>  get_lat_long('long'),                            
	        'lat_meta_key'       =>  'latitude',                         
	        'lng_meta_key'       =>  'longitude',                         
	        'radius'             =>  150,                               
	        'order'              =>  'ASC',                          
	        'distance_unit'      =>  111.045,                         
	    ));
    }
}

This currently does not work. Any help would be appreciated.

404 Error in get_pre_posts Callback

I am getting 404 error. How do i get this to work correctly? Am I doing something incorrect? See images attached for details. please Help!!

function filter_queryby_currentcity( $query ) {
if ( !is_admin() )
{
getGeoipDetails();
//print_r("filter:".trim(getGeoipDetails()["city"]));
// print_r("IP Based geo details".microtime(true).":".$GLOBALS['location_details']['geolat']);

$query->set( 'geo_query', [
  'lat'                =>  $GLOBALS['location_details']['geolat'],                                // Latitude point
    'lng'                =>  $GLOBALS['location_details']['geolong'],                               // Longitude point
   'lat_meta_key'       =>  '_tmbu_lat',                         // Meta-key for the latitude data
   'lng_meta_key'       =>  '_tmbu_lng',                         // Meta-key for the longitude data 
   'radius'             =>  50,                               // 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.

post_meta_tabe
print_rquery
]);

Deprecated notice in PHP 8.1

Deprecated: Creation of dynamic property Birgir\Geo\GeoQueryContext::$db is deprecated in /path/wp-content/plugins/geo-query/src/GeoQueryContext.php on line 26

Move the order parameter out of geo_query part of WP_Query

Instead of:

$args = [
    'geo_query' => array(
        'order' => 'DESC', // Order by distance
    ],
];
$query = new WP_Query( $args );

we should use the ordering support of WP_Query, e.g.

$args = [
    'orderby'      => array( 'distance' => 'ASC', 'title' => 'DESC' ),
    'geo_query' => [... ]
];
$query = new WP_Query( $args );


WordPress database error: [Unknown column 'settings.distance_unit' in 'field list']

Hey, Awesome plugin.
I've used your geo query for custom post type and it works wonderfully good.
But when I'm trying to use it wp_user_query it thows the following error.
Please help.
Thank you.

Notice: Undefined index: join in /home/abc/public_html/wp-content/plugins/geo-query-master/src/GeoQueryHaversine.php on line 77

Notice: Undefined index: groupby in /home/abc/public_html/wp-content/plugins/geo-query-master/src/GeoQueryHaversine.php on line 80
WordPress database error: [Unknown column 'settings.distance_unit' in 'field list']
SELECT SQL_CALC_FOUND_ROWS wp_users.*, settings.distance_unit * DEGREES( ACOS( COS( RADIANS( settings.latpoint ) ) * COS( RADIANS( mtlat.meta_value ) ) * COS( RADIANS( settings.longpoint ) - RADIANS( mtlng.meta_value ) ) + SIN( RADIANS( settings.latpoint ) ) * SIN( RADIANS( mtlat.meta_value )))) AS distance_value FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id ) WHERE 1=1 AND ( ( ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '%\"customer\"%' ) ) ) ORDER BY user_login ASC LIMIT 0, 2

GeoQueryUserHaversine returns 0 results when order argument is used

Not sure if the document needs to be updated (see: #7) or if there is a bug, but using the order parameter returns zero results when using GeoQueryUserHaversine. Omitting the order parameter returns as expected.

'geo_query' => [
		'lat'       	       	=> $data['lat'],
		'lng'          	=> $data['lng'],
		'lat_meta_key'  => 'lat',
		'lng_meta_key' => 'lng',
		'radius'       	=> 10,
		'order'       	=> 'DESC',
		'context'       	=> '\\Birgir\\Geo\\GeoQueryUserHaversine',
	 ],

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.