Sort and Modify ACF Repeater Field Results with acf/load_value

Customize Advanced Custom Fields output using acf/load_value filter applied after field value loaded from database. Sort repeater field rows with array_multisort by specific subfield values using field keys, implement SORT_ASC or SORT_DESC ordering, modify content with str_replace for text replacements, target specific fields using name, type, or key modifiers like acf/load_value/name={field_name}, process values before display without changing database, apply filters to all fields or specific field types, perfect for alphabetical sorting, custom ordering, bulk text corrections, or dynamic content modifications in ACF repeater and other field types.

Sort A ACF Repeater Field Results

ACF Hooks Wordpress

Sort A ACF Repeater Field Results Tutorial/Guide

Sometimes getting ACF result is not enough. We may need to modify , filter or may be some other action. for that we need to use a ACF hook which is called ” ‘acf/load_value'” . my example is regarding sorting the result according to our choice . but we can use it to modify content. Filters the field $value after being loaded.  

apply_filters( 'acf/load_value', $value, $post_id, $field );

  Parameters $value (mixed) The field value. $post_id (int|string) The post ID where the value is saved. $field (array) The field array containing all settings.   Modifers This filter provides modifiers to target specific fields. The following filter names are available:    acf/load_value Applies to all fields.    acf/load_value/type={$type} Applies to all fields of a specific type.    acf/load_value/name={$name} Applies to all fields of a specific name.    acf/load_value/key={$key} Applies to all fields of a specific key.  

function my_acf_load_value( $value, $post_id, $field ) {
   $order = array();
   foreach( $value as $i => $row ) {
      $order[ $i ] = strtolower($row['field_612f382fb1ad5']);//field key
   }
  array_multisort( $order, SORT_ASC, $value );
  return $value;
}
add_filter('acf/load_value/name=keyword_name', 'my_acf_load_value', 10, 3);


my_acf_load_value( $value, $post_id, $field ) {
    if( is_string($value) ) {
        $value = str_replace( 'Old Company Name', 'New Company Name',  $value );
    }
    return $value;
}

// Apply to all fields.
add_filter('acf/load_value', 'my_acf_load_value', 10, 3);

// Apply to textarea fields.
// add_filter('acf/load_value/type=textarea', 'my_acf_load_value', 10, 3);

// Apply to fields named "hero_text".
// add_filter('acf/load_value/name=hero_text', 'my_acf_load_value', 10, 3);

// Apply to field with key "field_123abcf".
// add_filter('acf/load_value/key=field_123abcf', 'my_acf_load_value', 10, 3);

  More you can check on https://www.advancedcustomfields.com/resources/acf-load_value/ full article by ACF team  

๐Ÿ’ก Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.