Support

Account

Home Forums Backend Issues (wp-admin) Sorting Admin Column with repeater

Solving

Sorting Admin Column with repeater

  • Hi i have this in my admin column :

    You can see screenshot here 🙂

    https://s22.postimg.org/5lzbokppt/Capture_d_e_cran_2016_09_15_a_13_30_36.png

    Rappel Nombre historique Durée total

    This is what i do to make it display :

    add_filter( 'manage_rent-orders_posts_columns', 'set_custom_edit_rappel_columns' );
    add_action( 'manage_rent-orders_posts_custom_column' , 'custom_rappel_column', 10, 2 );
    
    function set_custom_edit_rappel_columns($columns) {
       
        $columns['rappel'] = 'Rappel';
        $columns['historique_nombre'] = 'Nombre historique';
        $columns['temps_total'] = 'Durée total';
    
        return $columns;
    }
    
    function custom_rappel_column( $column, $post_id ) {
        global $post;
    	if($column == 'rappel')
    	{
    		$total = 0;
    		$repeater = get_field('historique_commande');
    		if ($repeater) {
    		  foreach ($repeater as $row) {
    		    if ($row['type_historique'] == 'Rappel') {
    		      $total++;
    		    }
    		  }
    		}
    		echo $total;
    	
    	}
    	
    	if($column == 'historique_nombre')
    	{
    		
    		$repeater = get_field('historique_commande');
    		if ($repeater) {
    		  
    		  echo count($repeater);
    		  
    		} else {
    			
    			echo '0';
    		}
    		
    	
    	}
    	
    	if($column == 'temps_total')
    	{
    		  
    			$repeater = get_field('historique_commande');    
    			$i = 0;
    			$len = count($repeater);
    			foreach ($repeater as $row) {
    			    if ($i == 0) {
    			       $datedebut = new DateTime($row['date']);   //$row['date']
    			       
    			       
    			    } else if ($i == $len - 1) {
    			        $datefin = new DateTime($row['date']);
    			        
    			       
    			        
    					$dteDiff  = $datedebut->diff($datefin); 
    			        
    			        $joursoupas = $dteDiff->format("%d");
    			        
    			        if($joursoupas == 0){
    				         echo $dteDiff->format("%d Jour - %H:%I:%S Heures"); 
    			        } else {
    				         echo $dteDiff->format("%d Jours - %H:%I:%S Heures"); 
    			        }
    			        
    			    }
    			    
    			    $i++;
    			}
    	
    	
    	}
    
    }
    

    I don’t no now how to make it able to sort thos 3 columns.

    I have tried

    function rappel_column_sortable($columns) {
    
              $custom = array(
    
              'rappel'    => 'rappel',
    
              );
    
          return wp_parse_args($custom, $columns);
    
    }
    
    add_filter( 'manage_rent-orders_sortable_columns', 'rappel_column_sortable' );'
    
    

    But it doesn’t work.

    Any idea how to sort by the number that i’m displaying in the admin column ?

    And for Durée total i’m showing the diff between two date time. Is it possible to also sort it ?

    Thanks in advance for your help

  • If you look you will find many topics started here about the same subject, sorting posts by the values in repeater fields. The short and ugly answer is that this cannot be done.

    The values in each repeater row are saved with a unique meta key. For example, let’s say that you have a repeater with a text sub field and you put values into three rows, the meta keys of the values are repeater_0_text, repeater_1_text, and repeater_2_text. There isn’t any logical way using SQL to create a query that will sort results based on this setup.

    I’ve found that repeaters should only be used for data that will be displayed without alteration and that a repeater should never be used when the data in them will be needed for complex operations, like sorting posts.

    I you must sort posts by these values then you will need to get creative and go beyond what ACF can do for you. We could, for example, easily sort posts by a repeater sub field if the values in that field were stored in the standard WP way for storing multiple values for a single meta_key and using an acf/save_post filter is is possible to get the values from a repeater and then store them properly. Here is an example. Please note, it is an example, it does not include all the necessary code and it may contain errors, I’m not testing this.

    
    // add an action that will run after acf is done
    add_action('acf/save_post', 'my_save_post_action', 20);
    function my_save_post_action($post_id) {
      // make sure the post is one we want to update
      if (get_post_type($post_id) != 'my-post-type') {
        return;
      }
      // set up a new meta key
      // that will hold the repeater sub field
      // in standard WP fashion
      // the meta key needs to be unique
      $meta_key = 'repeater_text_copy';
      // delete all the data form our meta key
      delete_post_meta($post_id, $meta_key);
      // now get the values from the repeater and
      // store in the new location
      if (have_rows('repeater', $post_id)) {
        while(have_rows('repeater', $post_id)) {
          the_row();
          // insert the post meta with $unique parameter set to false
          add_post_meta($post_id, $meta_key, get_sub_field('text'), false);
        }
      }
    }
    

    Now you can query posts and sort them based on the value in this new meta field.

  • Here’s an old topic that the developer commented on https://support.advancedcustomfields.com/forums/topic/order-custom-post-by-sub-custom-field-values/ which basically says the same thing but points out that you could do this if you only looked at the first row of the repeater when sorting.

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

The topic ‘Sorting Admin Column with repeater’ is closed to new replies.