I have full names as title posts and want to order it by the surname.

How could i do this with wordpress?

in PHP it would be along the lines of -

 SELECT * from posts ORDER BY SUBSTR(LTRIM(post_title), LOCATE(' ',LTRIM(post_title)))

current code getting people category is;

$args = array(
    'cat'=> '39,-41',

how can i get that second word order?

best, Dan.

  • Great question—wish I could upvote it twice.
    – supertrue
    Commented Sep 6, 2012 at 5:13
  • Couldn't you use a Custom Select Query? codex.wordpress.org/… I need to do the same thing. Will see if it works.
    – user20556
    Commented Sep 18, 2012 at 16:45

2 Answers 2


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(
        LOCATE(' ',LTRIM({$wpdb->posts}.post_title)))";

  • thanks - but how would i apply that to only this query_posts? D.
    – v3nt
    Commented Dec 7, 2011 at 16:22
  • 2 ways - (1 - more hackish): add this filter right before running the query, remove it afterward. (2 - slightly better) at the beginning of the filter function, check the $query object for variables which are only set in this particular query and return the original $orderby if they're not there. Commented Dec 7, 2011 at 19:09
  • This now ends up with a 404 error. WordPress must have changed since 2011. It always gives an error about the $query parameter being missing.
    – Drew Baker
    Commented Apr 8, 2014 at 23:09

Your first example is an SQL query, there is no native WordPress function that does this.

So that leaves you with some PHP options, explode it, str_word_count, along with sort (or ksort) to then arrange the array alphabetically.

This is a very odd way to use this type of data, it should be in a meta box, since you basically have to hack the query in your case to get what you want.

  • we're doing a mass import and everything is in the title field. Rather try this in wordpress than excel! Also keeps it easier to manage for the client...
    – v3nt
    Commented Dec 7, 2011 at 16:23
  • WordPress supports custom fields for exactly this, honestly using the title is a horribly bad idea, codex.wordpress.org/Class_Reference/…
    – Wyck
    Commented Dec 7, 2011 at 18:00
  • I agree with this approach in general, especially if you're doing a mass import. It should be a trivial task to set a custom field with the last word of the title when importing your posts. Commented Dec 7, 2011 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.