WordPress Orderby Last Word In Title

Try this. First add the following orderby filter in functions.php

function posts_orderby_lastname ($orderby_statement) 
  $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) DESC";
    return $orderby_statement;

and then use it in your query like so

add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
    $loop = new WP_Query(
        array (
            'post_type' => 'staff',
            'staff-type' => $type

and after the loop remove the filter

remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );

By milind

Milind is a Senior WordPress Engineer at rtCamp.
WordPress Ecosystem Contributor and AMP Support Engineer.