Looking for best solution

Hello all

I am in the process of removing a plugin, from quite a few sites, that was once bundled with Pro and X themes (The Grid).
The Grid plugin added the function to add categories to files (attachments) held in the Wordpress media library and then allowed you to create a gallery that could be filtered based on category.

So i am looking for advice on the how best to achieve this using loopers, possibly twig and ACF pro.

I know i can add categories to the media via ACFpro. I also know i can use a looper with a query string such as this:
post_type=attachment&post_mime_type=image&posts_per_page=12&post_status=any but it doesnt feel like the most efficient way and its really awkward to build the query strings that cover the categories or at least i am struggling to do it dynamically. In head i would use URL parameters to adjust the images shown in the gallery but also knowing i could use FacetWP if i wanted something a bit more slick.

I have experimented with a Lopper provider of query builder with a Post type of Media but this doesn’t seem to return anything.

Is this something i can start exploring with twig?

Any guidance is much appreciated

Hello @The_Capture_Factory,

Thank you for the inquiry.

You might want to check out the ACF Gallery field, which can be accessed using the Looper Provider > Dynamic Content. Please check out this documentation for more info: https://theme.co/docs/acf-pro-and-loopers#multi-dimensional-fields

You can also watch this video for additional instructions: https://www.youtube.com/watch?v=BFJdDM078VI

Unfortunately, this doesn’t include filtering by category, which might be more challenging using this approach compared to using query strings. Let us know if you need more details.

Best regards

So it sounds like using a query string is still the best way of accessing the information in the media library. so if i wanted to filter the posts (attachments ) returned by this query string

post_type=attachment&post_mime_type=image&posts_per_page=12&post_status=any

how would i add a specific category to the query string above. I have tried leveraging this tool https://buildingonwp.com/tools/query-builder/ but the string it returns fails to work when using it

Yes, using a query string might still be the best approach, especially when you’re trying to filter items by category. Unfortunately, the media or attachment post type doesn’t support categories by default, so you will still need to install a media category management plugin, such as the following.

https://wordpress.org/plugins/wp-media-category-management/
https://wordpress.org/plugins/wp-media-library-categories/

You can then use the tax_query parameter to filter items by category. The string should look something like this:

post_type=attachment&post_mime_type=image&posts_per_page=12&post_status=any&tax_query[0][taxonomy]=media_category&tax_query[0][field]=slug&tax_query[0][terms][0]=category-slug-1&tax_query[0][terms][1]=category-slug-2

The example above will retrieve items from “category-slug-1” and “category-slug-2.” The taxonomy name “media_category” may need to be adjusted based on the plugin you use.

can i not use ACFpro to create the media categories ?

i was going to re-use the method you used here Accessing the information in an ACF bidirectional relationship field array
to expand the array and then find what i need

i can see the media metadata array in the dc builder (see screen shot)
just getting the syntax wrong i think

How did you apply categories to the attachment post type using ACF? Did you create a custom taxonomy? If you’re only using a Select field, for example, then you will need to adjust the query string above to use meta_query instead of tax_query .

post_type=attachment&post_mime_type=image&posts_per_page=12&post_status=any&meta_query[0][key]=your_acf_field&meta_query[0][value]=your_value&meta_query[0][compare]=LIKE

If you can provide the login details in secure note, we’ll try to check your setup and provide a possible solution.

Did you enable the Pro > Theme Options > Twig > Debug option?

Details attached

We adjusted the Twig template, and the media_category returns an array with a single value holding the number 8 , but we’re not sure what it means or if it’s possible to use this value to filter the images. How did you apply categories to the images?

i set the category on the media in ACFpro taxonomies and selected post type media


i had previously tried using a field group and applying it to post type but there was no attachment post type listed

How do you apply the terms or categories to the actual images? As mentioned above, you might still need to use a dedicated media category management plugin because we are not sure if it is possible to group images by categories using ACF. Also, the media_category field might simply be an orphan meta info from the old plugin, The Grid, which is no longer useful in this case.

I am going to create a fresh sandbox site to test in. I’ll repost with information once it setup and running

Hi @The_Capture_Factory,

Thanks for the information.