I have a CPT named 'Physicians', and have assigned an ACF field named locations to that post type — locations is an ACF post object field.
I also have a 'Locations' CPT and intend on displaying the Physicians that work out of that office on the single template for that post type. I'd like to use the values populated within the locations post object field above (via a meta query) to filter and display them.
The current setup sits within single-location.php
WP Query
// Assign current Location page title to var
// Used in meta query to search for a match on 'Locations' field (per-physician)
$current_location_title = get_the_title();
// Build query based on query var values
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'physicians',
// Filter by current location only
'meta_query' => array(
array(
'key' => 'location',
'value' => $current_location_title,
'compare' => 'LIKE',
)
)
));
Because locations is a post object field, I actually need to be able to drill down into that array to find the true meta value, which is found at location->post_title. How can I reach that value in the meta key parameter within the query above?
meta_queryis very slow. If you want to filter posts by X then X should be a taxonomy, in this case ideally alocationtaxonomy. In this case alocationtaxonomy could be hundreds of times faster than thismeta_query. As for why you can't query, searching fortenwill also matchoftententetc, or eventenin unrelated fields stored within the same field. You can mitigate the problem somewhat, but it can't be solved and kept reliable