• Resolved alx359

    (@alx359)


    The attachment_category= param isn’t working for me in the way I would expect after reading the documentation.

    a) I’ve the following subtree defined in Att. Category:

    
    jewel-boxes [0 items]
    - long-box [36 items]
    - square-box [74 items]
    

    [mla_gallery attachment_category="jewel-boxes"] doesn’t produce any output, even if tax_include_children=true is implicitly set by default, according documentation.
    [mla_gallery attachment_category="long-box"] and [mla_gallery attachment_category="square-box"] output just fine.

    b) However, when attaching the children’s media also to the parent, i.e.:

    
    jewel-boxes [110 items]
    - long-box [36 items]
    - square-box [74 items]
    

    [mla_gallery attachment_category="jewel-boxes" tax_include_children=false] does produce the desired output, while [mla_gallery attachment_category="jewel-boxes" tax_include_children=true] doesn’t output anything, same like case a).

    IMO, one would expect tax_include_children=true parameter to implicitly include children media and render their output, regardless of jewel-boxes being empty, or not. Isn’t that a correct assumption? Also, when jewel-boxes is fully populated, it should display output regardless of the tax_include_children setting. Is it there any other rationale I’m not grasping for the way it is now?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter alx359

    (@alx359)

    I omitted an extra parameter and it simplified the example too much. The full problematic syntax should look like this:

    [mla_gallery attachment_category="jewelboxes,c-indian-east" tax_operator=AND tax_include_children=true]

    These don’t work:
    – “jewelboxes” has no attached media, but its children do (longbox, squarebox), it still returns nothing.
    – “jewelboxes” has all the media of its children attached, but also has the tax_include_children=true parameter, it still returns nothing because of this.

    The only way to make it work as expected is attaching media to the parent and excluding its children:

    [mla_gallery attachment_category="jewelboxes,c-indian-east" tax_operator=AND tax_include_children=false]

    Plugin Author David Lingren

    (@dglingren)

    Thanks for giving me the complete shortcode; the tax_operator=AND parameter is the source of your problem. When you use this setting items must be assigned to ALL of the terms you specify. Since you also coded tax_include_children=true, that means that items must be assigned to all four terms:

    jewel-boxes AND long-box AND square-box AND c-indian-east

    Do you see the problem?

    What do you really want? I am guessing you want something like:

    ( jewel-boxes OR long-box OR square-box ) AND c-indian-east

    Is that right? If so, you can use the more advanced “tax_query” parameter:

    [mla_gallery]
    tax_query="array(
        'relation' => 'AND',
        array(
            'taxonomy'=>'attachment_category',
            'field'=>'slug',
            'terms'=> array( 'jewel-boxes ' ),
            'operator' => 'IN',
            'include_children' => true
            ),
        array(
            'taxonomy'=>'attachment_category',
            'field'=>'slug',
            'terms'=> array( ' c-indian-east ' ),
            )
        )"
    [/mla_gallery]
    

    Note that I am using the alternative “enclosing shortcode” syntax to avoid problems WordPress has parsing complex parameters.

    Give the above suggestion a try and let me know how it works for you.

    Thread Starter alx359

    (@alx359)

    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;
    }
    
    
    Thread Starter alx359

    (@alx359)

    Thanks to a chat with David, and his kind and skillful demeanor, I was able to understand where the problem with my code was. Essentially, $tax_query always has to be formatted as a string statement, so $tax_query = array(...) should become $tax_query = "array(...)" instead, and everything inside that statement should also parse to a an escaped string, so array( $term ) should become "array('". $term ."')" or we should ensure that the expanded $term already comes escaped with single quotes.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the kind words and for confirming that the fix I gave you offline has solved the catastrophic problem you encountered. The error messages you posted contained a good clue: “expects parameter 1 to be string, array given”.

    Your original ew_tax_query( $term ) function was returning an array, not a string, containing the tax_query value.

    WordPress parses all shortcode parameters into separate string values and puts them in an array indexed by the parameter name. Any parameter you compose in a handler for one of MLA’s filters must have a string value as well; MLA’s parameter processing code does not examine the PHP type of the element values.

    As you’ve discovered, PHP coding and using MLA’s hooks is a power tool that can have sharp edges. I’m happy that this problem was easily solved.

    I am marking this topic resolved, but please update it if you have any other problems or further questions about using the tax_query parameter with [mla_gallery].

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘[attachment_category] param behavior’ is closed to new replies.