首先对你的帖子进行随机排序。然后,在外部查询中,按日期对子查询中的帖子进行排序。
我会在过滤器的帮助下完成posts_request
和posts_orderby
. 只有在设置\'ord_rand_then_date\' => true
参数
add_filter(\'posts_request\', \'se354396_request\', 50, 2);
add_filter(\'posts_orderby\', \'se354396_orderby\', 50, 2);
function se354396_request( $sql_request, $wp_query )
{
if ( ! $wp_query->get(\'ord_rand_then_date\', false) )
return $sql_request;
$sql_request = \'SELECT sub.* FROM (\' . $sql_request . \') sub ORDER BY post_date DESC\';
return $sql_request;
}
function se354396_orderby( $orderby, $wp_query )
{
$custom_order = $wp_query->get(\'ord_rand_then_date\', false);
if ( $custom_order !== true && $custom_order != \'1\' )
return $orderby;
return \'RAND()\';
}
Example:
$my_posts = get_posts( [
\'ord_rand_then_date\' => true,
\'suppress_filters\' => false,
// other parameters
] );