Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get distance value for each post #5

Closed
ciarlystreet opened this issue Dec 4, 2017 · 2 comments
Closed

Get distance value for each post #5

ciarlystreet opened this issue Dec 4, 2017 · 2 comments

Comments

@ciarlystreet
Copy link

Hi, this plugin is amazing! It's possible know the distance value for each post obtained from geo query?

@wxactly
Copy link
Member

wxactly commented Dec 18, 2017

Hi @ciarlystreet! If you sort by distance, elasticsearch returns the actual distance value as the sort value on each result. Then you can use an elasticpress hook to pull that into your post object, something like:

add_filter( 'ep_retrieve_the_post', function ( $post, $hit ) {
	$post['geo_distance'] = $hit['sort'][0];
	return $post;
}, 10, 2 );

@wxactly
Copy link
Member

wxactly commented Jan 19, 2018

Wanted to leave a quick follow-up here... you actually need to implement the hook mentioned above, PLUS an additional hook to allow elasticpress to set this value on your post object:

add_filter( 'ep_search_post_return_args', function ( $post_return_args ) {
	$post_return_args[] = 'geo_distance';

	return $post_return_args;
} );

Then you will be able to access this property on your post object in the loop:

global $post;

echo $post->geo_distance;

@wxactly wxactly closed this as completed Jan 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants