Media Library query – jig_media_library_query_args


This is the jig_media_library_query_args filter for developers.

It allows advanced users to influence the query for various sources based on media library. It filters the four Media Library queries in JIG, these are:

  • Normal galleries with a list of IDs.
  • Galleries based on tags, categories, or custom taxonomies, including the ones from Media Library Assistant.
  • Real Media Library galleries.
  • Images from multiple posts at the same time.

A helpful use case would be switching comma-separated terms (tags, categories, custom taxonomies) to the AND operator, meaning all terms must be present on an image for it to be displayed. There is no other way to do that presently (nor shortcuts based on plus signs instead of commas). Currently, media library queries use IN operator (match any of the listed terms). Another use would be showing grids based on custom meta keys such as those created with ACF. The filter uses the query arguments $args, and the shortcode attributes $atts to enable identifying a single grid.

Grids based on custom meta key/value on the images
Let's narrow down to only a JIG with a gallery ID. This assumes you created a grid already in JIG -> Create New Grid, and have an ID at hand. This is used so the code won't affect any grid, just the one you want to modify. Replace the example value with yours.

Then, we insert our additions to the meta_key and meta_value. If you omit the latter you can get images that have any value but they must have the key. Sometimes the key starts with an underscore, other times it doesn't, so try both.

Depending on what you are trying to achieve, consider creating grids based on tags, categories, custom taxonomies, those are usually easier to handle and set up.

Switching tags, categories, and custom taxonomies to AND operator
This assumes you use a shortcode like this in the first place:

[$justified_image_grid image_categories=humans,animals]

And you want to show images that have human and an animal in them, but not a lone wolf.

The code is very similar to how you'd add the AND to the recent posts queries. All taxonomies, both native and custom ones use the tax_query behind the scenes of JIG, for media library content. It doesn't have a default value, so the only thing in it should be your choice to include humans and animals. By inserting the operator in index 0, we can be sure that we are connecting humans and animals using an AND word. The index changes from 0 to 1 or 2 if you utilize more than one taxonomy at the same time. You could use tags, categories and a custom taxonomy, all three at the same time. The more you use, the less images you'll see in the grid, and between these there is an AND relation by default, which is meaningful in most cases. However, on their individual levels of comma separated terms, you can further narrow the query by using this method.

This FAQ entry was posted in Developer hooks on December 20, 2020