0

I've added two new meta_keys to the wp_usermeta table:

bid_user_lat and bid_user_lng

And I need to be able to get both of these values within a single row, along with the user_id.

ROW - (user_id) 220 | (bid_user_lat) 45.099080 | (bid_user_lng) -140.09099

Now comes the tricky part ... I then need to use these values within a calculated radius statement:

( 3959 * acos( cos( radians( @lat ) ) * cos( radians( **bid_user_lat.meta_value** ) ) * cos( radians( **bid_user_lng.meta_value** ) - radians( @lng ) ) + sin( radians( @lat ) ) * sin( radians( **bid_user_lat.meta_value** ) ) ) ) AS distance

In the end, I'm making sure that the user is within a certain radius, and if so, I will return it within the query.

But the most important part is being able to query the wp_usermeta the way I need to!

1 Answer 1

1

You can use the WP_User_Query class which works much like WP_Query.

The docs: http://codex.wordpress.org/Class_Reference/WP_User_Query

Below is a dump of the WP_User object that it will return, in this example using:

$wp_user_search = new WP_User_Query( array( 'fields' => 'all_with_meta' ) );
$get_users = $wp_user_search->get_results(); 

user var_dump

This should set you in the right direction because it was not obvious how your new meta_keys are set up and exactly how you want to run the query. The second question isn't really related to WordPress :)

If you don't want to use WP_User_Query you will have to use $wpdb which is more direct mySQL query.

it would be something like:

global $wpdb;
$get_map_user = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'bid_user_lat'");

It would probably be better if you had one meta_key with 2 values for your long/lat, instead of 2 meta keys.

http://codex.wordpress.org/Class_Reference/wpdb

2
  • Thanks for responding! I've now combined the two meta_key entries into one serialized cell. But now I'm not sure how to extract the data from a serialized string ...
    – dcolumbus
    Commented Oct 18, 2012 at 20:49
  • That is more a PHP question, you can use unserialize or a WordPress's function called maybe_unserialize . It would be best to close this question and ask a new one with a specific example.
    – Wyck
    Commented Oct 18, 2012 at 20:57

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.