wp-forge/wp-geo-query

Perform location based searches in WordPress.

1.0 2020-05-02 13:24 UTC

This package is auto-updated.

Last update: 2024-10-29 05:47:20 UTC


README

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'
				)
			);
		}
	}
);