order by second word in title?

There is a filter, ‘posts_orderby’, which allows you to specify your own ORDER BY clauses. In your case, the code would look something like this:

add_filter( 'posts_orderby', 'order_by_surname' );

function order_by_surname( $orderby, $query ) {

    // first you should check to make sure sure you're only filtering the particular query
    // you want to hack. return $orderby if its not the correct query;

    return "ORDER BY SUBSTR(
        LTRIM({$wpdb->posts}.post_title), 
        LOCATE(' ',LTRIM({$wpdb->posts}.post_title)))";

}

Leave a Comment