2013-03-25 25 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之後)排序是沒有用的。