Pawan Kumawat

Sort A ACF Repeater Field Results

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

 

Categories

Related Blogs

Convert PHP Array into JavaScript Array.

To convert both a normal PHP array, a multidimensional PHP array, and a nested PHP array into JavaScript arrays, you can use JSON encoding and decoding as previously explained. JSON is well-suited for handling various data structures, including both simple arrays and complex nested arrays.

How To Limit Search To Only Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?