2013-03-25 50 views
1

我正在烹饪一个简单的脚本来显示由rand和post_date命令的顺序。我在下面尝试过,但它不起作用:排序查询rand和post_date wordpress

$myposts = get_posts('numberposts=4&orderby=rand post_date&order=ASC); 

提前致谢!

回答

0

尝试

$myposts = get_posts('numberposts=4&orderby=post_date,rand&order=ASC); 
+0

不,你必须使用空间:http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters – soju 2013-03-25 15:41:36

+1

哦,是的。我知道如何构建这个SQL查询,但WP_Query不支持它。有点像︰orderby = post_date&order = RAND – Barif 2013-03-25 15:45:32

+0

这是“不起作用”,实际上我想rand所有帖子和后应用post_date排序,在SQL是很容易的,但与WordPress的“API”不是很简单... ... - – 2013-03-25 15:49:45

1

你的代码的工作,但rand后加入post_date根本没用......

SQL查询将是这样的:

SELECT [...] ORDER BY RAND() ASC, post_date ASC 

RAND()将返回浮点值,它不会返回两次相同的值,所以按post_date(在rand之后)排序是没有用的。