Home › Forums › General Issues › How do you get a variable's data out of an ACF filter?
Hello Community,
Can someone please help me, I need to be able to test a field’s arguments, e.g. $field[‘type’], to determine which fields to work with, then store the field(s) (or some field argument data about the field(s) in an array for use in the function after the filter has completed.
I cannot get any data out of the load_field (nor the prepare_field) filter after it has completed. So I attempted to use PHP’s pass by reference to port this data out so that it could be accessed after the filter completed.
However, this does not work! Unless after the filter has finished, I run the ACF function get_field_object() on the same field that I just used in the load_field filter?! When I do this then the passed variable(s) contain the proper values. Does this seem like an ACF error or possibly a BUG?
// Create an array to hold the subfields of a repeater field that has sorting-filtering turned on.
$pass_by_reference = array();
// Check all repeater fields in the post for the field argument sorting-filtering
add_filter('acf/load_field/type=repeater', function ( $field ) use ( &$pass_by_reference ) {
if ( !isset( $field['sorting-filtering'] ) ) return $field;
if ( is_null( $field['sorting-filtering'] ) ) return $field;
if ( empty( $field['sorting-filtering'] ) ) return $field;
foreach ( $field[ 'sub_fields' ] as $subfield ) {
// Store the subfields label and key in the passed by reference array variable
$pass_by_reference[ $subfield[ 'label' ] ] = $subfield[ 'key' ];
}
return $field;
});
At this point, the $pass_by_reference is an empty array WHEN IT should contain the subfields of the repeater field(s) that had sorting-filtering turned on.
$pass_by_reference = [];
However when I call the same repeater field with get_field_object() directly after the filter has finished then the passed by reference data appears.
get_field_object( 'field_5c5771f906565', 9344 );
NOW the $pass_by_reference array has the proper values for the subfields.
$pass_by_reference = ["Client Email Address":"field_5c5771f906566","Sent Date":"field_5c5771f906567","Email Sender":"field_5c5771f906568","email Log":"field_5c5771f906569"];
Can someone please help me with this? This is not the only instance that I need to get data out of a filter. It continues to come up that after accessing a field or a group of fields with an ACF filter that there is some sort of data that needs to exist after the filter has run in order to continue with my processes. This may be because I have begun to rely upon the ACF render_field_settings() to create more complex configurations.
If you know of any other ways to get data out of a filter that would be very helpful!
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 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.