Support

Account

Home Forums Add-ons Repeater Field Sorting a group field

Solved

Sorting a group field

  • Hi there,

    I have what I thought was a very simple thing to do but I am just hitting a brick wall over and over again. I’m hoping someone can help.

    The set up: I have a CPT (sponsor) where I have assigned an ACF group (not an acf field group – just a group). Individual posts within the CPT receives input such as an image, child age, sponsorship availability, date of birth, etc. There is also an ID number assigned to each child to help with keeping track.

    The problem: On the CPT archive page, I need to sort and display these children by ID number. Going off the principle that you treat a group as you would a repeater field, I have tried several times to use the instructions found at https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/ to no avail. For some reason, the same child is being repeated over and over, sometimes with strange letters or numbers after the name.

    Unfortunately, I do not have an option to remove things from the group to allow for sorting because 180 kids have already been entered and their information all goes away when the sub_fields are moved out of a group.

    The code: My CPT is sponsor. My group name is “child_info” ( field key “field_5f99f4b780bbe” ). The group sub_field I want to sort by is “child_idnum” ( field key “field_5f99ed668d9eb” ).

    I have placed this code into my functions file:

    function my_acf_load_value( $value, $post_id, $field ) {
    	
    	// vars
    	$order = array();
    	
    	
    	// bail early if no value
    	if( empty($value) ) {
    		
    		return $value;
    		
    	}
    	
    	
    	// populate order
    	foreach( $value as $i => $row ) {
    		
    		$order[ $i ] = $row['field_5f99ed668d9eb']; //the name and key match each other
    		
    	}
    	
    	
    	// multisort
    	array_multisort( $order, SORT_ASC, $value );
    	
    	
    	// return	
    	return $value;
    	
    }
    
    add_filter('acf/load_value/name=child_idnum', 'my_acf_load_value', 10, 3); //the name and key match each other

    and this (quite pared down for testing) code into my archive-sponsor.php file (pls note: I’ve also tried using the field key(s) here with no luck):

    <?php 
    // Get repeater value
    $repeater = get_field('child_info');
    // Obtain list of columns
    foreach ($repeater as $key => $row) {
    	$speaker[$key] = $row['child_idnum'];
    	$fspeaker[$key] = $row['search'];
    }
    
    // Sort the data, ascending
    array_multisort($speaker, SORT_ASC, $repeater);
    
    // Display newly orded columns
    echo '<ul class="br-sl">';
    foreach( $repeater as $row ) {
    		echo '<li>';
    			echo the_title();
    	echo $row['child_idnum'];
    	echo '</li>';
    	 } 
    	echo '</ul><!-- .br-sl -->';  wp_reset_query(); ?>
    

    Any help or direction is greatly appreciated. I’m sure I am missing something simple but I am too close to see what that is.

    Thank you!

  • I’m not sure what changed but I decided to try some code I’d been working with previously and it has decided to work today (aggravating but I’ll take what I can get). For anyone in a similar boat, this is what I did:

    <?php query_posts(array(
            'post_type' => 'sponsor',
            'showposts' => -1,
    		'meta_key' => 'child_info_child_idnum',
    		'orderby' => 'meta_value',
    		'order' => 'ASC',
        	'post_status' => 'publish'));
    
    			if ( have_posts() ) :?>
    				
    				<ul id="spo_kids">
    					<?php while ( have_posts() ) : the_post(); 
    
    						if( have_rows('child_info') ): 
    					?>
    
    								<ul>
    
    										<?php while( have_rows('child_info') ): the_row(); 								$child_image = get_sub_field('child_picture');
     ?>
    
    															<li>
    																<div>
    
    																		<img src="<?php echo esc_url( $child_image['url'] ); ?>" alt="<?php echo esc_attr( $child_image['alt'] ); ?>" />
    																	<?php echo '<h5>';
    																		echo the_title();
    																		echo '</h5>';
    
    																		echo '<span class="c-dob">';
    																			echo 'DOB: ';
    
    																				if ( get_sub_field('birthday') ):
    																					echo the_sub_field('birthday');
    																				else: echo 'Unknown'; endif; 
    
    																		echo '</span>';
    
    																		if ( get_sub_field('date_verified') ):
    																			echo '<span class="c-verified">';
    																				echo 'Verified: ';
    																				echo the_sub_field('date_verified');
    																			echo '</span>';
    																		else: endif;
    
    																	?>
    																				<a class="spo-links" href="<?php the_permalink(); ?>#spo_form" />
    																					<?php if ( get_sub_field('currently_sponsored') == 'Special Need' ): ?>
    																						Special Need
    																					<?php else: ?>
    																						Sponsor Now
    																					<?php endif; ?>
    																				</a>
    																				<a class="spo-links" href="<?php the_permalink(); ?>" />Learn More</a>
    																</div>
    															</li>
    
    										<?php endwhile; ?>
    
    								</ul>
    
    <?php endif; endwhile; endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.