functions - Sort search results by post type

I need to sort my search results by custom post type. the problem: the order is custom.I do have 5 different post types

I need to sort my search results by custom post type. the problem: the order is custom.

I do have 5 different post types and i got the order like so:

  1. artist
  2. artwork
  3. page
  4. post
  5. publication

I'd like the order to be like this:

  1. artist
  2. post
  3. artwork
  4. publication
  5. page

Here is what I have so far, the elements are grouped by their post type and get sorted by title. Wonderful. Now all I need is the custom ordering.

Any hint?

add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 2 );
function order_search_by_posttype( $orderby ){
   global $wpdb;
    if( ! is_admin() && is_search() ) :
        $orderby = "{$wpdb->prefix}posts.post_type ASC, {$wpdb->prefix}posts.post_title ASC";
    endif;
    return $orderby;
}

I need to sort my search results by custom post type. the problem: the order is custom.

I do have 5 different post types and i got the order like so:

  1. artist
  2. artwork
  3. page
  4. post
  5. publication

I'd like the order to be like this:

  1. artist
  2. post
  3. artwork
  4. publication
  5. page

Here is what I have so far, the elements are grouped by their post type and get sorted by title. Wonderful. Now all I need is the custom ordering.

Any hint?

add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 2 );
function order_search_by_posttype( $orderby ){
   global $wpdb;
    if( ! is_admin() && is_search() ) :
        $orderby = "{$wpdb->prefix}posts.post_type ASC, {$wpdb->prefix}posts.post_title ASC";
    endif;
    return $orderby;
}
Share Improve this question edited Aug 26, 2016 at 9:37 honk31 asked Feb 10, 2015 at 10:47 honk31honk31 1,4081 gold badge13 silver badges23 bronze badges 3
  • btw. there is no chance to rename the post-types, they do exist and the content is already in.. – honk31 Commented Feb 10, 2015 at 10:48
  • I would honestly just use usort to sort the returned array by post type – Pieter Goosen Commented Feb 10, 2015 at 11:05
  • @PieterGoosen could you be more precise? I honestly don't really understand the result in $orderby, so I don't know how to sort it. (I found an answer, but if usort would be a better way, I'd like to know it..)(See I use capitals now, just for you..) – honk31 Commented Feb 10, 2015 at 11:11
Add a comment  | 

1 Answer 1

Reset to default 24

I found the key: SQL CASE Expression

function order_search_by_posttype($orderby){
    if (!is_admin() && is_search()) :
        global $wpdb;
        $orderby =
            "
            CASE WHEN {$wpdb->prefix}posts.post_type = 'artist' THEN '1' 
                 WHEN {$wpdb->prefix}posts.post_type = 'post' THEN '2' 
                 WHEN {$wpdb->prefix}posts.post_type = 'artwork' THEN '3' 
                 WHEN {$wpdb->prefix}posts.post_type = 'publication' THEN '4' 
            ELSE {$wpdb->prefix}posts.post_type END ASC, 
            {$wpdb->prefix}posts.post_title ASC";
    endif;
    return $orderby;
}
add_filter('posts_orderby', 'order_search_by_posttype');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742285939a4415322.html

相关推荐

  • functions - Sort search results by post type

    I need to sort my search results by custom post type. the problem: the order is custom.I do have 5 different post types

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信