Skip to content

Instantly share code, notes, and snippets.

@asha23
Created March 16, 2018 15:43
Show Gist options
  • Save asha23/c9e6ce53d5efe0328968fe73d0c35f94 to your computer and use it in GitHub Desktop.
Save asha23/c9e6ce53d5efe0328968fe73d0c35f94 to your computer and use it in GitHub Desktop.
Ordering by surname (WordPress)
$my_query = null;
$my_query = new WP_Query($args);
$docs = $my_query->posts;
function lastNameSort($a, $b)
{
$aLast = end(explode(' ', $a->post_title));
$bLast = end(explode(' ', $b->post_title));
return strcasecmp($aLast, $bLast);
}
uasort($docs, 'lastNameSort');
foreach ($docs as $doc) {
$output .= '<option value="'.
get_permalink($doc->ID).
'">'.
$doc->post_title.
'</option>';
}
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment