title: Display Posts Category Wise
body: <p>I have code to show the latest 3 posts of every category and I want to exclude the child categories, but it doesn't exclude the sub category and it shows sub category separately and it show the latest 3 posts of sub category. Is there any way to exclude sub categories from loop.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;?php
  //start page loop
  if (have_posts()) : while (have_posts()) : the_post();
  
      //get meta to set parent category
      $library_filter_parent = '';
      $library_parent = get_post_meta($post-&gt;ID, 'wts_library_parent', true);
      if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; }
      ?&gt;
  
      &lt;div id="library-by-category-wrap"&gt;
  
          &lt;?php
          //get meta to set parent category
          $library_filter_parent = '';
          $library_parent = get_post_meta($post-&gt;ID, 'wts_library_parent', true);
          if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; };
  
          //term loop
          $terms = get_terms('library_cats','orderby=custom_sort&amp;hide_empty=1&amp;child_of='.$library_filter_parent.'');
          foreach($terms as $term) { ?&gt;
  
              &lt;div class="heading"&gt;
                  &lt;h2&gt;&lt;?php echo $term-&gt;name; ?&gt;&lt;/h2&gt;
              &lt;/div&gt;
  
              &lt;div class="library-category"&gt;
  
                  &lt;?php
                  //tax query
                  $tax_query = array(
                      array(
                          'taxonomy' =&gt; 'library_cats',
                          'terms' =&gt; $term-&gt;slug,
                          'field' =&gt; 'slug'
                      )
                  );
                  $term_post_args = array(
                      'post_type' =&gt; 'library',
                      'numberposts' =&gt; '3',
                      'tax_query' =&gt; $tax_query
                  );
                  $term_posts = get_posts($term_post_args);
  
                  //start loop
                  foreach ($term_posts as $post) : setup_postdata($post);
  
                      //get images
                      $featured_image = get_the_post_thumbnail($post-&gt;ID, 'cat-thumbnail'); ?&gt;
                        &lt;?php if(!empty($featured_image)) { ?&gt;
                            &lt;div class="library-item"&gt;
                              &lt;a class="library-title" href="#" title="&lt;?php the_title(); ?&gt;" target="_blank"&gt;
                                  &lt;h3&gt;&lt;?php the_title(); ?&gt;&lt;/h3&gt;
                              &lt;/a&gt;
                            &lt;/div&gt;
                            &lt;!-- /library-item --&gt;
                        &lt;?php } ?&gt;
  
                  &lt;?php endforeach; ?&gt;
              &lt;/div&gt;
              &lt;!-- /library-category --&gt;
  
          &lt;?php } wp_reset_postdata(); ?&gt;
  
      &lt;/div&gt;
      &lt;!-- /library-by-category-wrap --&gt;
  
      &lt;?php wp_reset_query(); ?&gt;
  
  &lt;?php endwhile; endif; ?&gt;
  </code></pre>