Sorting titles by last name in WordPress

Update (Feb 5 2014): I came up with a better method (github link), wrapping everything inside a function:

I’m sharing here a little php trick I came up with for a recent WordPress site: I needed to create alphabetic listings of persons and order them by their last name, but at the same time, I wanted to display the post titles as Firstname Lastname.

It’s a rather common problem, and various people have already attempted to solve it through very convoluted methods.

The much easier solution I came up with:

  • In the title field, I write the name in manner that allows me to use the normal alphabetic listing of WP_Query: starting with the Lastname, and using a comma as separator. For instance “Wilde, Oscar” instead of “Oscar Wilde“.
  • In the theme, when I want to display the title with the Firstname first, I use the php functions strpos and strstr to revert the order.

Here is the code snippet I wrote:

$mystring = get_the_title();
$findme = ',';
$pos = strpos($mystring, $findme);

if ($pos === false) {
    $regex_name = $mystring;
} else {
    $firstname = strstr($mystring, ',');
    $lastname = strstr($mystring, ',', true); // As of PHP 5.3.0
    $firstname = trim( substr($firstname, 1) );
    $regex_name = $firstname." ".$lastname;

It will revert the order only if a comma is present in the title, otherwise the title remains as it is.

And here is how it’s implemented in the theme:

include( TEMPLATEPATH . '/inc/name-regex.php' );
echo $regex_name;

So simple and efficient ;)

Note that the title $regex_name I gave to my variable is misleading, as there isn’t any regex (regular expression) involved in that function.