• Hey,

    I’m trying to get a slick way of ordering by lastname.
    Here’s what I’ve got so far:
    $author_subscriper = $wpdb->get_results(“SELECT * from $wpdb->usermeta”);
    Now, how do I do this and sort by last name?

    Thanks!
    Asaf

Viewing 5 replies - 1 through 5 (of 5 total)
  • popper

    (@julialasarte)

    Have your tried ORDER BY last_name?

    Thread Starter Asaf Cohen

    (@asaf050)

    Yes I tried, the problem is that there is no column named “last_name”.
    There is meta_key clolum that the value his’last_name’ and his ‘meta_value” is tha last name.
    for example:
    umeta_id.user_id…meta_key..meta_value
    5……………5……..last_name..Jacson
    21………….1……..last_name..Weston
    27………….3……..first_name.Bill
    30………….3……..last_name..Gates

    popper

    (@julialasarte)

    You’re totally right. Completely forgot how the tables where constructed! Sorry about that. How about something like this?

    SELECT * FROM wp_users INNER JOIN wp_usermeta ON (wp_users.id = wp_usermeta.user_id) WHERE meta_key ="last_name" ORDER BY meta_value

    I don’t know exactly what you need, but that’ll give you the user fields ordered by the user last name.

    Thread Starter Asaf Cohen

    (@asaf050)

    Thanks for your comment, INNER JOIN isn’t needed beacuse I’m currectly using the user meta table.
    This is my query right now:
    $author_subscriper = $wpdb->get_results(“SELECT * from $wpdb->usermeta WHERE meta_key = ‘wp_capabilities’ AND meta_value = ‘a:1:{s:10:\”subscriber\”;b:1;}’ AND WHERE meta_key =’last_name’ ORDER BY ‘meta_value'”);

    Isn’t working for me… (It doesn’t show result at all)
    Looking forward for your replay, thanks 🙂

    Thread Starter Asaf Cohen

    (@asaf050)

    Get it work! thank you very much!
    this is my query now:

    $author_subscriper = $wpdb->get_results(“SELECT * from $wpdb->usermeta WHERE meta_key = ‘last_name’ ORDER BY BINARY meta_value”);

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Custom mysql query’ is closed to new replies.