How to sort posts automatically by a Last Name included in title

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Automated sorting is always a fast way to maintain ordering without any admin intervention Automatic Order.

In this particular scenario, the post titles include a person full name, we need to use the last name to create an automatic ordering.

Once we set-up a sort, we switch to Automatic Order and use the Custom Function Callback functionality.


In this example the callback function name is called “custom_sorting_function_sort_by_last_name”, this should be placed in above field. The function should be included within theme functions.php or a custom plugin:

function custom_sorting_function_sort_by_last_name( $posts_list, $sort_view_id, $orderBy, $query)
            $list_map   =   array();
            foreach($posts_list  as  $object_id)
                    $object =   get_post($object_id);
                    $name   =   explode(" ", $object->post_title);
                    //remove empty spaces
                    $name   =   array_filter($name);
                    if ( count  ($name)  > 0 )
                            $list_map[$object_id]   =   current($name);
            //sort the dates ascending
            $post_list  =   array_keys($list_map);
            return $post_list;  

Notify of
Inline Feedbacks
View all comments