Skip to content

wp-forge/wp-geo-query

Repository files navigation

WordPress Geo Query

WordPress Versions PHP Versions

Perform location based searches in WordPress.

Installation

Install Composer.

In your WordPress plugin or theme directory, run:

composer require wp-forge/wp-geo-query

Make sure you have this line of code in your project:

<?php

require __DIR__ . '/vendor/autoload.php';

Usage

When creating a custom query:

<?php

$query = new WP_Query(
	array(
		'geo_query' => array(
			'lat'      => 0,
			'lng'      => 0,
			'distance' => 25,               // Default value is 25
			'units'    => 'miles',          // Default is miles, could also be 'mi', 'km', or 'kilometers'
			'lat_key'  => 'geo_latitude',   // Default value is 'geo_latitude'
			'lng_key'  => 'geo_longitude',  // Default value is 'geo_longitude'
		),
		// ...
	)
);

When using pre_get_posts:

<?php

add_action(
	'pre_get_posts',
	function ( WP_Query $query ) {
		if ( $query->is_main_query() /* Customize your conditional to meet your needs */ ) {
			$query->set(
				'geo_query',
				array(
					'lat'      => 0,
					'lng'      => 0,
					'distance' => 25,               // Default value is 25
					'units'    => 'miles',          // Default value is 'miles', could also be 'mi', 'km', or 'kilometers'
					'lat_key'  => 'geo_latitude',   // Default value is 'geo_latitude'
					'lng_key'  => 'geo_longitude',  // Default value is 'geo_longitude'
				)
			);
		}
	}
);