Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Under what circumstances would you, (should you?) use a foreach loop, instead of the wordpress loop.

I have no real coding need, but I'm curious. What's good practice here?

Should I:

    'post_parent' => $pos_id,

  foreach($sublabels as $sublabel) { // blabla something to do foreach}


    'post_parent' => $pos_id,
'cat' => 'label',


 while (have_posts()) : the_post() ?> // and then the blabla something for each

And why?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Your examples are showing two different thing The first one is looping over categories and the second is looping over posts, so the answer in this case is simple and depends on what you need to loop over (posts or categories).

No if you meant when should you use query_posts() vs. get_posts() vs. WP_Query then take a look at this awesome answer .

share|improve this answer
Ahhhaa. I was going to edit it and ask again, but actually it is essentially a question of 'get_posts' vs query_posts. Ha. Oops. Sorry for the reask then :) And thanks for the swift answer and link! – josh Dec 8 '11 at 17:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.