Recent posts query – jig_recent_posts_query_args

­

This is the jig_recent_posts_query_args filter for developers.

It allows advanced users to influence the recent posts query. For example, you could show grids based on custom post meta key and value pairs, or remove the default "only in stock" WooCommerce behavior, add an AND relationship between your custom taxonomy terms, etc. It uses the query arguments $args, and the shortcode attributes $atts to enable identifying a single grid.

Grids based on custom post meta key/value
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_query and we are making an assumption it exists. It often does, if you are not using a placeholder image for recent posts (it's a setting), and/or when using WooCommerce to show only in-stock items (more on that below). If it doesn't exist, just remove the [] from it and create an array of arrays (two opening and closing squery brackets instead of one, around the key value pair). Replace key value pair's example strings with your own.

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.

Showing WooCommerce products that are out of stock
Since JIG includes a meta query for _stock_status being equal to instock, it can only show products that are in stock by default. This is fine for most people as it's the expected behavior. However, by overriding the meta query with just the other usual option that restricts the grid to products with photos, the stock status "filter" can be eliminated.

Using an AND relationship operator with your custom taxonomy terms

This assumes you use a shortcode like this in the first place:

[$justified_image_grid recent_posts=yes recents_filter_tax=animals recents_filter_term=cat,dog]

And you want to show posts that are about a cat and a dog, but not those that only have one of these animals.

We are no longer bothering the meta_query as previously seen, rather this is the tax_query. It doesn't have a default value, so the only thing in it should be your choice to include cats and dogs. By inserting the operator in index 0, we can be sure that we are connecting cats with dogs with an AND word, behind the scenes.

Please note that if you are using the native category or tag taxonomies, you don't need the custom code! You can just use a + (plus sign) between them instead of the commas, and JIG will handle the rest and pass them on to WP Query in the appropriate way. This is only for the Recent posts feature, and not for normal media library image queries.

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