Reputation: 31
I want to query a Custom post type Person, filter those people by their first name and the name of the City they are living in.
But I don't know the ID of the City but only it's name.
The City is in my case another custom post type, linked to the Person with an ACF relationship field.
I'm using the Wordpress JSON API V2 to search people with filters, I used the add_meta_query_to_rest_query hook to filter my custom post type Person, if I add a simple filter it works :
$args['meta_query'][] = array(
'key' => 'firstname',
'value' => 'John',
'compare' => '='
);
But if I want to filter the persons living in Paris (which is a meta of the linked Custom post Type), I can't find the right syntax to do it. I tried to use city_name key.
$args['meta_query'][] = array(
'key' => 'city_name',
'value' => 'Paris',
'compare' => '='
);
Here is the only working way I found for now, with the ID :
$args['meta_query'][] = array(
'key' => 'city',
'value' => '"10"',
'compare' => 'LIKE'
);
Thanks
Upvotes: 0
Views: 510
Reputation: 164
ACF stores related post by its ID, so to match your search you need to use them (That's why it works in your last example). You need to get the Paris $post object so you can obtain its ID from it.
Just search your cities by slug (I think is a better approach than searching by tittle because slugs are unique).
$post_args = array(
'name' => 'paris',
'post_type' => 'city',
'post_status' => 'publish',
'numberposts' => 1
);
$my_city = get_posts($args);
$my_city_id = $my_city[0]->ID;
$args['meta_query'][] = array(
'key' => 'city',
'value' => $my_city_id,
'compare' => 'LIKE'
);
Upvotes: 1