![](/img/trans.png)
[英]WordPress pre_get_posts ordered randomly is duplicating the posts
[英]Posts not being ordered randomly
我试图让随机帖子出现在这个帖子循环中:
即使我添加了'orderby' => 'rand',
它仍然始终显示相同的 4 个帖子。 这是我的 post 循环的完整代码:
$query = new WP_Query(array(
'post_status' => 'publish',
'orderby' => 'title',
'orderby' => 'rand',
'cat' => '3',
'order' => 'ASC',
'posts_per_page' => 4
));
$post_count = $query->post_count;
$posts_per_column = ceil($post_count / 4);
$rows = array();
$count = 0;
while ($query->have_posts())
{ $query->the_post();
if($rows[$count] == ""){ $rows[$count] = ''; }
$rows[$count] = $rows[$count] . '<div class="col-post"><div class="post-title"><a href="'.get_permalink().'">'.get_the_title().'</a></div></div>';
$count++;
if ($count == $posts_per_column ) { $count = 0; }
}
foreach ($rows as $row) { echo $row . ''; }
wp_reset_query();
您当前正在运行两个orderby
arguments,这导致其中一个被忽略。 您明白按'orderby' => 'title',
和'orderby' => 'rand',
排序是不兼容的。
以下应该工作:
$query = new WP_Query( array(
'post_status' => 'publish',
'orderby' => 'RAND',
'cat' => '3',
'posts_per_page' => 4
) );
按参数对检索到的帖子进行排序。 默认为“日期(post_date)”。 可以传递一个或多个选项。
...
'rand' – 随机顺序。
'title' – 按标题排序。
此外, 'order' => 'ASC',
不是必需的(它只是被忽略了,你正在传递和争论什么都不会减慢查询速度)因为你正在使用'orderby' => 'RAND',
, by默认值设置为'orderby' => 'DESC',
指定'orderby'参数的升序或降序。 默认为“DESC”。 一个数组可以用于多个 order/orderby 集。
'ASC' – 从最低值到最高值的升序(1、2、3;a、b、c)。
'DESC' – 从最高值到最低值的降序(3、2、1;c、b、a)。
来源@ https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.