It's quite some time since I worked with WordPress, but it seems to me that:
You're executing the query twice.
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.
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().
- You should either put everything in
- add the
orderby parameter too to the