0

I have a query where I want to merge values from the wp_users and wp_meta tables, including the meta values of a given meta key. This key could have multiple entries.

SELECT
    u.id,
    (select meta_value from wp_usermeta where user_id = u.id and meta_key = 'first_name' limit 1) as first_name,
    (select meta_value from wp_usermeta where user_id = u.id and meta_key = 'last_name' limit 1) as last_name,
    u.user_email AS email,
    u.user_registered AS registered,
    (select meta_value from wp_usermeta where user_id = u.id and meta_key = '_example_key') as my_custom_meta_values
FROM wp_users u

This query returns a single column (my_custom_meta_values), containing all the serialised values from wp_usermeta for the given key.

My question is, how do I then loop over these values in PHP? I'm assuming that simply deserialising the row will fail.

1 Answer 1

0

It would appear simply un-serialising works:

        $results =  $wpdb->get_results( $sql_str );

        foreach($results as &$result) {

            $result-> my_custom_meta_values = unserialize($result-> my_custom_meta_values);
        }

If anyone can suggest a more efficient way to do this, I'd be interested.

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.