title: Limit foreach loop with condition
body: <p>this question already asked many times but I am unable to find a solution for my problem. My php knowledge is very limited.</p>
  
  <p>I am building a wordpress website where there a custom taxonomy called "series". This website will have many series and I am using a custom field for grouping series. So it will be easy to organize series on frontend in groups.</p>
  
  <p>So here the code for showing 'series' (<a href="https://pastiebin.com/5b445ae944a13" rel="nofollow noreferrer">https://pastiebin.com/5b445ae944a13</a>):</p>
  
  <pre><code>&lt;?php $x = 0; foreach ( $new_mod_series as $cat ): ?&gt;
  
     &lt;?php $seriestype = get_field( "series_type", $cat ); ?&gt;
  
     &lt;?php while ( $seriestype == "interview" and $cat_post-&gt;have_posts() ): $cat_post-&gt;the_post(); ?&gt;
  
        Show interview series...
  
     &lt;?php endwhile; ?&gt;
  
  &lt;?php if (++$x == 5) break; endforeach; ?&gt;
  </code></pre>
  
  <p>This code <code>&lt;?php $seriestype = get_field( "series_type", $cat ); ?&gt;</code> don't run outside of foreach loop and with this I can get value of 'series type' custom field. Then I am using <code>$seriestype == "interview"</code> in while to show only series which are interview.</p>
  
  <p>And now I am not getting expected or 5 foreach results because limiting only 'interview' series to show. But I need to show 5 recent interview series in a section, then other 5 series of another series section.</p>
  
  <p>Is there anyway to solve my problem? With my limited php knowledge, I tried a lot but could not found any solution. Hopefully I was able to explain correctly.</p>
  
  <p>Thanks
  Julash</p>