Support

Account

Home Forums Front-end Issues sorting entries in repeater post object field

Solving

sorting entries in repeater post object field

  • I have a post object (session_speaker) in a repeater field (session) that is related to a different post type (speaker). My users choose a speaker (or more than one speaker) for a session in a day and then those speakers show up on the event for the day. I’m trying to figure out how to sort the speakers by their post titles no matter what order the user enters their names in the backend.

    I’ve uploaded my ACF field page showing the field entries, the page edit page where users enter the information, and the front end display.

    here is the code

    <?php if (get_field(‘session’)) {
    while (has_sub_field(‘session’)) {
    ?>
    <?php
    $hilite = get_sub_field(‘session_highlight’);
    if( in_array(‘highlight’, $hilite)) {
    $sessionbgclass=” dark”;
    }else{
    $sessionbgclass=””;
    };?>
    <?php if( in_array(‘breakout’, $hilite)) {
    echo ‘<div class=”nopad sessionentry’. $sessionbgclass.'”><div class=”sessionbreakout”>’;}
    else{
    echo ‘<div class=”sessionentry’. $sessionbgclass.'”>’;}
    ?>
    
    <div class=”sessiontime”><h3><?php the_sub_field(‘session_start’); ?></h3></div>
    <div class=”sessiondetails”><h3><?php the_sub_field(‘session_title’); ?></h3>
    <?php the_sub_field(‘session_description’); ?>
    <!– Start of speaker list –>
    
    <?php
    $sessionspeakers = get_sub_field(‘session_speaker’);
    if ($sessionspeakers) {
    $count = 0;
    echo ‘<div class=”sessionspeakers”>Speaker’;
    if (count($sessionspeakers) > 1) {
    echo ‘s’;
    }
    echo ‘:<br>’;
    
    foreach ($sessionspeakers as $speaker) {
    $count++;
    $speakerlink = get_permalink($speaker->ID);
    $speakertitle = get_field(‘speaker-title’, $speaker->ID);
    if (get_field(‘speaker-company’, $speaker->ID)){
    $speakercompany = “, “.get_field(‘speaker-company’, $speaker->ID);}
    else{$speakercompany =””;}
    
    if ($count > 1) {
    echo ‘<br>’;
    }
    echo ‘‘.$speaker->post_title.’, ‘.$speakertitle.$speakercompany;
    } // end foreach speakers ?>
    </div> <!– End of sessionspeakers list –>
    <?php } // end if speakers
    } // end while has_sub_field
    }
    else {echo “<h2>Day one schedule coming soon, check back!</h2>”;}
    // end if get_field
    ?>

    I tried putting this in my functions file and it causes a sort but i don’t know how to make it sort by title

    function rt_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['title'];	//what goes here?
    	}
    	
    	// multisort
    	array_multisort( $order, SORT_DESC, $value );
    	
    	// return	
    	return $value;	
    }
    
    add_filter('acf/load_value/name=session_speaker', 'rt_acf_load_value', 10, 3);

    Utilmately i would like to sort by last name (2nd word in post title) This is the code i was using before i found acf:

    function posts_orderby_lastname ($orderby_statement) 
    {
      $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
        return $orderby_statement;
    }

    If i could Incorporate that it would be AMAZING.

    Thanks!

  • Hi @rudtek

    You need to get the post title for the order sort. It should be something like this:

    function my_acf_sort_post_object( $value, $post_id, $field ) {
    	
    	// vars
    	$order = array();
    	
    	// bail early if no value
    	if( empty($value) ) {
    		return $value;	
    	}
    	
    	// populate order
    	foreach( $value as $i => $post_id ) {
            $post_title = get_the_title($post_id);
    		$order[ $i ] = $post_title;
    	}
        
        $order_lowercase = array_map('strtolower', $order);
    	
    	// multisort
    	array_multisort( $order_lowercase, SORT_ASC, SORT_STRING, $value );
    	
    	// return	
    	return $value;	
    }
    
    add_filter('acf/load_value/name=session_speaker', 'my_acf_sort_post_object', 10, 3);

    Could you please test it out?

    I hope this helps 🙂

  • Thanks for helping, with this. It didn’t work though. I’m not sure what the strolower is, but it looks to place everything in lowercase ( don’t need that). in any case it’s not affecting the order or changing case when i implement this code.

    I’m not sure that the title is right in the add_filter call.
    right now it’s name=session_speaker. should it be name=session?

    To make sure we’re on teh same page…
    CPT 1. (EVENT)

    session (repeater field)
    session_speaker (post object related to CPT SPEAKER)

    CPT 2. (SPEAKER)
    title (this is what i want to be sorted by.

    So when someone loads my event page and browses to the schedeule they will see a session that has several speakers. I want those items to sort by 2nd word in title of the SPEAKER posts that are showing up. If nothing else by ASC at minimum. Right now they are sorted by how we drag them in the page edit page when entering them (see image previously attached) I want to over-ride that so no matter how they are entered, they will present on front end in alpha ASC order.

  • Hi @rudtek

    The strolower() function is used to make the sorting insensitive.

    If you want to sort the repeater, then you need to use the “session.” But if you want to sort the post object, then it should be “session_speaker.” I haven’t tested it with your setup, but it’s working on my post object field. Could you please share the JSON export file of your field group so I can test it up on my installation?

    Also, could you please debug it like this:

    function my_acf_sort_post_object( $value, $post_id, $field ) {
    	
    	// vars
    	$order = array();
    	
    	// bail early if no value
    	if( empty($value) ) {
    		return $value;	
    	}
    	
    	// populate order
    	foreach( $value as $i => $post_id ) {
            $post_title = get_the_title($post_id);
    		$order[ $i ] = $post_title;
    	}
        
        echo "<pre>";
        print_r($value);
        echo "</pre>";
        
        echo "<pre>";
        print_r($order);
        echo "</pre>";
        
        $order_lowercase = array_map('strtolower', $order);
    	
    	// multisort
    	array_multisort( $order_lowercase, SORT_ASC, SORT_STRING, $value );
    	
    	// return	
    	return $value;	
    }
    
    add_filter('acf/load_value/name=session_speaker', 'my_acf_sort_post_object', 10, 3);

    And on the front end, just execute the get_field() function like this:

    get_field('session_speaker');

    Thanks 🙂

  • This reply has been marked as private.
  • Hi @rudtek

    I’ve just tested your export file and the code is working correctly on my installation. Maybe there are settings that only available in your site environment. In this case, providing the credentials to your site would be the best thing to do.

    Could you please open a new ticket and provide temporary admin credentials to your site? If you can create a staging/development site and provide the admin and FTP credentials to that site instead, that would be wonderful!.

    You can open a new ticket here: https://support.advancedcustomfields.com/new-ticket. Also, please don’t forget to explain the issue again and provide the link to this thread.

    Thanks 🙂

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

The topic ‘sorting entries in repeater post object field’ is closed to new replies.