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!
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.
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;
100Array (  => 3391  => 3410  => 3412 ) 150Array (  => 3391  => 3410  => 3412  => 1292  => 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!
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!
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!