Support

Account

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