0

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?

2
  • 1
    You can't. Not with WP_Query. But why do you need the title though? Can't you compare the ID to the current post ID, instead of the titles? Commented Mar 28, 2023 at 21:09
  • 2
    note that even if you could reliably do this, which you can't, meta_query is very slow. If you want to filter posts by X then X should be a taxonomy, in this case ideally a location taxonomy. In this case a location taxonomy could be hundreds of times faster than this meta_query. As for why you can't query, searching for ten will also match often tent etc, or even ten in unrelated fields stored within the same field. You can mitigate the problem somewhat, but it can't be solved and kept reliable Commented Mar 28, 2023 at 22:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.