Hi,
Solved my issue with this bit of code.
foreach($_POST['row'] as $rows)
{
$row = array(
'field_59131578f9daf' => $rows['category'],
'field_59104ba3d8b5a' => $rows['product'],
);
add_row('field_59104b8fd8b59', $row, $post_id);
}
<input type="hidden" name="row[<?php echo $i; ?>][category]" value="<?php echo $term_id; ?>" />
<input type="radio" name="row[<?php echo $i; ?>][product]" value="<?php echo get_the_ID(); ?>" <?php if ( get_the_ID() == $product_id ) echo 'checked="checked"'; ?> ><?php the_title(); ?>
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
});
}
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,
It did not change anything. The foreach loop is still repeating the last item of the array.
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);
}
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.
Hi @dougfitz ,
This should help you in solving the first part of your problem.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Awesome! Thanks for your help. You just saved me a few nights.
Here is the final solution that worked for me :
while (have_rows('repeater_name')) {
the_row();
if (get_sub_field('sub_field') != $unique_key) {
// not our row
continue;
}
// will get here if this is our row
}
Hey @hube2 ,
I have already changed that, it still does not work.
I also made sure that the value we want to compare are at the same format using var_dump.
Any other reason why this would not work?
Thanks @hube2 ,
It looks like this the solution I need. However the code does not work. It always displays the latest value of the repeater.
Is this because it first loop through ‘ get_sub_field(‘sub_field’, ‘options’)’
How to prevent this ?
Hi @hube2 ,
Thanks for the help!
$selection_key returns an auto-generated key value. (i.e. ‘key_01’).
(If anyone is interested I just used this plugin https://github.com/andersthorborg/ACF-auto-generated-value)
I obtain this ‘$selection_key’ from a dynamically populated select field which gather data from a repeater field row.
So the $selection_key correspond to the selected choice.
I would like to use this unique key value to retrieve ALL data from the repeater field row.
Hope this make sense!
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.