Support

Account

Home Forums General Issues Get count of same values in an array

Solved

Get count of same values in an array

  • I’m far from a PHP professional, but I’m making out pretty well. However, I’ve been hung-up on something for a while now and would appreciate some help.

    I have a custom post type that has a radio button field with 3 options. I need a count of how many “posts” of each option exists.

    I’ve tried using array_count_values, but with no luck… and it’s probably just how or where I’m using it.

    I’m calling my WP_Query arguments like so:

    
    $args = array(
    	'posts_per_page' => -1,
    	'post_type' => 'team-member',
    	'meta_key' => 'team_member_type',
    	'meta_value' => array(
    			'Scientist or Technical',
    			'Administrator',
    			'Student'
    			),
    	'orderby' => 'title',
    	'order' => 'ASC'
    );
    

    The ‘team_member_type’ is my radio button field. I’m probably doing this wrong, but have listed all 3 options as a sub array so I can cycle through them with a foreach statment…

    
    $args['meta_value']
    

    and use the value to output just the team members with that value… arranging them into sections by type.

    I need to know ahead of time how many posts are in each section so I can divide them into equal height columns. How would I get the count of same values from ‘meta_value’?

    With no luck, I’ve tested this…

    
    $teamMemberTypes = array_count_values( $args['meta_value'] );
    
    foreach ( $teamMemberTypes as $key => $value ) {
    	echo "Key: $key; Value: $value<br />\n";
    }
    

    Thanks for any help, suggestions or a better way to do it.

    If this helps, here’s my whole WP_Query code.

    
    <?php
    
    // Setup custom post query
    $args = array(
    	'posts_per_page' => -1,
    	'post_type' => 'team-member',		// name of custom post type
    	'meta_key' => 'team_member_type',	// name of custom field
    	'meta_value' => array(
    						'Scientist or Technical',
    						'Administrator',
    						'Student'
    					),	// custom field values
    	'orderby' => 'title',
    	'order' => 'ASC'
    );
    
    $the_query = new WP_Query( $args );
    
    // Make sure posts exist
    if( $the_query->have_posts() ) :
    
    	// Setup section for the team member type
    	foreach ( $args['meta_value'] as $val ) { ?>
    
    		<h2 class="pgtitle"><?php echo $val; ?></h2>
    		
    		<ul class="team-members">
    		<?php while( $the_query->have_posts() ) : $the_query->the_post();
    
    			$teamMemberType = get_field( 'team_member_type' );
    			$teamMemberLocation = get_field( 'team_member_location' );
    
    			// Populate team members into the proper section
    			if( $teamMemberType == $val ) { ?>
    
    				<li>
    					<?php
    
    					// Add an icon to indicate their location
    					if ( $teamMemberLocation == 'pensacola' ) {
    						echo '<i class="fa fa-circle location-icon"></i>';
    					} else if ( $teamMemberLocation == 'ocala' ) {
    						echo '<i class="fa fa-circle-o location-icon"></i>';
    					}
    
    					// Final output ?>
    					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    				</li>
    			<?php } ?>
    			
    		<?php endwhile; ?>
    		</ul>
    	
    	<?php }
    
    endif; ?>
    
  • You WP query won’t work the way you’re doing it, a meta value can’t an array as far as I know.

    There really isn’t what I would consider a “Good” solution to this problem. WP really doesn’t offer a way to get the number of posts with a specific meta_value using WP_Query

    These are the choices I can think of.

    1) Get all the posts and loop through them twice, the first to see how many are in each “Group” and the second to output them. You can group them by using a new feature of WP_Query added in 4.2 and outlined here. https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

    I think this is probably the best choice because you’re only doing one query. At the end of the first loop you can use rewind_posts() https://codex.wordpress.org/Function_Reference/rewind_posts.

    2) Do 3 separate queries, one for each group, with a meta query for each groups, loop through the posts and store the post into a separate array for each group. Then you know know many are in each array and you can loop over each array to display. This is not as good a solution because you’re doing 3 queries.

    3) Probably the best choice, but a bit more complicated coding wise. Skip WP_Query and use the wpdb class directly. https://codex.wordpress.org/Class_Reference/wpdb. While it would probably be the best choice I don’t generally want to work that hard if I can accomplish what I want without it, as in my 1st suggestion.

  • @hube2 Thanks for the thorough reply. I’ve revisited my approach and implemented the first suggestion. I’m sure my code is not the cleanest, best way to accomplish this, but it’s working.

    Now, I just need to break my loop into columns. Thanks again.

    
    <?php
    
    // Setup vars
    $teamMemberTypes = array();
    $teamMemberScientistCount = 0;
    $teamMemberAdministratorCount = 0;
    $teamMemberStudentCount = 0;
    
    //
    // Setup first custom post query get team_member_type same value counts
    //
    
    $args1 = array(
    	'post_type' => 'team-member',	// name of custom post type
    	'meta_key' => 'team_member_type',	// name of custom field
    	);
    
    // The Query
    $the_query = new WP_Query( $args1 );
    
    // The Loop to get total counts
    if ( $the_query->have_posts() ) {
    				
    	while ( $the_query->have_posts() ) {
    		$the_query->the_post();
    
    		// Store team_member_type choices for use in second query
    		$teamMemberTypes = get_field_object( 'team_member_type' )[choices];
    		$teamMemberType = get_field( 'team_member_type' );
    					
    		if ( $teamMemberType == 'Scientist or Technical' ) {
    			$teamMemberScientistCount++;
    		} else if ( $teamMemberType == 'Administrator' ) {
    			$teamMemberAdministratorCount++;
    		} else if ( $teamMemberType == 'Student' ) {
    			$teamMemberStudentCount++;
    		} else {
    			echo 'It looks like a new Team Member Type has been added that is not recognized.';
    		}
    	}
    
    	// Add one more to each team member type to make the count correct
    	++$teamMemberScientistCount;
    	++$teamMemberAdministratorCount;
    	++$teamMemberStudentCount;
    
    } else {
    	echo 'Can not get same value count.';
    }
    
    /* Restore original Post Data */
    wp_reset_postdata();
    
    //
    // Setup second custom post query for page output
    //
    
    $args2 = array(
    	'posts_per_page' => -1,
    	'post_type' => 'team-member',		// name of custom post type
    	'meta_key' => 'team_member_last_name',	// name of custom field
    	'orderby' => 'team_member_last_name',
    	'order' => 'ASC'
    	);
    
    // The Query
    $the_query = new WP_Query( $args2 );
    
    // The Loop to output content on page
    if ( $the_query->have_posts() ) {
    
    	foreach ( $teamMemberTypes as $value ) {
    					
    		echo '<h2 class="pgtitle">' . $value . '</h2>';
    		echo '<ul class="team-members">';
    
    		while ( $the_query->have_posts() ) {
    			$the_query->the_post();
    
    			$teamMemberType = get_field( 'team_member_type' );
    			$teamMemberLocation = get_field( 'team_member_location' ); 
    
    			if ( $teamMemberType == $value ) {
    				echo '<li>';
    
    				// Add an icon to indicate their location
    				if ( $teamMemberLocation == 'pensacola' ) {
    					echo '<i class="fa fa-circle location-icon"></i>';
    				} else if ( $teamMemberLocation == 'ocala' ) {
    					echo '<i class="fa fa-circle-o location-icon"></i>';
    				}
    
    				// Final output
    				if ( $teamMemberType == 'Student' ) {
    					the_title();
    				} else {
    					echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    				}
    				echo '</li>';
    			}
    		}
    	echo '</ul>';
    	}
    
    } else {
    	// no posts found
    	echo 'No Posts Found';
    }
    
    /* Restore original Post Data */
    wp_reset_postdata();
    
    ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get count of same values in an array’ is closed to new replies.