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
- Redeclare/Change Slug of a Plugin’s Custom Post Type
- remove custom post type permalink
- Display a query with multiple post types and same relationship on a single page
- Custom sortable columns ordered by meta-value?
- how to group custom post types
- echo value from ‘select’ field type into page template using cmb2?
- Exclude add_filter from the admin
- WordPress 3.1 – How does one add sticky post capabilities to post types
- Posts to expire (deleted) after a date
- White screen error for a custom theme
- Automatically assign a custom post to a custom taxonomy based on custom field value
- Custom user role not working as expected
- wp_query and comment_parent – select only posts with top level comments
- Limit Authors to their Own Posts on front-end excluding admins
- How can I add a column in the wp_list_table of the admin area?
- How to filter the description of a custom post type
- Filter home_url for custom post type
- Add Content Exclusively to a Custom Post Type Feed
- How to change the admin menu “Pages” to something else
- Apply post formats to a specific post type only?
- Loading all files within a directory
- wp_insert_post generates endless posts
- Why is conditionally loading a custom plugin’s code only on a specific custom post type causing the site content to disappear?
- Dequeue scripts and styles only for specific custom post type
- How to customize a permalink (URL) structure?
- Can’t edit custom post type
- PHP variable not regenerating when publishing multiple posts at the same time
- Redirect 404 page with ID in slug to associated page with same ID in slug
- WordPress wrapped added a span tag to every single p tag
- Display featured image from one CPT within another CPT query
- Best way to style first post differently?
- How to rewrite url for any specific taxonomy?
- How to change the post type a theme shows by default?
- filter custom post in rest api with custom function
- Sort and filter custom post type posts by custom taxonomy
- Shortcode not working with post counter
- Allow users to create posts without logging in?
- Custom filter function not working with Custom post type
- How to assign classes to all elements?
- Search filter triggered & sort by custom post type
- Redirect to another page using contact form 7? [closed]
- How can I add a filter to a particular post format?
- Meta-Boxes for CustomPostType cause PHP Errors and Notices in “Add New” view
- custom post type metaboxes not saving
- Assign category a default post type
- Add an Outside Wrapper to my Header and Make it Full Width
- Showing custom post type categories in the menu
- How to use a variable as a function name?
- How can I remove filters from custom post types?
- Problem with Apply Filters on URL in meta box filed of custom port type
- how to retain the ability to modify the post slug after applying a post_type_link filter?
- posts within custom post type all share the same content in the front-end
- Meta box not appearing in my custom post type
- Let users upload image(s) to the post from front end
- Change Custom Post Type singular_name through function/filter
- Function to erase every post from a taxonomy
- Have custom post type Visibility be private by default, and have radio button also set to “Private”
- Sorting custom post types in edit.php : Post disappear
- Function to allow single post template based on custom taxonomy?
- conditional filter
- how to achieve this permalink abc.com/CPTName/CustomPostTypeCategorySlug/categoryname
- What is the best way to set the post category (Custom Post Type) automatically based on the one of the tags assigned to the post?
- Custom Form / Search with Custom Post Type Data
- Create WordPress User in backend, and automatically create a new post in Custom Post Type with user uame as the Title in the post
- Different number posts per page based on custom post type term id
- CPT tags not showing when editing post
- Persist meta box choices throughout all posts of same post type
- How can I add image sizes for a specific custom post type?
- Add content to wordpress edit.php page
- add_rewrite_tag broke permalinks that doesn’t use that specific tag
- Why is wp_list_authors not picking up Authors from CPT’s?
- Change Search display for Custom Post Type
- subtracting the current post form then whole loop, which is generating all CPT titles
- Custom Function to redirect singular post if specific meta field is empty
- fusion builder missing from custom post editor
- Insert custom taxonomy into category query
- filter rest api post by a acf filed
- Group Custom Posts Types in a Relation field of ACF
- Stored meta from attachment, video length?
- how it’s possible to show from a post of a custom post type the taxonomy/terms?
- Highlight a Post on archive page if it has a new comment?
- custom post type does not working when adding in child theme function
- Menu disappears with custom post type link
- Converting a checkbox filter for custom fields to a dropdown
- How can i call a custom method on submission of a custom plugin post type?
- using ACF datepicker to filter posts on a page
- Hook only specific post type
- Restrict a filter to a custom post type. Am I doing this right?
- Updating User Meta from Custom Post Field Upon Publish Not Working
- Generate daily archive whenever any post type is added
- Invalid Taxonomy in template
- Custom post type functions.php if statement on action
- A to Z list for custom post types
- Passing postid of Testimonial Custom Post in Shortcode Parameter
- Custom Post Types using wrong template (index) instead of archive-{type}.php – previously worked as expected
- Is it possible to use a post name in a custom post slug?
- Add post id to url instead of WordPress default -2 suffix