Support

Account

Home Forums Front-end Issues ACF Relationship – Loop, highest selected option

Helping

ACF Relationship – Loop, highest selected option

  • I’ve followed;

    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    To produce a list of houses and each house you select the number of bedrooms from a select field.

    What I’m trying to do, is during the loop it finds out which was the highest selected field e.g 4 Bedrooms and what was the lowest e.g. 2 Bedrooms (despite 1 Bedroom being an option, just not chosen).

    I can get the lowest selected or most selected e.g. if 4 Bedrooms was selected most, but trying to pick out the highest despite being only picked once.

    This is my code so far for min/max;

    <?php 
    	$bedroom_count = array();
    	if ($doctors) {
    		foreach ($doctors as $doctor) {
    			$bedrooms = get_field('bedrooms', $doctor->ID);
    			// echo $value;
    			if (!isset($bedroom_count[$bedrooms])) {
    				$bedroom_count[$bedrooms] = 0;
    			}
    			$bedroom_count[$bedrooms]++;
    		} // end foreach
    	} // end if	
    	 wp_reset_query(); 
    ?>
    <p>Bedrooms</p>
    <strong><?php echo array_search(min($bedroom_count), $bedroom_count); // min; ?> - <?php echo array_search(max($bedroom_count), $bedroom_count); // max ?></strong>
  • the number of bedrooms is the array index, array_search() searches the values which in your case is the number of times it was selected. If you looking for the most times or least times selected then you search the values. If you want to know the highest lowest selected you’d need to search the keys.

    
    $lowest_selected = min(array_keys($bedroom_count))
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF Relationship – Loop, highest selected option’ is closed to new replies.