Home › Forums › Add-ons › Gallery Field › Gallery field – query filtering by taxonomies
We’re doing a query on images uploaded to a Gallery field and need to filter results taking in account taxonomies attached to those images (setup in the ‘media library’).
1) I upload 5 images to the ACF gallery field in my post.
2) In the ‘media library’ I add categories to each one of those 5 images (colours for example: black, blue, yellow, etc).
3) In a query, I need to output all images in the ACF gallery field that are category ‘yellow’.
We checked this page and found that taxonomies are not included in the results.
Does anyone know how I can achieve this?
Thank you very much! 🙂
Does anyone have any idea on how I can achieve what I am after? 🙂
Also, I saw in the changelog for the latest update (5.7.9) there is a line that says:
* Dev – Added filter ‘acf/connect_attachment_to_post’ to prevent connecting attachments to posts.
Does this mean that it is possible to create a query that pulls images from the media library to display on a page, keeping it connected to the post they are attached to? In this way, when you click on the image it could take you to the post they are attached to?
This explains what I am trying to achieve:
ACF does not support taxonomies for media files because this is not supported natively by WP. Customization needs to be done in WP to do this, either through code or by using a plugin. I’ve looked into the ACF code and there isn’t any way to filter images for a gallery by taxonomy and there isn’t any way to add taxonomies to the array of images returned.
(I suppose that you could add a load value filter https://www.advancedcustomfields.com/resources/acf-load_value/ or maybe a format value filter https://www.advancedcustomfields.com/resources/acf-format_value/ to the gallery field to add this information to the value before it is returned, but I’m not 100% sure on how to do this.)
I’m not exactly sure where you want to do this filtering.
If you want to do it in the admin you’d need to build a new type of gallery field that includes filters for your custom media taxonomy https://www.advancedcustomfields.com/resources/creating-a-new-field-type/. It may also be possible to add field settings to the existing gallery field https://www.advancedcustomfields.com/resources/adding-custom-settings-fields/ and then add filters to the gallery field to alter how it works to use these settings to filter images, but I’m not even sure this is possible. The gallery field works in conjunction with the standard WP media library popup and dealing with this can be complicated if it can be done at all.
If you want to filter them on the front end…. if it were me I’d add a filter to the gallery load value hook, or more likely I would loop though all the images that ACF returns and only show the ones that should be shown based on additional queries.
Sorry for the long answer that probably didn’t help you, but there isn’t a simple solution to this and any solution is going to involve a lot of coding to accomplish.
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!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.