Unfortunately, that will never work. ‘rand’ is a random order, so you always end up with a randomized order, no matter what you put for the date.
In order to do something like what you’re describing, you have to use 'orderby'=>'rand'
. Then you can sort the result by date.
$query = new WP_Query(
[
'post__in' => $post_id_array,
'posts_per_page' => $number,
'orderby' => 'rand'
]
);
// Sort the resulting posts array by date
usort( $query->posts, function( $a, $b ) {
$t1 = strtotime( $a->post_date );
$t2 = strtotime( $b->post_date );
return $t1 - $t2;
});
// $query->posts is now sorted and in order by date.
Related Posts:
- WP_Comment_Query pagination, delving into the unknown
- Sort posts by category name and title
- Getting all user metadata from user
- Loop through all tags & output posts in alphabetical list
- Pagination with 5 posts per page
- Sorting Posts by custom field
- Order posts by date and then by custom field
- Order by the results of a function with WP_query
- WP_query category__in not working, only pulls from first category
- tax_query shows no results if nothing is selected
- meta_key and meta_value not working together
- Select random post every day
- get_query_var returns null
- post thumbnail not showing up with shortcode
- Different string for specifed post type on posts listing at homepage
- How can I order Wp_Query hierarchically?
- Meta query with JSON value
- Prevent duplicate post from dynamic multiple loops
- WP_Query ajax loader detect end of posts
- How to get count of posts assigned to given category?
- Can I count matches without doing a query?
- Wp redirect to url with ampersand string
- Get attachment by meta_key value
- Complex WP_Query order request: DESC by day, but then ASC by time
- How to detect if query is runned from widget?
- Using meta_query with multiple keys and compare values
- Change query based on post type while staying in loop
- Sort wordpress custom posts based on meta value
- Custom Pagination Layout for Elementor widget
- $WP_Query: How to display excerpt on first post only
- How to properly reset a nested WP_Query query
- How to make search for posts using get method?
- Is there a reason why Pages are not publicly_queryable?
- How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?
- Exclude some authors from query
- apply custom where to the default $wp_query
- wp_query serialised array
- Conditional operator OR not working with custom fields
- How to add custom meta to ‘pre_get_terms’?
- How do I check if an article is popular this week?
- Using Transients
- Should I reset $wp_query?
- how to write wordpress query for multiple metakeys checking?
- Sorting search results with custom dropdown
- Posts query according to meta box date
- create metabox to activate slider
- How to display a list of posts in same child category as current post
- querying posts with a custom status
- problem with loop
- Display post meta conditionally based on it’s value
- WP_Query will not display draft posts
- How can i show pagenavi in my author.php?
- Randomly select one of last 3 posts
- Query multiple meta key values for template
- How do I get the content of a custom instance of WP_Query?
- Change post query onclick
- Using WP_Query with multiple loops on one page in search.php
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- Order by post meta not working with pre_get_posts filter
- Searching not only by post name but also category
- WP_Query, Paginate by Custom Field
- Query post and sort by custom value calculated with custom field on cpt
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- is_user_logged_in not working from WP-API (wp-json)
- WordPress private post won’t display to other admins
- Only show tag with the same id as the post
- Search Exact on WordPress – Relevannsi Solution Not Working
- WP_Query() not working as expected
- How to sort store location by specific category order in WP store locations
- ACF: How can I publish values of ACF fields in a loop while using wp_query?
- Sort by price wpdb
- Posts per Page not working
- Trouble with serialized metadata
- Show a message if there are no active posts in category
- Get authors list and sort them by recent posts
- Custom filter from post meta
- Problem with my loops
- next_posts_link not visible on first page
- Pagination not working in custom loop WP_Query
- Multiple wpostmeta.meta_key
- Saving custom fields for WP_Query to retrieve
- Modify query to exclude certain ids and certain parents and its corresponding children
- How to fix pagination for custom loops on front-page.php
- Filter posts/pages by user_role array
- WP_Query with meta_value_num and meta_query not paged correctly
- I need value of saved meta to display next to the post it is associated with
- Post Query not working after updating to 4.1
- Cannot order by in WP_Query
- How to sort a WP_Query array by post_name after an array_merge();
- Comment count same for every post in homepage WP_Query
- Search Query that Includes Custom Table
- Pagination with WP_Query not working
- new WP_Query based on average values of meta fields
- WP_Query breaks pagination
- wpdb LIKE request shows all database data
- Can’t seem to get an else statement correct? [closed]
- query_vars category_name only display one catgory out of multiple categories
- Incorrect posts displayed on category page
- How do I have multiple metaqueries inside one wordpress query
- Can I use WP_Query to find a post with a particular menu_order?