Title: shortcode not working with exclude_category function
Last modified: May 3, 2019

---

# shortcode not working with exclude_category function

 *  Resolved [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/)
 * Hi, there. I’m using exclude_category() for the main page. I mean I don’t want
   to show post from “industry” category on the main page
    Unfortunately, in this
   case, short not working when I’m trying to insert id’s from excluded(“industry”)
   category.
 * Any ideas how to fix it? Thank you so much.

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

 *  [darnethil](https://wordpress.org/support/users/darnethil/)
 * (@darnethil)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11498130)
 * Hello, I started but I think I found a solution for you, tests category_id = “-
   52”
    52 is your ID you add the least as in the example.
 * By cons me my problem is that I want to do that with several IDs for several 
   categories but it does not work.
 * How to do?
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11498859)
 * If you would like to exclude multiple categories, it’s best to use a taxonomy
   query ([tutorial](https://displayposts.com/2019/01/07/taxonomy-queries/)).
 * [display-posts taxonomy=”category” tax_term=”category-1, category-2″ tax_operator
   =”NOT IN”]
 *  Thread Starter [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11500177)
 * Yes it’s not working when I’m tryind to show posts per ID’s (not categories ID)
 * Still not working (
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11502421)
 * Can you provide the specific shortcode you’re using that doesn’t work?
 * Thanks
 *  [darnethil](https://wordpress.org/support/users/darnethil/)
 * (@darnethil)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11503100)
 * I succeeded thank you here is my code:[display-posts taxonomy=”category” tax_term
   =”patrice, pierre, ladislas” tax_operator=”NOT IN” category=”LUDOTEK” posts_per_page
   =”200″]
 * [http://soludik.fr/liste-ludotheque/](http://soludik.fr/liste-ludotheque/)
    -  This reply was modified 7 years, 1 month ago by [darnethil](https://wordpress.org/support/users/darnethil/).
 *  Thread Starter [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11503294)
 * Here is an example of my shortcode: `[display-posts id="5982, 7072, 5542, 31"
   image_size="thumbnail" exclude_current="true"]`
 * The code above is showing only the one post (31). It belongs to the category “
   vendors”.
 * All other posts are not showing (5982, 7072, 5542). These posts are from the “
   industry” category that was excluded from the main page with exclude_category()
   function
 * I hope it’ll help to sort it out… Please let me know if you need any other information.
   Thank you
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11507081)
 * Can you share the exclude category function? My guess is it’s excluding those
   posts from all custom queries (like Display Posts) as well.
 *  Thread Starter [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11513402)
 * Sorry for the delay. Sure here is it:
 *     ```
       function exclude_category( $query ) {
           if ( $query->is_home() ) {
               $query->set( 'cat', '-135' );
           }
           if ( $query->is_category ) {
               $queried_object = get_queried_object();
               $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );
   
               if ( ! $query->is_admin )
                   //exclude the posts in child categories
                   $query->set( 'category__not_in', array_merge( $child_cats ) );
           }
   
           return $query;
       }
       ```
   
 *  Thread Starter [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11513431)
 * `$query->set( 'cat', '-135' );`
    This is an id of excluded category. And I want
   to use post ID’s (NOT category id) for shortcode. Thank you
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11513640)
 * Yes, that’s the issue right there. Your exclude_category() function affects every
   query on the site. You need to limit it to the main query using $query->is_main_query().
 * Here’s more information: [https://www.billerickson.net/customize-the-wordpress-query/](https://www.billerickson.net/customize-the-wordpress-query/)
 *  Thread Starter [artkom](https://wordpress.org/support/users/artkom/)
 * (@artkom)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11516685)
 * Yes, this is the solution.
    The post can be closed as resolved.
 * Thank you so much!

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

The topic ‘shortcode not working with exclude_category function’ is closed to new
replies.

 * ![](https://ps.w.org/display-posts-shortcode/assets/icon-256x256.jpg?rev=2940963)
 * [Display Posts - Easy lists, grids, navigation, and more](https://wordpress.org/plugins/display-posts-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/display-posts-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/display-posts-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/display-posts-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/display-posts-shortcode/reviews/)

## Tags

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

 * 11 replies
 * 3 participants
 * Last reply from: [artkom](https://wordpress.org/support/users/artkom/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/shortcode-not-working-with-exclude_category-function/#post-11516685)
 * Status: resolved