Support

Account

Home Forums Front-end Issues Count what select field options was chosen

Solved

Count what select field options was chosen

  • I’ve been trying to crack this and google doesn’t seem to have much (unless I’m asking the wrong question).

    I have 2 CPT and created a ‘relationship’ in one to pick CPT from the other.

    Inside the first CPT I have a select field, for example

    Number of bedrooms: 1, 3, 4, 5 etc.

    Each post has selected a bedroom number.

    What I’m trying to do is count which field in the select has been picked the least and most.

    So;

    Plot A has ‘2’ bedrooms
    Plot B has ‘4’ bedrooms
    Plot C has ‘4’ bedrooms
    Plot D has ‘4’ bedrooms
    Plot E has ‘2’ bedrooms

    From that, you can see 2 bedrooms option was selected the least and 4 bedrooms option was selected the most.

    I know it’s a count but can’t work out how to get the data from the chosen fields…

  • I’ve made some progress on this, I’m looking for this to work with my select field;

    <?php 
    	$array = array(3, 5, 5); 
    	echo (min($array)). ' - ' .(max($array)) ; 
    ?>

    So I can get the CPT loop of the ACF fields, but when I try and get that to show the min/max of above it errors

  • There isn’t enough information here. How are you attempting to get the value selected for each post?

  • Hi @hube2

    Sorry, I am using the relationship field in 1 CPT to get data from another (hope that makes sense?)

    So I have the loop of CPT (1) and inside that done this;

    <?php 
    	$dev_id = get_the_ID();
    	$doctors = get_posts(array(
    		'post_type' => 'house_types',
    		'meta_query' => array(
    			array(
    				'key' => 'development_plots', // name of custom field
    				'value' => $dev_id, // matches exactly "123", not just 123. This prevents a match for "1234"
    				'compare' => 'LIKE'
    			)
    		)
    	));			
    	if( $doctors ): 
    ?>
    	<?php 
    		foreach( $doctors as $doctor ): 
    	?>
    		<?php 
    			$field = get_field_object( 'bedrooms', $doctor->ID );
    			$value = $field['value'];
    			$label = $field['choices'][ $value ];
    		?>
    			<?php echo $value; ?> //these being 5 5 2 etc. 
    	<?php endforeach; ?>
    <?php endif; ?>

    Where at the end I was hoping to use the PHP from my previous message to show the min/max of the select fields, e.g.

    Min: 2
    Max: 5

  • In your query you must include the quotes

    
    'value' => '"'.$dev_id.'"', // matches exactly "123", not just 123. This prevents a match for "1234"
    

    In your loop you actually need to count the totals for each selection, I’m not 100% certain this will do what you want.

    
    $counts = array();
    if ($doctors) {
      foreach ($doctors as $doctor) {
        $value = get_field('bedrooms', $doctor-ID);
        echo $value;
        if (!isset($counts[$value])) {
          $counts[$value] = 0;
        }
        $counts[$value]++;
      } // end foreach
    } // end if
    echo array_search(min($counts), $counts); // min
    echo array_search(max($counts), $counts); // max
    
  • That seems to be working thanks John! 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Count what select field options was chosen’ is closed to new replies.