2014-09-22 5 views
0

사용자 정의 게시 유형 (post_type)이 있고 게시 유형이 product 인 항목이 검색 결과에 앞서 나타남을 보장하려면 어떻게해야합니까? 가급적 간단히 쿼리의 일부분을 변경하여 order by 부분을 변경하십시오. 검색어로 MYSQL 주문

현재 내가 가진 :

ORDER BY posts.post_type DESC, posts.post_date DESC 

testimonial의 또 다른 포스트 유형을 제외하고, 작동은 DESCproduct 전에 온다. ASC에서 다른 맞춤 게시물 유형 articlesproduct 앞에 있지만 처음에는 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; 
} 

답변

1

당신은 ORDER BY 절에 표현식을 사용할 수 있습니다 :

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 posts.post_type='product' DESC, 
     posts.post_type DESC, 
     posts.post_date DESC 

당신이 할 수있는 :

ORDER BY FIND_IN_SET(posts.post_type,'product, testimonial'), 
     posts.post_type DESC, 
     posts.post_date DESC 

하지만 제품 유형을 다른 테이블로 옮기고 각 유형에 우선 순위 정수를 지정하는 것이 이상적입니다. 그러한 드라마없이 순서를 완전히 제어 할 수 있습니다!

+0

감사를 할 수있는 나는 할 수있다! post_type = '제품'DESC, posts.post_type = '제품'DESC, posts.post_date의 DESC – Martin

+0

사과를하면 아직 그룹화를 유지하는 오래된 표현 .. 업데이 트를 참조하십시오. 비교 결과는 1 또는 0 (true/false)을 반환하므로 제안 내용이 원본과 동일합니다. – Arth

+0

감사합니다 Arth ... 그래서 이것은 post_date DESC에 의해 정렬 된 모든 제품을 반환 할 것입니다. 그렇다면, 내가 겪어 온 그대로이기 때문에, 굉장합니다. – Martin

0

당신은 단순히 그냥

ORDER BY (
    posts.post_type = 'product' DESC, 
    posts.post_type DESC, 
    posts.post_date DESC 
)