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

Austin Matzko austin at ilfilosofo.com
Wed Dec 23 16:27:12 UTC 2009


On Wed, Dec 23, 2009 at 10:10 AM, Davit Barbakadze <jayarjo at gmail.com> wrote:
> When I need custom loop with wp tag support I usually do:
>
> <?php if ($posts = get_posts("cat=4,7")): ?>

[snip]

> But sometimes I see a different approach:
>
> <?php $query = new WP_Query("cat=4,7");

[snip]

> 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?

Most of the time those two approaches will have the same result.
However, you should keep in mind that get_posts() by default sets its
"suppress_filters" argument to true, so if a plugin tries to customize
results via the WP_Query filters, it will be able to change the
results in your second, WP_Query example but not the first,
get_posts() example.  Since most themes use WP_Query or query_posts,
most plugin authors have a reasonable expectation that they'll be able
to use the WP_Query filters to affect theme output, and any themes you
make with default get_posts() will defy those expectations.

Also, since get_posts() instantiates WP_Query() itself, the second
route is more direct.


More information about the wp-hackers mailing list