Home › Forums › General Issues › Get count of same values in an array › Reply To: Get count of same values in an array
@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();
?>
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.