I have an acf-options page with a message field that displays a list of all the posts fitting a certain criteria. The result would be displayed in a table – one post per row, with a checkbox in one of the column.
When saving the options page, I would like to update the value of specific custom field in all the posts that have the checkbox checked***
Is this doable with only PHP and JS, or will this require Ajax ?
Is this even doable with a message field, or I am tackling this issue the wrong way ?
***the value of the checkbox itself won’t have to be saved – the checkbox just indicates the action to do on a specific custom field of the post. Actually it doesn’t even have to be a checkbox. Could be a simple div, that would have its css class or .html() changed with JS on click.
(fyi, what i want to achieve is :
if the checkbox is checked => the value of a custom field is changed in the corresponding post => the post won’t appear anymore in the options page (because the php query filters it out)
Most likely I would use a repeater to do the same thing, unless I was planning on showing a very large number of rows.
You can do this with PHP. You would need to give your checkbox field a unique name. The you would need to create PHP that runs when the page is submitted to get your checkbox from $_POST and update the corresponding posts. Yes it is doaable. You can use an acf/save_post action.
As long as it’s not an ACF field and has a unique name it won’t be save anywhere by default.
The advantage for me is having the ACF fields submitted and using those rather than a custom input. The other advantage for me is not building a table in a message field.
But, it would not reduce work. I’d have to dynamically load the ACF repeater. I’d have to make all the fields except the checkbox readonly. I still have to do all the processing. And then I would need to delete the repeater data that ACF saved after I was done processing. In the long run this would probably be more work to be honest. I’d also need to weigh that against I needed this and the time I have available to get the job done. And like I said, how many posts would be shown. Make a repeater too long and bad things can happen so I’d have to look at the possibility of the repeater growing too large.
You must be logged in to reply to this topic.
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!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.— Advanced Custom Fields (@wp_acf) January 6, 2023
© 2023 Advanced Custom Fields.