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

Using in conjunction with multiple meta queries in REST url #17

Open
reuben-stone opened this issue May 24, 2019 · 3 comments
Open

Using in conjunction with multiple meta queries in REST url #17

reuben-stone opened this issue May 24, 2019 · 3 comments

Comments

@reuben-stone
Copy link

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

@birgire
Copy link
Owner

birgire commented May 24, 2019

Hi Reuben,

I guess the AND case would be covered with the Rest API usage example mentioned here:

https://github.com/birgire/geo-query

using the PHP snippet there and then:

https://example.com/wp-json/wp/v2/posts?geo_location={"lat":"64.128288","lng":"-21.827774","radius":"50"}&...other...

But I'm not sure at the moment what would be the best way to handle it with OR as the geo query is not part of the WP_Meta_Query class. Maybe that would be an interesting path to look at for the plugin?

I remember playing with:

https://wordpress.stackexchange.com/a/178492/26350

for meta query and a simple title search. and also a way to combine two WP_Query here:

https://github.com/birgire/wp-combine-queries

but these methods might not be useful here?

It looks you're not using the native WP REST API filters, if I recall correctly the filter parameter is no longer supported. I guess you're using a plugin for that, maybe that plugin has a support for such an adjustment with other queries?

@reuben-stone
Copy link
Author

reuben-stone commented May 25, 2019

Thanks for the reply.

I'll take a look at the links you sent over and see if I can possibly make a custom endpoint for this specific use case.

I was just wondering though, with the custom filter for the geo-query, would it at all be possible to add in a relational meta query into the filter itself. This may be a bit out there and not possible at all, but as all I need to do it modify the lat long returned response to also include posts with a meta query value, could something like this work?

`add_filter( 'rest_cause_query', function( $args, $request ) {
$geo = json_decode( $request->get_param( 'geo_location' ) );
if ( isset( $geo->lat, $geo->lng ) ) {

    $meta_query = array(
                    'key'     => 'cause_type',
                    'value'   => 'National',
                    'compare' => 'IN',
                );

    $args['geo_query'] = [
        'lat'                =>  (float) $geo->lat,
        'lng'                =>  (float) $geo->lng,
        'lat_meta_key'       =>  'latitude',
        'lng_meta_key'       =>  'longitude',
        'radius'             =>  ($geo->radius) ? (float) $geo->radius : 50,
        'order'              =>  'ASC', 
        'meta_query'         =>  $meta_query,
        'relation'           =>  'OR',
    ];
}
return $args;

}, 10, 2 );`

Or would this require configuration of the plugin itself?
Thanks again.

@reuben-stone
Copy link
Author

Actually clearly that would require re-writing the plugin.

It would be good if it were possible to have the geo-query as part of the WP_Meta_Query class. Then we could use this in conjunction with other queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants