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');
Related Posts:
- How to get Custom Post ID by adding filter to child theme’s function
- Filter categories of posts with checkboxes
- WooCommerce product search titles only
- How come Featured Image isn’t showing up in my Custom Post Type?
- Redeclare/Change Slug of a Plugin’s Custom Post Type
- single-{$post_type}-{slug}.php for custom post types
- Row actions for custom post types?
- How to use a custom post type archive as front page?
- remove custom post type permalink
- How to change default position of WP meta boxes?
- Saving Taxonomy Terms
- Filter by custom field in custom post type on admin page
- How can I remove the “Add New” button in my custom post type?
- Media library – Limit images to custom post type
- Displaying Custom Post Types In “At A Glance” Meta Box
- Display a query with multiple post types and same relationship on a single page
- Set posts of a custom post type to be private by default?
- How to order posts of a custom post type by date DESC in dashboard Admin?
- How to remove Filters from post admin page?
- Custom sortable columns ordered by meta-value?
- How to change “Draft” string for status of custom post type to “Unavailable”?
- Adding a drag and drop re-ordering from a custom post type default list [closed]
- How can I filter posts by post_parent in the admin?
- Custom post status not working
- how to group custom post types
- How do I filter the excerpt metabox description in admin?
- How do I Filter Custom Post Type by Custom Taxonomy in the newest WordPress RESTful API?
- Filter for “get_post_type_archive_link()”
- How to correctly get post type in a the_title filter
- clients list using wordpress
- How to add a CSS class to every image in a Custom Post Type
- expire wordpress user posts
- Best way to filter featured image text for a custom post type?
- How can I get next/ previous post links to order by a filter (by the last word of the title)?
- echo value from ‘select’ field type into page template using cmb2?
- Filter by custom Field for Custom post type Admin Listing
- Forcing all posts associated with a custom post type to be private
- Most efficient way to search for values from CPT in Post content
- WP Admin default view mode for Custom Post Type
- Exclude add_filter from the admin
- Find callback function for custom taxonomy metabox
- Date archives for custom post type
- WordPress 3.1 – How does one add sticky post capabilities to post types
- post_type_link filter causes 404 on the CPT page it’s used on
- Remove wpautop from all posts/pages except my custom post type
- How can I add dropdown widget/box to admin post page?
- Remove date and category filters when editing custom post types
- Detect meta value changes when post is updated (post_updated)
- Taxonomy terms with edit/filter link in wp-admin, in the list of custom posts
- Search everything (posts, pages, tags, cpt, meta)
- Make the ‘all’ filter default instead of ‘mine’ in a custom post type
- Exclude custom function content from certain pages
- Title_save_pre – Simple problem that u know for sure
- Posts to expire (deleted) after a date
- Filter get_categories() for taxonomy term in WordPress
- Debug output during filter execution doesn’t work
- White screen error for a custom theme
- How to Removing fields from the Media Uploader/Gallery on a Custom Post Type Edit Page
- Best way to style first post differently?
- Using Default WordPress Custom Header Image Crop Feature in a Post Type / MetaBox
- How do I add a custom post type to the Featured Content in twenty fourteen theme?
- Ajax filter with custom taxonomies
- Filter posts with ajax form and checkboxes
- Multiple post types in archives (filter?)
- Image size filtering in Media uploader according to custom post type
- How to remove CPT comment feed from head?
- Automatically assign a custom post to a custom taxonomy based on custom field value
- Exclude Custom Post Type & Pages From Auto-Tag Function
- Saving multiple Metabox contents
- Limiting number of custom posts shown on taxonomy page
- if && problems with transition_post_status
- Unable to filter on field on edit.php page
- Is it possible to insert text into an html tag using functions.php?
- Custom user role not working as expected
- Custom post type loops with different page templates
- custom post type upcoming post and past post
- wp_query and comment_parent – select only posts with top level comments
- Highlight nav menu terms
- Help with issues on “Adding a Taxonomy Filter to Admin List for a Custom Post Type?”
- Want to filter only parent post in admin area
- Limit Authors to their Own Posts on front-end excluding admins
- Adding posts of custom type into the main feed
- Used with meta_query in query_posts works slowly
- Hooking into wp_export filter for custom post type [closed]
- Add multiple custom post types in functions.php, but only one custom post type show in dashboard
- Multiple Content Block
- Custom Post Row Actions
- Unable to save or update postmeta for custom post type via action using save_post hook
- Filters post in admin with dropdown select, custom post type
- Admin List Dynamic Heading
- Beginner question: Accessing functions.php through admin web interface in order to import custom post types?
- What is better: Custom Fields vs Custom Taxonomies with Terms for perfomance, scalability and better user experience
- Append taxonomy terms as class names in markup?
- How to: add option to add more checkboxes on custom post type
- How can I add a column in the wp_list_table of the admin area?
- Archive filter disappears on no results?
- How to filter the description of a custom post type
- Allow Users Access to Custom Post Type Only
- How do you output custom code between posts in the loop?
- Display only custom post type count for current author on the “At a Glance” dashboard widget