• I started using “Enhanced Media Library” plugin to try to put some order in my Media Library, so I created categories and assigned them to images, but soon I came to what it seems to me a great limitation of the plugin: although I can assign multiple categories to an image, I can’t filter images by multiple categories.

    Is there a way to achieve it? Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Edward

    (@edwardhathway)

    I would also like to know this. If not currently possible perhaps it could be included in future updates.

    This would be great for my galleries: A filter with at least two categories.

    Thread Starter cicopitalia

    (@cicopitalia)

    I managed to write a snippet (that I add to WordPress by means of Code Snippets plugin) that filters media library items by multiple (one or more) categories. It is customized to be used with Shortcode Ultimate plugin, in fact the snippet just define a shortcode ([mmj-gallery ...]) that is used to create the actual shortcode string of a ‘Shortcode Ultimate’ image gallery. It might be called a ‘metashorcdode’.

    // needs 'Shortcode Ultimate' plugin
    // default limit: -1 (no limit)
    // default size: 180
    // 'attachment_category' in the example is the category name for attachments created by 'Media Library Assistant' plugin
    // usage examples:
    // [mmj-gallery categoryname='attachment_category' categoryterms='term1,term2,term3']
    // [mmj-gallery limit=20 categoryname='attachment_category' categoryterms='term1,term2,term3']
    // [mmj-gallery size=180 categoryname='attachment_category' categoryterms='BRAU1,Santa Verdiana']
    
    function mmj_gallery_call($atts)
    {
        // Attributes
        $a = shortcode_atts(array(
            'categoryname' => 'not_provided',
            'categoryterms' => 'not_provided',
            'limit' => -1,
            'size' => 180
        ), $atts);
        
        // Output Code
        $tax_query_array = array();
        foreach (explode(',', $a['categoryterms']) as $value) {
            array_push($tax_query_array,array(
                'taxonomy' => $a['categoryname'],
                'field' => 'slug',
                'terms' => array($value)
            ));
        }
        if (count($a)>1) {$tax_query_array["relation"]="AND";}
    	//print_r($tax_query_array);
        $args = array(
            'post_type' => 'attachment',
            'post_status' => 'any',
            'post_mime_type' => array( 'image/jpeg','image/gif','image/png','image/bmp','image/tiff','image/x-icon' ),
            'posts_per_page' => -1,
            'tax_query' => $tax_query_array
        );
        $query_images = new WP_Query($args);
        $id_list = "";
        foreach ($query_images->posts as $image) {
            $id_list .= (($image->ID) . ",");
        }
        return do_shortcode("[su_custom_gallery source=\"media: ".$id_list."\" limit=".$a['limit']." link=\"lightbox\" width=\"".$a['size']."\" height=\"".$a['size']."\"][/su_custom_gallery]");
        //return "[su_custom_gallery source=\"media: ".$id_list."\" limit=".$a['limit']." link=\"lightbox\" width=\"".$a['size']."\" height=\"".$a['size']."\"][/su_custom_gallery]";
    }
    
    add_shortcode('mmj-gallery', 'mmj_gallery_call');
    

    Let me know if it useful for you.
    Possible improvements:
    – add the chance to combine AND and OR clauses
    – automatic management of tranlsated taxonomy with multilingual sites
    – make it a standalone plugin (I don’t have the knowledge nor the time to do it)

    • This reply was modified 6 years, 6 months ago by cicopitalia.
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve language
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: add ul
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: fix typo
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: fix typo
    • This reply was modified 6 years, 6 months ago by cicopitalia. Reason: improve description
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter media library items by multiple categories’ is closed to new replies.