Skip to main content
Tweeted twitter.com/StackWordPress/status/1019957159960948736
edited title
Link
honk31
  • 1.4k
  • 1
  • 13
  • 23

Sort search with posts_orderbyresults by post type

deleted 31 characters in body; edited title
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209

sort Sort search with posts_orderby

hi and thanks for stopping by..

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

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

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

i'dI'd like the order to be like this:

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

hereHere is, what i gotI have so far, the elements are grouped by their post type and get sorted by title. wonderfulWonderful. nowNow all iI need is the custom ordering. any

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;
}

sort search with posts_orderby

hi and thanks for stopping by..

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 got 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;
}

Sort search with posts_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;
}
Source Link
honk31
  • 1.4k
  • 1
  • 13
  • 23

sort search with posts_orderby

hi and thanks for stopping by..

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 got 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;
}