如何获取最新的投资组合帖子然后将其随机化?
$args = array('numberposts' => 8,'post_type' => 'portfolio','orderby'=> 'id','order' => 'desc');
// get results
$the_query = new WP_Query( $args );
最佳答案
尝试使用 'orderby'=> 'rand'
,而不是 'orderby'=> 'id'
。
来源:http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
您可能还应该指定 'posts_per_page' => 8
而不是 numberposts
。
但也许您想要 8 个最新帖子,然后随机选择 8 个帖子之一。如果是这样,这可能有效:(并指定 orderby
=> 'ID' [大写字母])
$posts = $the_query->get_posts();
$rand_key = array_rand($posts);
然后$posts[$rand_key]
将随机选择8个帖子之一。
最后,如果您想要随机排列 8 个最新帖子,请尝试以下操作:
$posts = $the_query->get_posts();
shuffle($posts)
关于sql - 如何使用 WP_Query 随机化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137419/