如果我有自定义的post类型(post_type),并且我希望保证具有post类型的product的任何内容都出现在搜索结果中的其他任何内容之前,那么我如何做到这一点呢?最好只是修改查询的order by部分。
目前我有:
ORDER BY posts.post_type DESC, posts.post_date DESC这是可行的,除了另一种post类型的testimonial出现在product on DESC之前。在ASC上,一个不同的自定义post类型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;
}发布于 2014-09-22 22:11:11
可以在ORDER BY子句中使用表达式:
ORDER BY posts.post_type='product' DESC,
posts.post_type DESC,
posts.post_date DESC如果您有更多的要求,您可以:
ORDER BY posts.post_type='product' DESC,
posts.post_type='testimonial' DESC,
posts.post_type DESC,
posts.post_date DESC或者使用FIND_IN_SET
ORDER BY FIND_IN_SET(posts.post_type,'product, testimonial'),
posts.post_type DESC,
posts.post_date DESC但是,理想情况下,我会将产品类型转换到另一个表,并给每个类型一个优先级整数。这样你就可以完全控制秩序而不需要这样的戏剧!
发布于 2014-09-22 23:53:36
你可以简单的做
ORDER BY (
posts.post_type = 'product' DESC,
posts.post_type DESC,
posts.post_date DESC
)https://stackoverflow.com/questions/25983840
复制相似问题