Home › Forums › Bug Reports › load_field after using get_field_object & passing a variable by reference
Hi,
I have an unusual situation and I am not sure if it is by design, or a glitch, or something I am not doing properly. I have spent a week on this and isolated the problem.
It is a two-part problem that occurs in different situations but appears to be related.
Problem one:
I need to be able to use get_field_object on a field and then later in the same function use load_field filter on the same field, and it will no run the filter.
In this first problem, I need to be able to access the field object (get_field_object) for an array of field keys to build an array of the fields that have a custom field argument set, which was added with the action acf/render_field_settings.
Then loop this array with load_field. However, since we have already called this field with the get_field_object it will not run the filter.
// Create an array to hold field keys for fields that have the field argument of sorting-filtering turned on.
$filter_sorts = array();
// This is a partial array to illustrate the problem, there are over 50 fields in the post that could be in the array which would be dynamically built with get_post_meta() for all the fields on the post.
$collected_fields = array( 'field_5c60c179e7ffd', 'field_5c5771f906565', );
// Loop the array.
foreach ( $collected_fields as $collected_field ) {
// Get the field object for the fields in the array.
$field_settings = get_field_object( $collected_field, 9344 );
// Avoid the php unidentified object error for fields that do not have sorting-filtering turned on.
if ( !isset( $field_settings[ 'sorting-filtering' ] ) ) continue;
// Make sure sorting-filtering is not empty; if it is continue foreach.
if ( is_null( $field_settings[ 'sorting-filtering' ] ) ) continue;
if ( empty( $field_settings[ 'sorting-filtering' ] ) ) continue;
// Add field keys that have sorting-filtering turned on.
$filter_sorts[] = $field_settings[ 'key' ];
} // END foreach loop
// filter_sorts properly equals ["field_5c5771f906565"] because this is the only field of the two that has sorting-filtering turned on. Again this could be out of many fields that were passed by get_post_meta.
//Loop the filter_sorts array after it has been filled.
foreach ( $filter_sorts as $filtersort ) {
// Add the load_field filter
add_filter('acf/load_field/key='.$filtersort, function ( $field ) {
// Do someting
return $field;
});
} // END foreach loop
THIS WILL NOT WORK because we have called field ‘field_5c5771f906565’ with the get_field_object earlier in the function.
HOWEVER IF WE:
// Just send the field (as a test) by itself in an array without using get_field_object with the field that has sorting-filtering turned on it will run the filter.
$filter_sorts = array( 'field_5c5771f906565', );
//Loop the filter_sorts array.
foreach ( $filter_sorts as $filtersort ) {
// Add the load_field filter
add_filter('acf/load_field/key='.$filtersort, function ( $field ) {
// Do someting
return $field;
});
} // END foreach loop
THIS WILL WORK however, it is not what we need since I would not know which fields to manually add to the a filter_sorts array before using get_post_object to test them first.
Problem Two:
I need to be able to use closure to pass an array by reference so that we can add elements to it while in the filter, which we can access after the filter has ran.
// Create array to hold fields that have sorting-filtering turned on.
$test_pass_by_reference = array();
// Add the filter for a field that does have sorting-filtering turned on, this would be an array of fields, however, for simplicity I have called a single field that has sorting-filtering turned on.
add_filter('acf/load_field/key=field_5c5771f906565', function ( $field ) use ( &$test_pass_by_reference ) {
// Add this field to the array
$test_pass_by_reference = $field['sorting-filtering'];
return $field;
});
At this point the test_pass_by_reference is an empty array.
$test_pass_by_reference = [];
However, if I call the same field with get_field_object directly after the filter has been applied.
$field_settings = get_field_object( ‘field_5c5771f906565’, 9344 );
Then the test_pass_by_reference array will show the proper value.
$test_pass_by_reference = [ 1 ];
It is important that we can use acf/render_field_settings to add a field argument and then check that argument to decide what to do to other field arguments or vica versa use pass by reference to add values to an array that are persistent after the filter is complete to do something to these fields without having to call get_field_object for the passed variables to exist.
Please help.
Thank you
Geoffrey
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.