title: Parent categories and child categories posts
body: <p>I have code to show the latest 3 posts of every category (including the child categories) and I want to exclude the posts from these child categories, because these posts are already shown in parent categories.</p>
  
  <p>For example I have a printer category as parent and (printer accessories,.....,.....) as child categories.<br>
  So it shows the latest 3 post of each and I want to exclude any child categories (printer accessories,.....,....) and their posts from being shown.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>$cat_args = array(
    'orderby' =&gt; 'name',
    'order' =&gt; 'ASC',
    'child_of' =&gt; 0
  );
  
  $categories =   get_categories($cat_args); 
  
  foreach($categories as $category) {
      echo '&lt;dl&gt;';
      echo '&lt;dt&gt; &lt;a href="' . get_category_link( $category-&gt;term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category-&gt;name ) . '" ' . '&gt;' . $category-&gt;name.'&lt;/a&gt;&lt;/dt&gt;';
  
       $post_args = array(
        'numberposts' =&gt; 3,
        'category' =&gt; $category-&gt;term_id
      );
  
      $posts = get_posts($post_args);
  
      foreach($posts as $post) {
      ?&gt;
          &lt;dd&gt;
                      &lt;div class="allincat"&gt;
  
                      &lt;div class="catmeta"&gt;                    
                      &lt;span class="authinfo"&gt; 
                      &lt;div class="authimg"&gt;&lt;/div&gt;
                       &lt;?php the_author(); ?&gt; | &lt;?php the_time('jS F Y') ?&gt; &lt;/span&gt;
  
  
                      &lt;/div&gt;
  
                      &lt;div class="allincatimg"&gt;
                      &lt;?php the_post_thumbnail(array(50,50)); ?&gt; &lt;/div&gt;
                      &lt;div class="allincattit"&gt;                    
                      &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
                      &lt;/div&gt;
  
  
  
  
  
                      &lt;/div&gt;
  
                      &lt;/dd&gt;
      &lt;?php
      }
      echo '&lt;div class="view-all"&gt; &lt;a href="' . get_category_link( $category-&gt;term_id ) . '" title="' . sprintf( __( "View all items in %s" ), $category-&gt;name ) . '" ' . '&gt;View all items in ' . $category-&gt;name.'&lt;/a&gt;&lt;/div&gt;';
      echo '&lt;/dl&gt;';
  }
  </code></pre>