Home Forums Front-end Issues acf/fields/post_object/query in Block


acf/fields/post_object/query in Block

  • 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.

    I’ve used 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

    1. A Taxonomy Field
    2. A Post Object Field

    And you want to filter the posts available for selection in the post object field by the selected term.

    This is not possible without custom coding. You will need to add custom JavaScript to ACF and you will need to use the ACF JS API.

    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.

  • Thanks, John. In hindsight, using the post object field wasn’t really necessary for this particular issue. I simplified it by switching to using the select field and populating it with the load_field filter.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.