Title: Multiple Categories with &#039;AND&#039; relation
Last modified: August 24, 2016

---

# Multiple Categories with 'AND' relation

 *  Resolved [ryleef](https://wordpress.org/support/users/ryleef/)
 * (@ryleef)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/)
 * I am currently having problems trying to display only files of multiple category
   types. I am using the Enhanced Media Library plugin which sets files taxonomy
   as a media_category. Below is the code I am having problems with:
 * `[dg descriptions=true fancy=true media_category=form media_category=contractor
   relation=AND id=-1]`
 * The above code displays ALL files that have `media_category` of `contractor` 
   regardless whether they also have the `form` `media_category` as well. I have
   also noticed that swapping them around (`contractor` in the first `media_category`,`
   form` in the second) shows all `form`s regardless if they have a `media_category`
   of `contractor`.
 * Any help would be appreciated.
 * Cheers
 * [https://wordpress.org/plugins/document-gallery/](https://wordpress.org/plugins/document-gallery/)

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

 *  Thread Starter [ryleef](https://wordpress.org/support/users/ryleef/)
 * (@ryleef)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/#post-6178394)
 * NOTE: I have also tested this with comma separated values:
 * `[dg descriptions=true fancy=true media_category=form,contractor relation=AND
   id=-1]`
 * This doesnt work and displays all files.
 * I have also tested with quotes surrounding values (`media_category="form"`)
 *  Thread Starter [ryleef](https://wordpress.org/support/users/ryleef/)
 * (@ryleef)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/#post-6178397)
 * From a quick debug, it seems like it ignores the first value (`form` in this 
   case) and only applies the second (`contractor`)
 *  Thread Starter [ryleef](https://wordpress.org/support/users/ryleef/)
 * (@ryleef)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/#post-6178404)
 * Was able to “fix” this with the addition to the following code in `class-gallery.
   php`
 * ***NEW***
 *     ```
       foreach ($this->taxa as $taxon => $terms) {
                   $terms = $this->getTermIdsByNames($taxon, explode(',', $terms));
                   foreach($terms as $term){
                       $taxa[] = array(
                             'taxonomy' => $taxon,
                             'field'    => 'id',
                             'terms'    => $term,
                             'operator' => isset($operator[$taxon]) ? $operator[$taxon] : 'IN'
                       );
                   }
   
                }
       ```
   
 * ***OLD***
 *     ```
       foreach ($this->taxa as $taxon => $terms) {
                   $terms = $this->getTermIdsByNames($taxon, explode(',', $terms));
                       $taxa[] = array(
                             'taxonomy' => $taxon,
                             'field'    => 'id',
                             'terms'    => $terms,
                             'operator' => isset($operator[$taxon]) ? $operator[$taxon] : 'IN'
                       );
   
                }
       ```
   
 * Created a separate array for each term so that the ‘AND’ relation worked as expected.
 * If there is a “proper” way around this please comment below.
 *  Plugin Author [Dan Rossiter](https://wordpress.org/support/users/danrossiter/)
 * (@danrossiter)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/#post-6178493)
 * Hi ryleef,
 * Please take a look at the NOTE following the relation option documentation on
   the installation tab.
 * I believe you’re looking for “media_category_relation”.
 * -Dan

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

The topic ‘Multiple Categories with 'AND' relation’ is closed to new replies.

 * ![](https://ps.w.org/document-gallery/assets/icon.svg?rev=983329)
 * [Document Gallery](https://wordpress.org/plugins/document-gallery/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/document-gallery/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/document-gallery/)
 * [Active Topics](https://wordpress.org/support/plugin/document-gallery/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/document-gallery/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/document-gallery/reviews/)

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [multiple](https://wordpress.org/support/topic-tag/multiple/)
 * [multiple categories](https://wordpress.org/support/topic-tag/multiple-categories/)

 * 4 replies
 * 2 participants
 * Last reply from: [Dan Rossiter](https://wordpress.org/support/users/danrossiter/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/multiple-categories-with-and-relation/#post-6178493)
 * Status: resolved