I am trying to filter the contents of a WP Query using a meta_query. I have a feeling it's to do with the saved value being a serialised array but I've tried a few fixes online to no avail. Should have all the info you need below in code comments.
$user_id = get_current_user_id();
var_dump($user_id); //int(1)
$assignedZones = get_field('author_assigned_zones', 'user_'.$user_id);
var_dump($assignedZones); //array(1) { [0]=> object(WP_Term)#17784 (10) { ["term_id"]=> int(9) ["name"]=> string(6) "Zone 2" ["slug"]=> string(6) "zone-2" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(9) ["taxonomy"]=> string(5) "zones" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } }
$zoneIDs = array();
var_dump($zoneIDs); //array(0) { }
foreach ($assignedZones as $assignedZone ) {
$zoneIDs[] = $assignedZone->term_id;
} var_dump($zoneIDs); //array(1) { [0]=> int(9) }
$hostQuery = new WP_Query(array(
'post_type' => 'hosts',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'host_zones',//DB Value = a:3:{i:0;s:1:"9";i:1;s:2:"12";i:2;s:2:"13";}
'value' => $zoneIDs, //array(1) { [0]=> int(9) }
'compare' => 'IN'
)
)
));