Forums

[resolved] MYSQL Query for sorting wordpress usermeta data (5 posts)

  1. estern23
    Member
    Posted 1 year ago #

    Hi i am trying to do a query against the wordpress usermeta database to get all users that have a custom meta_name and value assigned. The custom name/value is a score (integer). I need to bring back the users with the highest score and limit the query to 5 results.

    I have a working statement but it doest want to sort them properly.

    $gather_top_users = "SELECT * FROM ".$wpdb->prefix."usermeta WHERE meta_key='points' ORDER BY meta_value DESC LIMIT 5 ";

    Any ideas why this is not properly sorting them in the array?

  2. MichaelH
    Volunteer
    Posted 1 year ago #

    Try

    ORDER BY meta_value+0
  3. estern23
    Member
    Posted 1 year ago #

    Thank you, I will test this out later.

    What is the logic behind adding the "+0" to the order?

  4. MichaelH
    Volunteer
    Posted 1 year ago #

    Makes it act like a number instead of a string.

  5. estern23
    Member
    Posted 1 year ago #

    Awesome, thanks again for your help

Topic Closed

This topic has been closed to new replies.

About this Topic