1

I have a query that loads values from the database based upon a province a user lives:

$query = "SELECT * FROM $wpdb->usermeta WHERE meta_key='provincie' AND ( ".$provincie_check.")";        

This loads all provinces that a user holds. Based upon this I load the users:

for($p=0; $p <count($personen); $p++){
    $persoon = $personen[$p];
    if ($p % 2 == 0){
            $oddeven = 'even';
        }else{
            $oddeven = 'odd';
        }
        $id = $persoon->user_id;
        $user_info = get_userdata($id);
        $p_fname = get_user_meta($id, 'first_name', true); 
        $p_fname = array($p_fname);
        sort($p_fname);
}

For every user I create a table to view them. I want them sorted upon ther first name. So I thought I create an array of all first names and sort that. But no luck.

How can I sort the for-loop to view my users sorted by their first name?

4
  • This should be throwing an error AND ( ".$provincie_check."). Unless you've got something for it that will valid syntax. Commented Apr 9, 2015 at 13:51
  • Nope no error you just missed a part of the code that provides $province_check it results 'meta_value' LIKE '%province%' Commented Apr 9, 2015 at 13:53
  • I didn't "miss" it, you didn't provide that important bit of code in your question, that's why I said that. Commented Apr 9, 2015 at 13:54
  • I didn't say that you missed it by overreading it but missed it because I didn't provide it. However it's there now :-) Commented Apr 9, 2015 at 13:57

1 Answer 1

1

Why you don`t extract them directly with SQL?

$query = "SELECT user_id, meta_value as name 
          FROM $wpd->usermeta 
          WHERE meta_key 
          LIKE 'first_name' 
          AND user_id 
          IN (SELECT user_id 
              FROM $wpdb->usermeta 
              WHERE meta_key='provincie' 
              AND ( ".$provincie_check.")) 
              ORDER BY name";  
Sign up to request clarification or add additional context in comments.

2 Comments

I like this. I will take a look. Thank you
This did the trick for me. However can you explain why I should change the * with theuser_id and meta_value. I thought they were used to get those values and nothing else

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.