Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.