Title: osmir's Replies | WordPress.org

---

# osmir

  [  ](https://wordpress.org/support/users/osmir/)

 *   [Profile](https://wordpress.org/support/users/osmir/)
 *   [Topics Started](https://wordpress.org/support/users/osmir/topics/)
 *   [Replies Created](https://wordpress.org/support/users/osmir/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/osmir/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/osmir/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/osmir/engagements/)
 *   [Favorites](https://wordpress.org/support/users/osmir/favorites/)

 Search replies:

## Forum Replies Created

Viewing 1 replies (of 1 total)

 *   Forum: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
   
   In reply to: [Get Parent Category Posts, Not Sub-Categories Posts Unless Belong to Parent Also](https://wordpress.org/support/topic/get-parent-category-posts-not-sub-categories-posts-unless-belong-to-parent-also/)
 *  [osmir](https://wordpress.org/support/users/osmir/)
 * (@osmir)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/get-parent-category-posts-not-sub-categories-posts-unless-belong-to-parent-also/#post-1852068)
 * I got my solution of the topic.
    Main idea is to filter out posts placed in subcategories.
   So we can use negative values in **_cat_** variable of the **_query\_posts()_**.
 * My steps:
    1. Determinig list of subcategories for current category and merging them together
    2.     ```
           $subcategories = get_categories(array('type' => 'post', 'child_of' => $_GET['cat']));
       
           foreach ($subcategories as $i => $value) {
               $excluded_cats .= ",-".$value->cat_ID;
           }
           ```
       
    3. You’ve got something like **,-10,-5,-3** Note leading comma.
    4. Generating **_cat_** variable
    5.     ```
           if (!empty($excluded_cats)) {
              $MainLoopArgs = array('cat' => $_GET['cat'] . $excluded_cats);
           }
           else {
              $MainLoopArgs = array('cat' => $_GET['cat']);
           }
           ```
       
    6. I include current category and exclude subcategories in one **_cat_**. Result
       is something like **4,-10,-5,-3**.
    7. Passing **_$MainLoopArgs_** into **_query\_posts()_**
        `query_posts( $MainLoopArgs);`
 * Sorry, maybe my example not so graceful, but it works! 🙂
 * _**P.S.** Yes, you have to make (m)any validation checks of variables for realy
   robust app._

Viewing 1 replies (of 1 total)