Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
    'posts_per_page'=>50,
    'cat'=> '39,-41',
    'orderby'=>'title',
    'order'=>'ASC',
);
query_posts($args);

how can i get that second word order?

best, Dan.

share|improve this question
    
Great question—wish I could upvote it twice. –  supertrue Sep 6 '12 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 Sep 18 '12 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(
        LTRIM({$wpdb->posts}.post_title), 
        LOCATE(' ',LTRIM({$wpdb->posts}.post_title)))";

}
share|improve this answer
    
thanks - but how would i apply that to only this query_posts? D. –  daniel Crabbe Dec 7 '11 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. –  goldenapples Dec 7 '11 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 Apr 8 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.

share|improve this answer
    
+1 for using a custom field –  Geert Dec 7 '11 at 16:09
    
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... –  daniel Crabbe Dec 7 '11 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 Dec 7 '11 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. –  goldenapples Dec 7 '11 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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