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
- When to use WP_query(), query_posts() and pre_get_posts
- Order by optional meta key?
- WP_Query order by multiple meta keys & fields
- How to get order of posts?
- Get All IDs Of A Post Type Using WP_Query
- How to know which one is the main query?
- get query’s query string
- assign 2 $args to one wp_query
- Revolution Slider Orderby Two Custom Fields
- Show one post per author and limit query to 8 posts
- 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
- Next/Prev posts on same page
- How to query a custom post type with a taxonomy filter but display post type archive page?
- WP_Query last five posts, simply ordered by meta_value
- WP_Query “OR”: Find posts by ID, Name or Post Title?
- Orderby ASC changes to DESC in WP_Query
- Change query_posts to WP_Query in page but does not work
- WP_Query condition affects posts_per_page count
- WP_Query order result by date AND meta_value
- Order by title without taking into account ‘the’
- Serve specific “template” page at any URL that ends in its slug
- Slow page loads due to WordPress Core Query
- how to make members list directory through wordpress post custom meta key.
- Advanced ordering of query_posts
- Add class to first post in custom loop using post_class filter
- meta_value_num not ordering all items
- Is there a better way to pull in custom content without querying posts?
- how to get post order by post id wp_query?
- How to get all post except particular category without breaking the paging
- How do I rewrite this loop as a new WP_Query style-loop?
- Filter Custom Taxonomy Posts
- How to provide meta_key array to wp_query?
- Order post type by sum of two custom fields
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- Specify strict ‘order by’ in WordPress query
- Combine 2 arrays in a query parameter
- How do you determine if a result in a search query is a post or a page?
- construct complex queries with WP User Query
- How order posts from category by date and comment count?
- Get a list of ACF Repeater-Fields as array
- Creating attachments archive in tags and categories
- Show single posts date, in a page of posts
- How to get specific string in explode array?
- filter posts by meta key with pagination
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Custom template for password protected page
- WP_Query Class custom field parameters
- Add AJAX “Load more” on custom query block
- Elementor posts custom query based on tag [closed]
- WP_Query order by multiple meta keys & fields
- Facing problem with tax_query results
- sanitize_post() is not sanitizing Post Object
- Retrieve all custom field values of a specific custom field metakey as an array, inside WP_Query
- Query for current post
- Loop through array of pages
- Query Order By Number Variable From API
- How to query most recent post for each author?
- Sorting query_posts() with a complex orderby filter
- Order 2 meta_queries differently in WP_Query?
- How to search CPTs in draft using get_page_by_title()
- How to show post title in content editor in backend?
- Custom WP_query and integrating into theme file
- Best way to Order Post in Home without a plugin
- WP_Query: Meta_Query with serialized value (or a workaround)
- Query All Attachments and Order by Parent Publish Date
- Order posts by custom column using pre_get_posts
- Add filter to Orderby Parameter using Array
- Iterate through ID’s in loop
- Order WP_Query results in order other than ASC or DESC
- How to organize a WP_Query’s list of posts by category and display category title?
- Sorting: custom query with orderby meta_value_num THEN by title
- Get post closest to today
- query post based on comparison
- Converting an existing query_posts to WP_Query
- WordPress Query showing multiple titles
- custom query to get posts
- Dynamically create array from page title
- Multiple loop for “featured” items returns wrong posts
- WordPress Query Returning Every Post
- query_posts problem – need help
- WP Query – Show custom posts only if user contain some user meta
- Show number of posts AND number on current page (cannot make it work)
- Why does wp_query only display the most recent post when using order ASC?