Support

Account

Home Forums Add-ons Repeater Field Sort by date a repeater field

Solving

Sort by date a repeater field

  • I am making a program for a festival using repeater field.
    Every rows have different sub field, including a “date & time” sub field.

    > I am searching for a way to order all the rows by date,
    using the time of the sub field.

    > And i am searching for a way to display the next coming event on a div aside.

    Is it possible to do that ?

    
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    
    <?php if( have_rows('programmation') ): ?>
    
    	
        <?php while ( have_rows('programmation') ) : the_row(); 
        ?>
    
        <div class="programmation">
            <?php the_sub_field('titre');?>
    
            <?php the_sub_field('auteur'); ?>
    
           	<?php the_sub_field('date');?>
    
            <?php the_sub_field('categories');?>
    
            <?php the_sub_field('resume');?>
    
         </div> 
    
        <?php endwhile; ?>
    
      <?php else :
    
        // no rows found
    
    endif;
    
    ?>
    
    <?php endwhile; ?>
  • Hi @simtwo

    Could you please tell me if you want to sort the repeater from the current post or all posts?

    If you want to sort the repeater from the current post, please check this page: https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/.

    If you want to sort the repeater from all posts, please check this answer: https://support.advancedcustomfields.com/forums/topic/calendar-by-month-with-date-repeater/#post-41019. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/.

    I hope this helps 🙂

  • Hi @James
    Thank you for your help, I’ve explored all your link.
    To answer your question : I am trying to sort a repeater from a single page, so the current post I guess ?

    I am trying to this (in the “Advanced” part).
    A code that allow to sort front and backend.
    https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/.

    So I use this code in my function.php

    
    
    <? 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['date'];
            
        }
        // DATE is my time sub field.
        
        // multisort
        array_multisort( $order, SORT_DESC, $value );
        
        
        // return   
        return $value;
        
    }?>
    
    <? add_filter('acf/load_value/name=scores', 'my_acf_load_value', 10, 3);?>
    
    

    And I have the same code (the one that is in my question) on my template.php

    Could you give me a hand ?

  • Hi @simtwo

    The acf/load_value hook doesn’t have the field name yet, so you need to use the field key instead. Also, you need to convert the returned date to timestamp by using the strtotime() function so you can sort it easily. Could you please try the following code?

    foreach( $value as $i => $row ) {
            
        $order[ $i ] = strtotime($row['field_1234567890abc']);
            
    }

    Where ‘field_1234567890abc’ is the field key.

    I hope this helps 🙂

  • Hey @James, thank you for your help.
    So I did try the following code you send me, it doesn’t seem to work yet.

    Here is what I wrote on my functions.php

    <? function my_acf_load_value( $value, $post_id, $field ) {
    
        $order = array();
        
    
        if( empty($value) ) {
            
            return $value;
            
        }
    
        foreach( $value as $i => $row ) {
            
        $order[ $i ] = strtotime($row['date']);
            
    }
    
        array_multisort( $order, SORT_DESC, $value );
      
        return $value;
        
    }?>
    
    <? add_filter('acf/load_value/name=date', 'my_acf_load_value', 10, 3);?>
    
    
  • Hi @simtwo

    Like I said before, acf/load_value doesn’t know the field name yet, so you need to use the field key.

    To get the field keys, kindly open the field group editor page and click the “Screen Options” menu. In this menu, please enable the “Field Keys” option for ACF PRO version or set the “Show Field Key” option to “yes” for the free version. I’ve attached a screenshot for your reference.

    Thanks 🙂

  • Ok thank you for the explanation, here is what I achieve

    <? function my_acf_load_value( $value, $post_id, $field ) {
    
        $order = array();
        
    
        if( empty($value) ) {
            
            return $value;
            
        }
    
        foreach( $value as $i => $row ) {
            
        $order[ $i ] = strtotime($row['field_5794d01d5c71a']);
            
    }
    
        array_multisort( $order, SORT_DESC, $value );
      
        return $value;
        
    }?>
    
    <? add_filter('acf/load_value/key=field_5794d01d5c71a', 'my_acf_load_value', 10, 3);?>
    
    

    So far, the order of the content doesn’t seem to change.
    I can’t seem to find where the problem come from, any idea ?
    Thank you very much @James

  • Hi @simtwo

    Could you please test it by changing how it is sorted from this:

    array_multisort( $order, SORT_DESC, $value );

    To this one:

    array_multisort( $order, SORT_ASC, $value );

    Also, could you please debug the value like this:

    print_r( get_field('programmation', 99) );

    Where ’99’ is the post ID.

    Thanks 🙂

  • Sorry I am learning php, I don’t know where to place the “debug”
    So far, still no change. Thank you.
    Here is my function.php

    <? function my_acf_load_value( $value, $post_id, $field ) {
    
        $order = array();
        
    
        if( empty($value) ) {
            
            return $value;
            
        }
    
        foreach( $value as $i => $row ) {
            
        $order[ $i ] = strtotime($row['field_5794d01d5c71a']);
            
    }
    
        array_multisort( $order, SORT_ASC, $value );
    
      
        return $value;
    
        print_r( get_field('programmation', field_5794d01d5c71a) );
        
    }?>
    
    <? add_filter('acf/load_value/key=field_5794d01d5c71a', 'my_acf_load_value', 10, 3);?>
  • Hi @simtwo

    Could you please open a new ticket and provide temporary admin credentials (and FTP account if it’s possible) to your site? 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 10 posts - 1 through 10 (of 10 total)

The topic ‘Sort by date a repeater field’ is closed to new replies.