There are certain limitations on what ACF locations can be accomplished programmatically using the ->setLocation() function along with the ->and() and ->or() additions. Being able to pass a complete array to ->setLocation() would allow significantly more flexibility.
For example, I'm currently trying to build 'Bulk Add [post_type]' admin pages for each of my custom post types, and I have an ACF fieldset I want on each of these pages. Originally I tried:
/* manual for demonstration, but would be generated programmatically */
$post_types = [
'team',
'testimonial',
];
foreach($post_types as $post_type) {
$bulk_add->setLocation('options_page', '==', 'bulk-add-' . $post_type);
}
This doesn't work, as each iteration through the array overwrites the location, so the field only appears on the final array item. I also can't do something like:
as I get a no such function: or error, since we're out of the setLocation context.
A solution that would be very extensible would be to allow passing an entire 'location' array (exactly as ACF expects it) to the setLocation function, in lieu of the 3 parameters it expects. That way, I could do something like this:
$post_types = [
'testimonial',
'team',
];
/* set blank array for population */
$location_array = array();
/* loop through the post types to add to array */
foreach($post_types as $post_type) {
/* each of these is a new 'and' argument in ACF */
array_push($location_array,
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'bulk-add-' . $post_type,
)
)
);
}
/* (this locations array now looks like:) */
$location_array_demo = array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'bulk-add-testimonial',
),
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'bulk-add-team',
)
)
);
/* set the location of the field set to the location array */
$bulk_add->setLocation($location_array);
I'd think implementing this would be a matter of:
if parameter 1 provided to setLocation is an array, pass that array directly as the 'location' parameter; ignore any subsequent ->and or ->or functions
else continue as usual (require 3 parameters, build the 'location' array with the ->and and ->or functions)
There are certain limitations on what ACF locations can be accomplished programmatically using the
->setLocation()
function along with the->and()
and->or()
additions. Being able to pass a complete array to->setLocation()
would allow significantly more flexibility.For example, I'm currently trying to build 'Bulk Add [post_type]' admin pages for each of my custom post types, and I have an ACF fieldset I want on each of these pages. Originally I tried:
This doesn't work, as each iteration through the array overwrites the location, so the field only appears on the final array item. I also can't do something like:
as I get a
no such function: or
error, since we're out of thesetLocation
context.A solution that would be very extensible would be to allow passing an entire 'location' array (exactly as ACF expects it) to the
setLocation
function, in lieu of the 3 parameters it expects. That way, I could do something like this:I'd think implementing this would be a matter of:
setLocation
is an array, pass that array directly as the 'location' parameter; ignore any subsequent->and
or->or
functions->and
and->or
functions)Thanks for the consideration!