Thanks again for the help – I think I’ve solved the problem! It was all about handling the loops properly.
Keep up the good work!
Hi guys,
Thanks for your help firstly! I’m really impressed with the quality of this plugin, I know I’m trying to do something tricky here with only a working knowledge of PHP. Everything else I’ve used ACF for (lots of applications!) works like a charm straight out of the box.
Now.
I tried removing the postdata functions, but that doesn’t seem to have changed anything.
Taking the $idlist declaration out of the loop does seem to have helped- I set up two test repeater rows and posts chosen in both are being affected.
However – only the values from the last repeater row are being applied. I.e. I have two sales, one takes $50 off a group of posts, the second takes $150. Now all the posts selected are showing $150 reduction.
This is the current code I’ve put in a test page template to get the print:
if(get_field('sales', 'option')):
while(has_sub_field('sales', 'option')):
$saletype = get_sub_field('sale_type');
$discount = get_sub_field('discount_amount');
$saleitems = get_sub_field('devices_on_sale');
if( $saleitems ):
foreach( $saleitems as $post):
$thisid = $post->ID;
$idlist[] = $thisid;
endforeach;
endif;
echo($discount);
print_r($idlist);
endwhile;
endif;
Which yields:
100Array ( [0] => 3391 [1] => 3410 [2] => 3412 ) 150Array ( [0] => 3391 [1] => 3410 [2] => 3412 [3] => 1292 [4] => 751 )
So by my understanding, this is adding both groups of selected posts to the same array.
Perhaps during the foreach loop, the $post->$ID can be stored differently… I have a vague idea – what I need is not a single named array, but some kind of anonymous array that can be referenced differently somehow, when I need it?
Alternatively, could I create a “name” text subfield for each repeater row to provide each row with a unique identifier, then reference the related array from that somehow?
Argh! Any further help much, much appreciated! Cheers for having such great support!
This is no longer relevant- delete if you wish
P.S. – It occurs to me that I may be able to use a different methodology here-
We have pages on which category posts are displayed – technically in the loop on those pages, for each post I can check if that post has been selected for a sale, and do whatever I want to the post formatting then – correct? If you could point me in the right direction on how to approach either of these methods I’d greatly appreciate it. Thanks again!
Hi there, thanks for your response!
To clarify, now I have set up an Options Page with a repeater field called “Sales”. This repeater contains three fields:
“Sale type” – Select – Fixed $ amount / Fixed % discount
“Discount” – Number – Amount of discount
“Devices” – Post Object – Selects applicable posts
Then, for the main device post type, those three fields will be duplicated. Now what I need is a way to pass those values to the fields in the posts chosen. I understand I can use update_field() – I’m thinking I can write a loop that collects the post IDs from the post object field and then uses update_field() to apply the values selected to those posts.
That seems to make sense- but then I’m not sure how I would even call the update_field() action or where that would live. I am far from an expert in WordPress plugin coding, so any help is much appreciated!
Cheers
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.