Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP REST API, orderby distance WordPress

Hi,

I am pretty new to extending wordpress queries, but I do have some knowledge of the ins and outs and feel that I can follow along.

I am building an angular application that uses wordpress as the backend.

In wordpress I am using the WP REST API (http://wp-api.org/) v2 and the Advance custom fields plugin (ACF).

I would like to be able to call the rest API and order the posts by distance from the user, using the ACF location as well as the users location.

I would imaging the call to look something like this:
http://domain.com/wp-json/wp/v2/posts/?meta_key=order_location&meta_value=45.7269585,13.7562836

I found a similar question that also uses the law of cosin to work out the distance
<blockquote>
cos(d/R)=cos(lat1)cos(lat2)cos(long2-long1)+sin(lat1)sin(lat2)
http://wpquestions.com/question/showChrono/id/7926
</blockquote>

I am just not confident on how to apply this to the result of a WP REST query or how to use the ACF results meta field data.

Answers (2)

2016-04-11

Reigel Gallarde answers:

are you using custom post type? or just the post?


Klutz comments:

Hi Reigel,

This is just for normal posts.
(But in the future I might want to extend this for custom-post types, would this be quiet different?)


Klutz comments:

Hi Reigel,

This is just for normal posts.
(But in the future I might want to extend this for custom-post types, would this be quiet different?)


Reigel Gallarde comments:

The reason I asked is because "wp-json/wp/v2/posts/" is intended for post post type... we can filter it, but it will not be a good idea because this will also affect requests that are not rest api related...

so here's how I think a better way...

we will add endpoint.. [[LINK href="http://v2.wp-api.org/extending/adding/"]]sample here[[/LINK]]

and here's what I have so far... [[LINK href="https://gist.github.com/reigelgallarde/8c8890994dcb99874a1bc4f83b7224f7"]]https://gist.github.com/reigelgallarde/8c8890994dcb99874a1bc4f83b7224f7[[/LINK]]

on function wp_json_namespace_v2__posts, we use our own WP_Query... this way, we can change post_type in the future...

another thing to note in on the function my_geo_join... you need to change the meta_key according to what you have... mine has 'lat' and 'lon' meta keys.. you may need to change this to your ACF field lat/long...

you can then browse it with the url like this, http://domain.com/namespace/v2/posts/?order_location=lat,lon
where lat and lon are valid coordinates...

you could also change "namespace/v2/posts/" to something you like... just look at function wp_json_namespace_v2__rest_api_init...


Reigel Gallarde comments:

typo

http://domain.com/namespace/v2/posts/?order_location=lat,lon

to

http://domain.com/wp-json/namespace/v2/posts/?order_location=lat,lon

example call
http://domain.com/wp-json/namespace/v2/posts?order_location=10.2661820,123.9972950


Reigel Gallarde comments:

I have new version... this version uses filter so we can use the current functionality of rest api..

the code: [[LINK href="https://gist.github.com/reigelgallarde/fa9b1c7bdea618ecbc8aff31e7a2ca8a"]]https://gist.github.com/reigelgallarde/fa9b1c7bdea618ecbc8aff31e7a2ca8a[[/LINK]]

sample link: http://domain.com/wp-json/wp/v2/posts?order_location=10.2661820,123.9972950

you still have to change meta keys in function my_geo_join..

if nothing goes wrong, this would add a field to the result that looks like this:

geolocation: {
latitude: "10.3402620",
longitude: "123.9415520",
distance: "6.368880746600806"
},


Reigel Gallarde comments:

have you got the chance to try my solutions?


Klutz comments:

Hi Reigel,

Wow thanks so much!
I havent had a chance to have a look at the solution just yet, but it makes sense.
I will try this first thing in the morning and let you know.

Thanks again for the help.


Reigel Gallarde comments:

Okay... just let me know if you need anything...


Reigel Gallarde comments:

any progress on this?

2016-04-11

Rempty answers:

Hello Klutz, do you have the distance as a meta_key or want calculate the distance and order with this new value?


Klutz comments:

Hi Rempy,

Much like the example, I have the users lat/long and the posts lat/long (through ACF field).
And using the Haversine function (cos(d/R)=cos(lat1)cos(lat2)cos(long2-long1)+sin(lat1)sin(lat2)), I want to order the posts by distance ASC.

The difference from the example is that I am trying to use this through WP REST API (http://v2.wp-api.org/).

I hope that makes sense?