Support

Account

Home Forums Backend Issues (wp-admin) Sort on repeater field in admin

Solving

Sort on repeater field in admin

  • Hi there,

    I want to sort on a repeater field in the admin. I have a field called ‘mc_sort_id’ and I can set here a value like 1 or 2. In the front-end I can sort on this field, but how do I do this in the admin? I tried this but it doesnt work https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/

    https://www.dropbox.com/s/dcchk42p7ghua7i/Screenshot%202016-08-22%2008.55.08.png?dl=0

    Thanks in advance!

    Regards,
    Jarah

  • In order for the repeater to be sorted in the admin, you will actually need to update the field so that it is sorted. You can probably do this with an acf/update_value filter https://www.advancedcustomfields.com/resources/acfupdate_value/.

    
    add_filter('acf/update_value/name=my_repeater_field', 'my_sort_repeater_function', 10, 3);
    function my_sort_repeater_function($value, $post_id, $field) {
      // sort repeater array
      return $value;
    }
    
  • To follow up on the idea John Huebner mentioned to use the acf/update_value filter.

    First you will need the field key of the field you have named “mc_sort_id”.
    To get that go to the Field Group admin page, click the “display field key” button in help dropdown, then copy the “mc_sort_id”-field key (looking something like this “field_1231313”).
    Then you should be able to use the following code in your functions.php file.
    Just remember to replace “my_repeater_field” with the name of your repeater field and also replace “field_xxxxxxxx” with the field key you copied above.

    function my_acf_update_value( $value, $post_id, $field  ) {
    	$mc_sort_id_FIELD_KEY = 'field_xxxxxxxx'; // Set this to the field key of your field called "mc_sort_id".
    	
    	$order = [];
    
    	foreach ( $repeater as $i => $row ) {
    		$order[ $i ] = $row[ $mc_sort_id_FIELD_KEY ];
    	}
    
    	array_multisort( $order, SORT_ASC, $value );
    	
    	return $value;
    }
    add_filter( 'acf/update_value/name=my_repeater_field', 'my_acf_update_value', 10, 3 );
  • hi, and if the field is in option? I tried to add the above but to no avail.
    The only thing that is different is that my field is contained within the option page.

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

The topic ‘Sort on repeater field in admin’ is closed to new replies.