I’m using WP from an external PHP script by including the wp-load.php
file.
All functions and everything works as expected so far, except one thing: I can’t get the $wp_query->found_posts
to work after I fetch posts with get_posts()
function.
Any hints what should I be using instead?
Thanks!
The WordPress function
get_posts()
is making it’s own instance ofWP_Query
that is not globally accessible:so you could instead try
to give you the array count of post objects returned by
get_posts()
.WP_Query() class usage example:
You could consider using the
WP_Query()
class directly.Here is an example how you can use it:
We use
wp_reset_postdata()
in the end, to restore the global$post
object, since we change it viathe_post()
method.Reference:
http://codex.wordpress.org/Function_Reference/wp_reset_postdata
http://codex.wordpress.org/Class_Reference/WP_Query