Thank you very much for the advanced query example usage! It was eye-opening, and helped improve my code to generalize the support for term_group and other tweaks.
However, a quite unexpected issue happened.
As I’ve multiple paginations in a single page, I’ve switched to the mla_gallery_raw_attributes hook instead of mla_gallery_attributes, as mentioned in this post. The “raw” hook and tax_query together though make the situation “catastrophic”, hanging the server and filling error_log with hundred of thousands of repeating PHP notices about these 4 lines:
[20-Aug-2017 06:01:50 UTC] PHP Warning: strpos() expects parameter 1 to be string, array given in D:\domains\wp.mydomain.com\wp-content\plugins\media-library-assistant\includes\class-mla-data.php on line 1254
[20-Aug-2017 06:01:50 UTC] PHP Warning: strpos() expects parameter 1 to be string, array given in D:\domains\wp.mydomain.com\wp-content\plugins\media-library-assistant\includes\class-mla-data.php on line 1258
[20-Aug-2017 06:01:50 UTC] PHP Warning: strpos() expects parameter 1 to be string, array given in D:\domains\wp.mydomain.com\wp-content\plugins\media-library-assistant\includes\class-mla-data.php on line 1265
[20-Aug-2017 06:01:50 UTC] PHP Warning: substr() expects parameter 1 to be string, array given in D:\domains\wp.mydomain.com\wp-content\plugins\media-library-assistant\includes\class-mla-data.php on line 1280
Using the mla_gallery_attributes hook instead things go back to normal, and tax_query seems working, but pagination with a custom mla_page_parameter doesn’t paginate (i.e. repeats the first page over and over — the reason I switched to the ‘raw’ version of the hook that fixed this).
Here’s my code example:
/**
* Manage custom MLA syntax to display galleries in lightbox pop-ups
* https://wordpress.org/support/topic/group-of-wc-product/
* Example Usage:
* [mla_gallery attachment_category=a3-print my_term_group=true]
*/
add_filter( 'mla_gallery_attributes', 'ew_gallery_attributes', 10, 1 );
function ew_gallery_attributes( $attr ) {
if( $attr['my_term_group'] === 'true' && $attr['attachment_category'] ) {
// Build a complex query: attachment_category term AND term_group terms associated with product
$attr['tax_query'] = ew_tax_query( $attr['attachment_category'] );
// Differentiate between multiple paginations
$attr['mla_page_parameter'] = 'mla_paginate_'.$attr['attachment_category'];
// attributes not needed anymore should get disposed
unset( $attr['attachment_category'] );
unset( $attr['my_term_group'] );
}
return $attr;
}
/**
* Query Builder. Properly handle search in attachment_category hierarchies
* Example: ( jewel-boxes OR long-box OR square-box ) AND (c-indian-east OR c-mermaids)
* https://wordpress.org/support/topic/attachment_category-param-behavior/#post-9423956
*/
function ew_tax_query( $term ) {
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'attachment_category',
'field' => 'slug',
'terms' => array( $term ),
'operator' => 'IN',
'include_children' => true
),
array(
'taxonomy' => 'attachment_category',
'field' => 'slug',
'terms' => array('c-indian-east','c-mermaids'),
'operator' => 'IN',
'include_children' => false
)
);
return $tax_query;
}