wp query – Use value from meta key array for use in WP_Query


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(
            '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?

Graeme Bryson 2 months 2023-03-28T16:03:43-05:00 0 Answers 0 views 0

Leave an answer