Support » Fixing WordPress » Sort posts alphabetically by last name or last word

  • Hi I am hoping someone is clever enough to help me out. I have a category in my site that is called “clients” and I basically have a page that just lists all of it’s posts’ titles (client names). Does anyone know of a way to order the posts alphabetically by the clients last name or have a loop that order the last word in the title? So it would be:

    John Anderson
    Betty Boo
    Anthony Downes

    Thanks so much.

Viewing 1 replies (of 1 total)
  • Try adding this just before the query_posts():

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');
    $mam_global_orderby = "
    UPPER(CONCAT(REVERSE(SUBSTRING_INDEX(REVERSE($wpdb->posts.post_title),' ',1)),$wpdb->posts.post_title))
    ";

    and this just after:

    $mam_global_orderby = '';

Viewing 1 replies (of 1 total)
  • The topic ‘Sort posts alphabetically by last name or last word’ is closed to new replies.