Support

Account

Home Forums Front-end Issues relationship -> query multiple values

Solved

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'
    					);
    				}
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘relationship -> query multiple values’ is closed to new replies.