【发布时间】:2014-09-22 22:06:57
【问题描述】:
如果我有自定义帖子类型 (post_type),并且我想保证帖子类型为 product 的任何内容出现在搜索结果中的其他内容之前,我该如何实现?最好只更改查询的order by 部分。
目前我有:
ORDER BY posts.post_type DESC, posts.post_date DESC
这可行,除了 DESC 上的 product 之前的另一个帖子类型 testimonial。在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;
}
【问题讨论】:
标签: mysql wordpress woocommerce