Support

Account

Home Forums General Issues Query by repeater field, order and categorise

Solved

Query by repeater field, order and categorise

  • Here’s where I’m at. I need to create an ‘index’ (much like at the end of a book) on a site I’m building, based around a repeater field.

    Currently, you can add a ‘programme’ post (CPT), and add ‘artist names’ to that, which is currently set up as a repeater field with two text fields.

    Now, I’m creating this ‘index’ page, which needs to list out all artist names, for all ‘programme’ posts, and organise them A—Z, filed under their own category. (See here for an example: http://minimalissimo.com/index/)

    So, say across 4 posts, there’s a total of 18 artist names, it would look something like:

    A
    Joe Allan
    Frank Aztec
    
    B
    Jane Bank
    
    C
    Mike Crichton
    Mandy Curtz

    I can output all the names no problem, and then use a bit of PHP to add the initial letter… but it’s failing to sort itself before this so I am getting the following:

    This is my code so far:

    <?php if ( have_rows('artists') ) : ?>
    	<?php $previous = null; ?>
    	
    	<?php while ( have_rows('artists') ) : the_row(); ?>
        <?php $firstLetter = substr(get_sub_field('last_name'), 0, 1);
            if ( $previous !== $firstLetter ) {
    			echo '<p>' .$firstLetter. '</p>';
    			$previous = $firstLetter;
            }
        ?>
        <p><?php the_sub_field('first_name'); ?>&nbsp;<?php the_sub_field('last_name'); ?></p>
    	<?php endwhile; ?>
    	
    <?php endif; ?>

    Can you guys help at all?

    Thanks,
    R

  • 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

  • Hi @elliot

    Many thanks for your help.

    This hasn’t outputted as expected. Looks like there’s both last name and last name for each artist (instead of first/last) and then it’s still not grouping all the Bs, all the Cs etc together.

    I managed to fix the first name/last name stuff, but it’s still not grouping.

    This is my full code below. Hope you can help, and many thanks for your help so far.

    <?php query_posts ( array ( 
    	'post_type' => 'programme',
    	'category_name' => 'archive',
    	//'meta_key' => 'last_name', 
    	//'orderby' => 'meta_value', 
    	//'posts_per_page' => -1, 
    	'order' => 'DESC' ) ); ?>
    
    <div class="container_12">
    	<div class="prefix_1 grid_10 suffix_1">
    		<?php while ( have_posts() ) : the_post(); ?>
    			
    			<?php 
    
    			$groups = array();
    			
    			if ( have_rows('artists') ) {
    				
    				while ( have_rows('artists') ) {
    					
    					the_row();
    					
    					
    					// vars
    					$first_name = get_sub_field('first_name');
    					$last_name = get_sub_field('last_name');
    					$first_letter = substr($last_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;
    					
    				}
    				
    			}
    			
    			// 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 endforeach; ?>
    				
    			<?php endif; ?>
    	
    		<?php endwhile; ?>
    	</div>
    </div>
    <div class="clear"></div>
    
    <?php wp_reset_postdata(); ?>
  • I think it needs to sort all the names, before it adds the first letter…

  • And if you see in the image below, where I have just print_r( $groups ) it needs to add all the arrays into one big array, then sort it.

  • Hi @rdck

    Please consult google for tutorials regarding how to sort an array. This is not relevant to the ACF plugin, so I can’t offer any more help.

    Thanks
    E

  • @elliot I’m just asking because I’m new to this and want to understand – but should the:

    <?php endwhile; ?>

    at the end of your code block actually be a second:

    <?php endforeach; ?>

    Like I said – just asking because I want to make sure I understand. Thanks in advance for your time and insight.

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

The topic ‘Query by repeater field, order and categorise’ is closed to new replies.