I’ve built an ACF block which allows the user to embed a pre-built
acf_form along with a few options. One of those options is selecting a taxonomy term (service-category) which will determine which services (post type) the end user can choose from in the ‘what service are you enquiring about’ field.
However, I can’t figure out how to filter my
acf_form field to show the services from the chosen taxonomy.
acf/load_field to filter the field args but this appears to be too late in the process and, while the amended arguments appear as I want them, the post_object field returns “The results could not be loaded”.
I’m also filtering
acf/fields/post_object/query but here don’t have access to any of the block (or post) data so can’t amend my query args according to saved meta.
Finally, when logging the query args via
acf/fields/post_object/query I can see that they are unchanged even when using
acf/load_field which causes “The results could not be loaded” – so load_field is having some effect but not the desired one.
Any pointers or ideas for making this work will be gratefully received.
This will work the same way whether using blocks or not using blocks.
To summerize and make sure I’m not missing anything.
You have two fields in your group
And you want to filter the posts available for selection in the post object field by the selected term.
In your JS code you will need to use the select2_ajax_data hook and get the value selected in the taxonomy field and add it to the values submitted in the AJAX request.
Then in your acf/fields/post_object/query filter you will need to get this value from $_POST and use the value to alter the query done for the post object field.
You may also need to trigger a refresh of the post object field or delete any value that is already selected in the post object field if the term selected in the taxonomy field is changed. I don’t have any references on how to do this part.
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!