[wp-hackers] Testing for existence of WP_Query results

Ade Walker photofantaisie at gmail.com
Thu Aug 13 18:39:28 UTC 2009


Hi all,

This probably exposes my lack of PHP knowledge, and understanding of the
WP_Query object, but I came across the following situation with a plugin of
mine recently.

In order to test that a WP_Query had actually found posts, I used this
conditional check

$recent = new WP_Query(args);
if($recent) :
while($recent->have_posts()) : $recent->the_post();
// rest of loop etc

else :
// do something else

But I found that if($recent) nevers returns false, even if no posts are
found.

Is there a different way to test (before the while loop runs) that any posts
were found by the query?

I'm probably overlooking something blindingly obvious - but I just can't see
it... :-/

Thanks!

Ade.


More information about the wp-hackers mailing list