It’s quite some time since I worked with WordPress, but it seems to me that:
You’re executing the query twice.
-
First when you pass
$argsto the constructor during instantiation. -
Second when you call
query().
With this, you’re essentially overwriting the first query (the one that contains the orderby=rand).
The documentation of WP_Query mentions that get_posts() is called if you use a parameter in the constructor, and it shouldn’t be called twice:
&get_posts() – Fetch and return the requested posts from the database.
Also populate $posts and $post_count. Note: This is called during
construction if WP_Query is constructed with arguments. It is not
idempotent and should not be called more than once on the same query
object. Doing so may result in a broken query.
And the query() method’s documentation states that it calls get_posts(), therefore it’s called twice:
&query( $query ) – Call parse_query() and get_posts(). Return the
results of get_posts().
The solution:
- You should either put everything in
$args, or - add the
orderbyparameter too to the$wp_query->query(..)call.