Title: Nested Loop Get_posts
Last modified: August 30, 2016

---

# Nested Loop Get_posts

 *  Resolved [Jacks89](https://wordpress.org/support/users/jacks89/)
 * (@jacks89)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/nested-loop-get_posts/)
 * Hi I need help with nested loop implementation.
 * Here is my current code.
 *     ```
       <?php
   
       $cat_args=array(
         'orderby' => 'name',
         'order' => 'ASC',
         'exclude' => 11,13,22
          );
   
         $categories=get_categories($cat_args);
         foreach($categories as $category) {
           $args=array(
             's' => $s,
             'showposts' => 5,
             'category__in' => array($category->term_id),
             'caller_get_posts'=>1,
           );
   
           $count_args = array(
             's' => $s,
             'showposts' => -1,
             'category__in' => array($category->term_id),
             'caller_get_posts' => 1,
             'exclude' => 11,13,22
           );
           $count_posts=get_posts($count_args);
   
           $posts=get_posts($args);
           if (count($count_posts)!=0){
           ?>
            <section class="<?php echo $category->name ?>-section-segment">
           <?php
           if ($posts!=NULL) {
             echo '<p><strong>'.count($count_posts).'</strong> '<span class="capitalize-letter">'.$s.'</span>' found in  <a href="'.get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></p> ';
             foreach($posts as $post) {
   
               setup_postdata($post); ?>
               <article class="post" id="post-<?php the_ID(); ?>">
                 <div class="title">
                   <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
                 </div>
                 <div class="entry each-category-items">
                   <div class="three columns alpha">
                     <?php if ( has_post_thumbnail() ) { ?>
                      <a href="<?php the_permalink() ?>"><?php the_post_thumbnail();?></a>
                     <?php } else { ?>
                       <a href="<?php the_permalink() ?>"><img class="scale-with-grid" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" /></a>
                     <?php } ?>
                   </div>
                   <div class="eight columns omega">
                     <p><?php echo substr(get_the_excerpt(), 0,150); ?></p>
                     <?php /* <a class="read-link" href="<?php the_permalink();?>">Read More</a>  */ ?>
                   </div>
                   <div class="clear"></div>
                 </div>
                 <div class="postmeta">
                   <?php /* <span><i class="icon-user"></i>Posted by <?php the_author(); ?> </span> */ ?>
                   <span><i class="icon-calendar"></i>Posted on <?php the_time('F j Y'); ?></span>
                   <span class="right"><i class="icon-link "></i><a href="<?php the_permalink();?>">Permalink</a></span>
                 </div>
               </article>
   
             <?php
             } // foreach($posts
           } // if ($posts
            } ?>
           </section>
           <?php
         } // foreach($categories
       ?>
       ```
   
 * Here is my intention:
    Loop each category from the search keyword{ Loop maximum
   5 articles per category{ Loop the articles that has “Featured Articles” in category
   Loop the articles that has no “Featured Articles” in category } }
 * Any advice?

Viewing 1 replies (of 1 total)

 *  Thread Starter [Jacks89](https://wordpress.org/support/users/jacks89/)
 * (@jacks89)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/nested-loop-get_posts/#post-6385108)
 * Nevermind.
 * I found a round about of doing it.
 * Run 2 loop:
    1st loop is check if in_category of featured articles 2nd loop is
   check if ! in_category of featured articles

Viewing 1 replies (of 1 total)

The topic ‘Nested Loop Get_posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Jacks89](https://wordpress.org/support/users/jacks89/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/nested-loop-get_posts/#post-6385108)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
