Home › Forums › Front-end Issues › relationship -> query multiple values
Hello,
i’m trying to query a list of multiple ID values for relationship.
before this, i made an array() with my different IDS.
i found on this forum a tuto https://support.advancedcustomfields.com/forums/topic/query-multiple-values-in-relationship-field/
but it returns empty at my place.
my code :
// affichage des maisons d'hotes limitrophes
$maison_h = array(
'post_type' => 'maisons-hotes',
'post__not_in' => $post_id,
'meta_query' => array(
'relation' => 'OR'
)
);
//var_dump($cities);
foreach($cities as $cities_id) {
array_push($maison_h['meta_query'], array(
'key' => 'contact_city',
'value' => '"' . $cities_id . '"',
'compare' => 'LIKE'
));
var_dump($cities_id);
}
$test = get_posts($maison_h);
var_dump($test);
as i said before, $cities is an array with only 4 ID.
(array(4) { [0]=> int(1103) [1]=> int(1279) [2]=> int(1105) [3]=> int(1104) })
var_dump($cities_id); return the id’s ( int(1103) int(1279) int(1105) int(1104) )
but i don’t know why, my query return array(0) { }
I have at least 1 post for each of those ID’s.
if i do it manually, i get it to work
$args = array(
'post_type' => 'maison-hotes',
'post__not_in' => $post_id,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'contact_city', // name of custom field
'value' => '"1104"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
array(
'key' => 'contact_city', // name of custom field
'value' => '"1103"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
array(
'key' => 'contact_city', // name of custom field
'value' => '"1105"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
array(
'key' => 'contact_city', // name of custom field
'value' => '"1279"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
)
);
so i’m gonna do a foreach to get all my array, but i would like to understand whats wrong on the previous code
ok, got it !
$args = array(
'post_type' => 'maison-hotes',
'post__not_in' => $post_id,
'meta_query' => array(
'relation' => 'OR',
)
);
foreach($cities as $cities_id) {
$args['meta_query'][] = array(
'key' => 'contact_city', // name of custom field
'value' => '"'.$cities_id.'"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
);
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.