I think I got it!!!
Try this:
$orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order. " , {$wpdb->posts}.post_title " . $get_order;
Basically it’s sorting by lastname and then first name in 1 query.
So, your whole code would be:
add_filter( 'posts_orderby', function( $orderby, \WP_Query $q )
{
if( 'wpse_last_word' === $q->get( 'orderby' ) && $get_order = $q->get( 'order' ) )
{
if( in_array( strtoupper( $get_order ), ['ASC', 'DESC'] ) )
{
global $wpdb;
$orderby = " SUBSTRING_INDEX( {$wpdb->posts}.post_title, ' ', -1 ) " . $get_order. " , {$wpdb->posts}.post_title " . $get_order;
}
}
return $orderby;
}, PHP_INT_MAX, 2 );
Related Posts:
- How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name
- Assign posts to taxonomy terms instead of the taxonomy terms to posts?
- How to completely disable a taxonomy archive on the frontend?
- Restrict taxonomy dropdown to post type
- array_pop() expects array, boolean given for $event_type_term ->slug
- custom post type permalinks with category and tags
- Get template part based on custom taxonomy term
- WordPress returns 404 on custom rewrite rule conflict in parameters
- How to show term-specific post list, without posts associated with child terms?
- Conditional two level dropdown filter for custom post type
- Show all custom post type posts sorted by custom taxonomy then by another custom taxonomy
- Custom Post Type Category List & Post Count
- Custom taxonomy archive page not working
- How to show multiple post types on taxonomy archive?
- Custom post type / custom fields
- Custom post type taxonomy template
- using custom taxonomies on non wp table?
- Remove Taxonomy Slug when No Taxonomy is Assigned to Custom Post Type?
- Get the posttype of a taxonomy/term
- Taxonomy Templates
- Custom Taxonomy Creates Unwanted Integer Terms on Post Save
- Add hierarchical taxonomy to permalink for custom post type
- Custom Posts Hierarchy? (State/City/Restaurant)
- Custom post types, taxonomies, and permalinks
- Custom Post Type vs. User Page
- How to rewrite custom post type with custom taxonomy urls?
- How to get posts from multiple custom post types according to it’s custom taxonomy terms?
- Is it possible to create a shortcode that will query a post based on taxonomies?
- need advice on how to do a lists using custom post types – taxonomy vs postmeta
- get_template_part for each level of taxonomy term
- Displaying a custom post types custom taxonomy value?
- custom permalinks based on taxonomies for custom post type
- Am unable to reset a query properly
- Conditional Statement custom post type category
- Custom Post Type permalink shows wrong page: homepage/index.php
- using $wpdb to get custom post type with term
- Categories sorting
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Allow user to set custom order to a list of custom taxonomies?
- Display Custom Toxonomy In WordPress and theme Widgets category selection dropdown list
- How do I require the specification of term in a custom post type and custom taxonomy?
- Custom Post Type Pagination Paginates Only in URL Structure
- Terms showing up in object cache on unrelated pages
- I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
- Hierarchial Custom Post Types/Taxonomies
- Hierarchical permalinks for custom post type and taxonomy
- Only show posts with date of today or in the future (i.e don’t show past posts)?
- Best way to specify “article type” in URL
- Rename a slug label
- Dynamically insert an article at the top of a taxonomy archive – or?
- WordPress multisite – is it possible to have different taxonomies for each site?
- Migrating Hierarchal Taxonomy Categories Between Post Types
- How to show list of taxonomy terms associated with specific post?
- Exclude latest post from WP_Query taxonomy term loop
- I have custom post type with custom taxonomy. But not found the taxonomy page..
- Display Taxonomy Term Children in a Drop Down without Submit Button
- Use different categories for a custom post
- Filter posts by specific custom category and current tag
- Help on Custom Post Types
- Custom Post Type URL doesn’t work anymore
- using custom taxonomies with custom post types: display list of posts by ‘category’
- Custom category URL not displaying posts
- Display featured posts for a custom post type by taxonomy
- What is the most efficient way to execute recursive complex queries?
- 2 custom post type paging in 1 custom taxonomy
- WordPress REST API V2: “{CUSTOM_POST_TYPE} matches Term ID List and Term ID Taxonomy Query, but should match only one.”
- Duplicate slug/permalink issue while adding same post name
- how to achieve this permalink abc.com/CPTName/CustomPostTypeCategorySlug/categoryname
- display post count in archive page that have relation with another taxonomy term
- Remove custom permalink base from CPT custom category and custom tags
- Pretty Permalinks with CTP and Taxonomies / Hierarchical
- Change Search display for Custom Post Type
- WooCommerce – Custom related product (Spareparts)
- Automatically add CPT UI categories to the menu
- Custom category taxonomy – archive page not showing up
- tax_query not working?
- Issue with Custom Post Types and Permalinks
- Automatically Add a default category to a Custom Post Type before save_post action
- Get list of all Topics in use by a custom post type
- I want to display my custom post type arranged by taxonomy
- Connect 8 Taxonomies to One (Main) taxonomy. How to?
- Display posts grouped by post type in taxonomy.php
- Advice on Categorization for a Custom Post Type
- Pods pagination erases my sub menu
- How to have permalink structure based on taxonomy terms
- Checking Taxonomy Terms for a Custom Post Type With get_the_terms Not Working
- How to List CPTs Under One Tax Term
- Displaying or Hiding Metadata on Page from WP CPanel
- Get posts that have custom post type value
- Taxonomy Query of Custom Post Displays Archive Instead of Posts
- Listing custom post type items from a couple of custom taxonomies
- Invalid Taxonomy in template
- Permalink for Custom Post Types
- Catergory args causing loop not to show
- Function not working on live server but every thing is fine on localhost
- How to change custom post type slug without damage seo? [closed]
- Custom Post Types using wrong template (index) instead of archive-{type}.php – previously worked as expected
- Custom Post Type urls not working
- pre_get_posts causes Custom Posts appear under ‘Pages’ menu
- Show custom category archive as front page and remove taxonomy slug from urls