Here’s my approach…
First you have to select 3 latest posts, then you have to pick random one of them…
But it’s easier to shuffle selected posts than picking only one of them – that way you can still use normal loop:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish'
);
$rand_query = new WP_Query( $args );
shuffle( $rand_query->posts );
if ( $rand_query->have_posts() ) :
while ( $rand_query->have_posts() ) : $rand_query->the_post();
?>
// HERE GOES THE DIV WITH POST
<?php
break; // we want only one post to be shown, so we break the loop
endwhile;
endif;
?>
Related Posts:
- Query Posts in a Predefined Order
- Order posts by ID in the given order
- WP_Query ordered by custom field that is a date string?
- Show all parents and children in custom post type in right order
- How add a custom posttype name using ACF field to a query post array
- (Solved) WP_Query ($ args) -> How to sort letters and numbers within the same array
- Weird orderby => post__in issue
- Query all posts if multiple meta key don’t exist
- Include current post into loop
- How can I comment comma-separated array values?
- How do I add a relation parameter to my filter query?
- WP_User_Query Orderby Not Working
- Order by meta date with a thousanth of a second defaults to post order
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- query_posts with sorting on a custom datestamp
- new WP_Query with order args – no more distinction between categories
- Orderby meta _value that is saved as an array
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- Get post ids from WP_Query?
- Nested meta_query with multiple relation keys
- Some doubts about how the main query and the custom query works in this custom theme?
- Wp get all the sub pages of the parent using wp query
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How to query for most viewed posts and show top 5
- WP_Query orderby date not working
- WP Query where title begins with a specific letter
- WP_Query vs get_posts
- Order by optional meta key?
- Sorting: custom query with orderby meta_value_num THEN by title
- Order by DESC, ASC in custom WP_Query
- WP_Query order by multiple meta keys & fields
- Order by & include array by specific post ids
- Using OR conditions in meta_query for query_posts argument
- Secondary Sort (fallback) for WP_Query
- How to get order of posts?
- Custom WP_Query order by post_meta and (author) user_meta
- Sorting posts by custom date fields (non standard date format)
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Query Multiple Post types each with own meta query
- Get posts by menu ID
- What kind of object type is WP_Query?
- Get the number of posts from the current page results
- How can I save an array from a random post sequence for later use?
- WordPress Custom Query to show posts from last x years
- How to order posts tag by tag?
- Query Custom Meta Value with Increment
- How do I order pages and categories by ID or name in the same query?
- Getting an array out of WPQuery
- Get All IDs Of A Post Type Using WP_Query
- How to know which one is the main query?
- Get posts by meta data OR title
- Perform query with meta_value date
- Using is_main_query to select custom post type on certain page
- get query’s query string
- Meta Query with date and time on the same Day before given time
- get_the_title($postID) OR get_the_title()?
- WP_Query sort by comment meta data
- Display Posts by modifying the where clause only for my query
- Order posts by tags count?
- Merge 2 args in one WP_Query and order it by date
- assign 2 $args to one wp_query
- Orderby = none not working [duplicate]
- Use Transient API to cache queries for all posts in all categories?
- How do I create my own nested meta_query using posts_where / posts_join?
- WP_query ‘orderby=none’ Problem
- How to use filter hook posts_join for querying taxonomy terms in posts_where?
- Order posts by date and then by custom field
- Best practice for multiple queries on page
- How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
- Revolution Slider Orderby Two Custom Fields
- How to Get All Posts but the Private ones?
- Order posts ascending with number in title
- List taxonomy terms plus their latest post ordered by post date
- WP Meta Query for some meta (array) values
- Order by the results of a function with WP_query
- meta_query with array as value
- How to order posts in wp_query by a meta_value of the corresponding author
- Placing a div or img in between a post array using WP Query [closed]
- How to query ‘posts_per_page’ to display a different blog posts index template?
- Show one post per author and limit query to 8 posts
- How to find out what “Blog pages show at most” is set to [duplicate]
- Order by menu structure
- Multiple search queries on one page
- How to show only one post for each categories of taxonomy of custom post that contains a specific custom field
- Pass the same object to multiple widgets in a template with one query
- WP_Query order by date in meta_value
- Retrieve or Query Pages by ID
- How to use order RAND() on WordPress?
- Ordering by meta_value AND date NOT WORKING with wp_query
- WP query exclude post within the last month / only show over 1 month old
- WordPress query for most recent posts from multiple categories
- WP_Query orderby breaks when using AJAX?
- How do you query wordpress posts using a math formula between multiple meta field values?
- Order WP Query posts by custom order calculated from post meta values
- Custom Post order for homepage
- wp_query for displaying attachments with a tag
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Too many posts cause slow load when paginating