Media Library Assistant
[resolved] Can't filter by category (6 posts)

  1. publicradio
    Posted 10 months ago #

    Hello, the documentation for this plugin says I can use 'category=xxx' and 'post_tag=xxx' to filter galleries using WordPress' built-in functionality. However, I find that neither of these actually work. 'tag=xxx', however, does filter by tag. So, what is the correct way to filter by category?


  2. David Lingren
    Plugin Author

    Posted 10 months ago #

    Thanks for your interest in the plugin and for this question. The "Category" and "Tag" filtering is provided by the WordPress WP_Query class, and the syntax is a bit confusing. You can find the details in the Codex:

    WP_Query Class Reference

    Category, in particular, is not what you'd expect. Here are the parameters you can use:

    1. cat (int) - use category id.
    2. category_name (string) - use category slug (NOT name).
    3. category__and (array) - use category id.
    4. category__in (array) - use category id.
    5. category__not_in (array) - use category id.

    There are several examples in the Codex article.

    This question has come up before, and I am planning to add more material to the Settings/Media Library Assistant Documentation tab to better address it. I am also considering adding a "category" parameter as a synonym for "category_name" just to make things easier.

    I am marking this topic resolved, but please update it if you have any problems or further questions about filtering by taxonomies like Category and Tag. Thanks for adding your voice to the mix and motivating some MLA improvements.

  3. publicradio
    Posted 10 months ago #

    Thanks, David, but I am still having problems.

    For my tag archives, I am using

    [mla_gallery tag="'. $tag .'"]

    And this works fine. However, for category, I am using:

    echo do_shortcode('[mla_gallery category="'. $category_name .'"]');

    And this doesn't filter at all. I have tried echoing $category_name and I know it's returning the slug, so the shortcode is definitely sending:

    [mla_gallery category="nature"]

    For example, yet the result is to get all images in any category. I have also changed the category to be a number, the ID of the category, but this also doesn't work.

  4. publicradio
    Posted 10 months ago #

    OK, I figured this out. I didn't understand what you were saying. I know you explained it in another thread, and I didn't understand it there, either. So I'll post the solution here, in case anyone else has the same problem.

    OK, so: if you are in the category archive (category.php), $cat and $category_name are built-in. you can do <? echo $cat; ?> and get the ID of the category you're on. $category_name returns the slug.

    So, if you want a gallery of all your images in a given category, on the category archives, you just query the category and define it, like so:

    <? echo do_shortcode('[mla_gallery category_name="'. $category_name .'"]')?>

    You can also do:

    <? echo do_shortcode('[mla_gallery cat="'. $cat .'"]')?>

    Whichever you use, make sure it's symmetrical and you use the same parameter twice.

    Thanks for the help!

  5. David Lingren
    Plugin Author

    Posted 10 months ago #

    You've got it; thanks for posting the explanation.

    The next MLA version will accept "category" as a synonym for "category_name", because this is a common and entirely understandable thing to try.

  6. David Lingren
    Plugin Author

    Posted 9 months ago #

    I have released MLA v1.90, which adds category as a synonym for category_name. I hope that will avoid some of the confusion you and others have had in the past. Thanks for motivating an enhancement to the plugin.


You must log in to post.

About this Plugin

  • Media Library Assistant
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic