Hi all, I do think that what I need has been done before maybe there is even a plugin or another ready made solution, I simply couldn’t find one as my searches seem to be too complex – probably I’m just missing out on phrasing my search the right way…
I have product pages and I want one custom field named ‘product of the month’ or something like that. Whenever this field is checked (and the product-page saved) I want to take a way the property from the old product of the month. Essential I need one value that con only be set to one product.
I know that I can write a function that checks on save and resets the value on the former product of the month. It’s not such a big deal, but it would be great to have added functionality like a warning/confirmation in the backend etc.
It feels like someone might already have built something like that but as I said I didn’t really have success searching so if someone knows of a solution I would be greatfull.
I don’t think I’ve ever seen something like this done before, and to be honest, I don’t know if it can be done.
It would be nearly impossible to have a confirmation to deselect the existing product of the month without have a second field (true/false) that would need to be checked that you can look at when validating the first field.
Thank you John for your reply.
I’m rather sure it can be done, and I might give it a try if I can finde the time and energy.
I somehow felt that this function would be something that has been whished for/needed by a lot of people before, but you might be right that it’s jus a fancy idea of mine…
Like you said, it is possible to check if there is another post selected during the validation process. But ACF can only return valid or not valid. There is no mechanism for a second response in order to change something else. The only thing you can do with a single field would be to always unselect the other post when a post is saved as selected.
That’s why I hoped that someone else had already done it. Sound like some work…
Given this, what I would do is….
I would create an acf/render_field action. This actions would look to find if there is already a currently set post and would output a hidden field with a true/false (1 or 0) value. The name of the field does not matter because it will not be saved, you just need to make sure it does not conflict with anything else.
Sorry, I don’t have any code for this example.
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!
© 2023 Advanced Custom Fields.