Skip to content

Instantly share code, notes, and snippets.

@moskalukigor
Created April 25, 2018 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moskalukigor/80c8e7a25a5e7d0edc7d91957309b168 to your computer and use it in GitHub Desktop.
Save moskalukigor/80c8e7a25a5e7d0edc7d91957309b168 to your computer and use it in GitHub Desktop.
Wordpress Search Order By Post Type
<?php
add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 1 );
function order_search_by_posttype( $orderby ){
if( ! is_admin() && is_search() ) :
global $wpdb;
$orderby =
"
CASE WHEN {$wpdb->prefix}posts.post_type = 'product' THEN '1'
WHEN {$wpdb->prefix}posts.post_type = 'post' THEN '2'
WHEN {$wpdb->prefix}posts.post_type = 'page' THEN '3'
WHEN {$wpdb->prefix}posts.post_type = 'attachment' THEN '4'
WHEN {$wpdb->prefix}posts.post_type = 'showroom' THEN '5'
WHEN {$wpdb->prefix}posts.post_type = 'newspress' THEN '6'
WHEN {$wpdb->prefix}posts.post_type = 'infopages' THEN '7'
WHEN {$wpdb->prefix}posts.post_type = 'gallery' THEN '8'
ELSE {$wpdb->prefix}posts.post_type END ASC,
{$wpdb->prefix}posts.post_title ASC";
endif;
return $orderby;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment