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' );
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s