Working on a site that has a lot of posts, I need to display 3 posts from a particular category, but all of them need to be from the latest 10 published on the site. I can either grab 3 completely random posts (which tends to pull posts that are very old) or grab 10 posts (but I don't know how to then randomize the order and only display 3).
So far, I have this query:
$args = array( 'post_type' => 'post', 'category_name' => 'mycategory', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC', 'meta_key' => '_thumbnail_id', 'no_found_rows' => 'true' ); $query = new WP_Query( $args );
along with this attempt to get 3 random posts from the 10 queried:
$randomPosts = shuffle( $query ); $randomPosts = array_slice( $randomPosts, 0, 3 );
But treating the results as an array doesn't work, since it's actually an object.
My only other thought is to use
'posts_per_page' = 3 with
'orderby' => 'rand' to grab 3 random posts and add a
'date_query' to restrict it to the past 6 months. That would be close, but it would be preferable to restrict the query to the 10 most recent posts (they may all be published 3 days ago or 5 months ago, they are published together in uneven spurts).
What is the best approach?
Query the 10 latest posts as I'm doing, then convert the object to an array, shuffle, and slice, and convert it back to an object, or is there a simpler, more efficient way to accomplish the goal?