Hello, my repeater is located in profile page
‘great_repeater’->
–‘sub_field_1’
–‘sub_field_2’
$id = userid
$key = correct row
1 = value (for true/false -field)
update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 1, ‘user_’ . $id); //THIS WORKS JUST FINE BUT…
I need to check the value(1) before so how can I do it with get_sub_field?
My final goal is:
if (get_sub_field(something..something..wuts?) == 0) {
update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 1, ‘user_’ . $id);
} else {
update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 0, ‘user_’ . $id);
}
<?php if( get_field('great_repeater') ): ?>
<?php while( has_sub_field('great_repeater') ): ?>
<?php
$select = get_sub_field('sub_field_1');
?>
<?php foreach( $select['choices'] as $k => $v ): ?>
ifelse
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
You can’t use get_sub_field() outside of a have_rows() loop.
You can get the value of that sub field directly using get_post_meta().
For example, if your repeater is named “repeater” and the sub field you want to check is named “sub_field” and the row you want to check is the 2nd row then
$value = get_post_meta($post_id, 'repeater_1_sub_field', true);
indexes start a 0 so the second rows index is 1.
This is how repeaters and sub fields are named
"{$repeater_name}_{$index}_{$sub_field_name}"
@hube2 I think you have a typo in your answer. It should be:
$value = get_post_meta($post_id, 'repeater_1_sub_field', true);
.. that is, a comma after the $post_id instead of a period.
Cheers!
well spotted, I’ve updated my comment to correct