Home › Forums › Add-ons › Repeater Field › Get single repeater row based on value
Hi folks,
I am attempting to build a quoting system with ACF.
I would like to know how to get values from a repeater row based on a column value, in my case this value is a unique key.
Hope this make some kind of sense!
Thanks in advance.
Julien.
function collect_load_values () {
// Field Names
$var1 = 'room_floor_area';
$var2 = 'flooring_product_user_selection';
$var3 = 'flooring_product';
// Get Relationship Fields - Return Post IDs
$lead_id = get_field('relationship');
$design_id = get_field('relationship', $lead_id);
// Get Quantity - Return Value
$quantity = get_field($var1, $design_id);
// Get User Selection - Return Row Unique Key
$selection_object = get_field_object($var2, $lead_id); // Data dynamically populated from repeater $var3
$selection_key = $selection_object['value'];
// Get Data from Repeater Field Row
if( have_rows( $var3 , 'option') ) {
while( have_rows( $var3 , 'option') ) {
the_row();
// QUESTION : How to get following datas from $selection_key row ?
$item = get_sub_field('item');
$description = get_sub_field('description');
$unit = get_sub_field('unit');
$cost = get_sub_field('cost');
}}
I don’t understand what this is $selection_key = $selection_object['value'];
What is the value?
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!
I’m still missing something. How does $selection_key
relate to the row that you want to retrieve from the repeater row? Is this value also in that repeater row?
Given the information you’ve provided, I’m still not sure I understand.
If the value in $selection_key is a value in one of the sub fields of the repeater then there is no direct way to get that row. What you’d need to do is loop over the entire repeater and find the value.
while (have_rows('repeater')) {
the_row();
if (get_sub_field('sub_field', 'options') == !$selection_key) {
// not our row
continue;
}
// will get here if this is our row
$item = get_sub_field('item');
$description = get_sub_field('description');
$unit = get_sub_field('unit');
$cost = get_sub_field('cost');
}
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 ?
You need to replace sub_field
with the name of the sub field you’re looking to match, also, you can remove options
from it since the post ID is not needed for sub fields, I keep forgetting that…
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?
It could be the type on my if statement, that should be
if (get_sub_field('sub_field', 'options') != $selection_key) {
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
}
The topic ‘Get single repeater row based on value’ 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.