Wordpress users and usermeta - joining multiple rows in one table to one row in another table

前端 未结 3 1219
广开言路
广开言路 2021-01-31 05:00

I want to create a view from both the wp_users and wp_usermeta tables so that I can query rows in the view from an external application. Basic auth details are stored in wp_user

3条回答
  •  北海茫月
    2021-01-31 05:57

    I found that Toote's solution works but is a performance hog on a MySQL database taking about 17 seconds to execute a "select * from users_with_meta_view".

    I got this down to 0.0054 sec with a view structured like this instead:

    CREATE OR REPLACE VIEW users_with_meta_view AS
    SELECT
        u.id,
        u.user_login AS login,
        u.user_pass AS password,
        u.user_email AS email,
        (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,
        (select meta_value from wp_usermeta where user_id = u.id and meta_key = 'country' limit 1) as country
    FROM wp_users u
    

提交回复
热议问题