Home › Forums › General Issues › Loop acf/load_field with 'foreach'
Hi folks,
The basic idea is that for each option name in the array $select_fields, it will return and load dynamically dropdown lists.
The code works if I don’t insert it in a loop. The problem is that I would have to copy too many times the same function in my function.php file, not really efficient!
Here is my attempt so far in loading the function ‘load_select’ in a loop.
Hope someone can help as this would save me heaps of time !
Thanks in advance.
// Loop
$select_fields = array( 'boat-bow', 'boat-stern' );
foreach ($select_fields as $var ){ add_filter('acf/load_field/name=' . $var . '-select', 'load_select' ); };
function load_select ( $field ) {
// Get Selected + Default Values
$design_id = get_field('relationship');
$quantity = get_field( $var , $design_id);
$default = get_field( $var . '-default' , $design_id, false);
$default_price = get_field( 'price', $default) * $quantity;
// Query All Choices
$choices_query = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'schedule', 'category_name' => $var ));
foreach ( $choices_query as $choice ) {
$item = get_field('item', $choice->ID, false);
$description = get_field('description', $choice->ID, false);
$total = get_field('price', $choice->ID) * $quantity - $default_price;
$choices[$choice->ID] = '<b>' . $item . '</b> | ' . $description . ' | $' . $total ;
}
// Reset + Load Select Field
$field['choices'] = array();
$field['choices'] = $choices;
$field['default_value'] = $default;
var_dump($var);
return $field;
}
I’m not exactly sure what you’re having a problem with here. Can you explain a little more?
Hi John,
Thanks for your help.
var_dump($var) returns NULL and var_dump($field) returns all my custom ‘schedule’ posts, as if category_name -> 'my_category'
was not functioning/filtering properly inside the get_posts()
function.
I would like to know if there is any other way of looping acf/load_field with foreach as the code shown above does not work at the moment.
I believe that ‘catagory_name’ only works with posts in the ‘Post’ post type, but I could be wrong.
Have you attached the standard WP category taxonomy to the custom post type?
Hi John,
I have tried the code outside the loop and it is working.
It seems that I was missing a global variable in my function, hence why the previous code was returning NULL. However this still does not solve my problem..
I have isolated some of the code to identify what could possibly my error. I am now sure it is not taxonomy issue as you suggested.
The foreach loop is repeating the last item of the array. I am not sure if this has anything to do with ACF but I followed instructions from other forums regarding the rest of the code and it still does not work. Hope you can help.
$select_fields = array( 'boat-bow', 'boat-stern',);
foreach ( $select_fields as $var ){ add_filter('acf/load_field/name=' . $var . '-select', load_select ); }
function load_select () {
global $var;
var_dump($var);
}
try removing the comma (,) after boat-stern. This is creating a 3rd array element that is NULL
Hi John,
It did not change anything. The foreach loop is still repeating the last item of the array.
It is not possible for this too loop more than twice
$select_fields = array( 'boat-bow', 'boat-stern');
foreach ( $select_fields as $var ){
add_filter('acf/load_field/name=' . $var . '-select', load_select );
}
}
// function should be outside of loop
function load_select () {
global $var;
var_dump($var);
}
It still does not work though.
var_dum ($var) returns string(17) "boat-stern" string(17) "boat-stern"
I used the same piece of code as you.
$select_fields = array( 'boat-bow', 'boat-stern');
foreach ( $select_fields as $var ) {
add_filter('acf/load_field/name=' . $var . '-select', load_select );
}
function load_select () {
global $var;
var_dump($var);
}
Hi John,
I found the solution to this problem by using php function ‘use’. See below.
Thanks for your help!
$select_fields = array( 'cornices', 'bedrooms-flooring' );
foreach ( $select_fields as $var ) {
add_filter('acf/load_field/name='.$var.'-select', function ( $field ) use ($var) {
// some code using $var
});
}
The topic ‘Loop acf/load_field with 'foreach'’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.