[wp-hackers] new WP_Query vs. get_posts() - any difference?

Davit Barbakadze jayarjo at gmail.com
Wed Dec 23 16:10:25 UTC 2009


When I need custom loop with wp tag support I usually do:

<?php if ($posts = get_posts("cat=4,7")): ?>
 <ul>
    <?php foreach ($posts as $post) : setup_postdata($post); ?>
        <li>
           <h1><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title();
?></a></h1>
        </li>
    <?php endforeach; ?>
  </ul>
 <?php endif; ?>

But sometimes I see a different approach:

<?php $query = new WP_Query("cat=4,7");
if ($query->have_posts()) : $query->the_post(); ?>
  <ul>
     <?php while ($query->have_posts()) : the_post(); ?>
      <li>
           <h1><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title();
?></a></h1>
      </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>

and codex uses it to for demonstration of  how the custom loop should be
called. First approach is obviously cleaner, but maybe it has unobvious
drawbacks?

-- 
Davit Barbakadze


More information about the wp-hackers mailing list