如果我有自定義帖子類型(post_type
),並且我想保證任何帖子類型爲product
的帖子出現在搜索結果中的其他任何內容之前,我怎麼能實現這一點?最好通過改變查詢的部分order by
。通過查詢MYSQL訂單
目前我有:
ORDER BY posts.post_type DESC, posts.post_date DESC
這工作,但另一篇文章類型testimonial
來product
上DESC
之前。在ASC
不同的自定義帖子類型articles
之前product
但我需要product
先來(與他們訂購post_date
),然後一切 - 也排序post_date
。
的完整代碼:
add_filter('posts_orderby','search_sort_custom',10,2);
function search_sort_custom($orderby, $query)
{
global $wpdb;
if(!is_admin() && is_search()) {
$orderby = $wpdb->prefix."posts.post_type DESC, {$wpdb->prefix}posts.post_date DESC";
}
return $orderby;
}
謝謝,我能做到! post_type = '產品' DESC, posts.post_type = '產品' DESC, posts.post_date DESC – Martin 2014-09-22 22:23:51
抱歉,您仍然是保持分組的舊錶達式。請參閱更新。比較結果返回1或0(真/假),所以你的建議與我原來的一樣。 – Arth 2014-09-22 22:32:25
Thanks Arth ...因此,這將返回由post_date DESC首先訂購的所有產品,以及由post_date DESC訂購的所有其他產品(混合在一起)。如果是這樣,真棒,因爲這是我所追求的。 – Martin 2014-09-22 22:46:46