Home › Forums › General Issues › Query by repeater field, order and categorise › Reply To: Query by repeater field, order and categorise
Hi @rdck
To get your data to group via the first letter, you will need to use an array to store the data in a grouped fashion like so:
<?php
$groups = array();
if ( have_rows('artists') ) {
while ( have_rows('artists') ) {
the_row();
// vars
$first_name = get_sub_field('last_name');
$last_name = get_sub_field('last_name');
$first_letter = substr($first_name, 0, 1);
// add $first_letter holder to groups
if( !isset($groups[ $first_letter ]) ) {
$groups[ $first_letter ] = array();
}
// append artist to group
$groups[ $first_letter ][] = $first_name . ' ' . $last_name;
}
}
// test $groups - can be removed after testing
echo '<pre>';
print_r( $groups );
echo '</pre>';
// ouput
if( !empty($groups) ): ?>
<?php foreach( $groups as $letter => $artists ) : ?>
<h3><?php echo $letter; ?></h3>
<?php foreach( $artists as $artist ): ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
hope that helps.
Thanks
E
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.