Created
April 25, 2018 09:00
-
-
Save moskalukigor/80c8e7a25a5e7d0edc7d91957309b168 to your computer and use it in GitHub Desktop.
Wordpress Search Order By Post Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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