Wordpress提取wp_usermeta

时间:2011-09-24 14:07:50

标签: php wordpress

我正在编写一个脚本,它将以可读的布局提取wp_usermeta。

我有类似下面的SQL:

select distinct(user_id), meta_value as suburb from wp_usermeta where meta_key = 'suburb' order by user_id

现在有一种方法我们可以包含所有其他字段以及使用sql查询加入右边的列并将结果显示在一个表中以便我可以将其用于csv export吗?

更新25 / SEP / 2011,下午12:34: 无所谓,我发现我的答案参考下面的sql !!!

SELECT
    u.id, u.user_login,
    MIN(CASE m.meta_key WHEN 'title' THEN m.meta_value END) AS title,
    MIN(CASE m.meta_key WHEN 'first_name' THEN m.meta_value END) AS first_name,
    MIN(CASE m.meta_key WHEN 'last_name' THEN m.meta_value END) AS last_name,
    MIN(CASE m.meta_key WHEN 'suburb' THEN m.meta_value END) AS phone,
    MIN(CASE m.meta_key WHEN 'state' THEN m.meta_value END) AS state,
    MIN(CASE m.meta_key WHEN 'country' THEN m.meta_value END) AS country,
    MIN(CASE m.meta_key WHEN 'postcode' THEN m.meta_value END) AS postcode,
    MIN(CASE m.meta_key WHEN 'contact_no' THEN m.meta_value END) AS contact_no,
    MIN(CASE m.meta_key WHEN 'email' THEN m.meta_value END) AS email,
    MIN(CASE m.meta_key WHEN 'occupation' THEN m.meta_value END) AS occupation,
    MIN(CASE m.meta_key WHEN 'workplace' THEN m.meta_value END) AS workplace,
    MIN(CASE m.meta_key WHEN 'maternitybg' THEN m.meta_value END) AS maternitybg,
    MIN(CASE m.meta_key WHEN 'trainingdate' THEN m.meta_value END) AS trainingdate,
    MIN(CASE m.meta_key WHEN 'traininglocation' THEN m.meta_value END) AS traininglocation,
    MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
    MIN(CASE m.meta_key WHEN 'trainingyear' THEN m.meta_value END) AS trainingyear,
    MIN(CASE m.meta_key WHEN 'coltraining' THEN m.meta_value END) AS coltraining,
    MIN(CASE m.meta_key WHEN 'isinstructor' THEN m.meta_value END) AS isinstructor,
    MIN(CASE m.meta_key WHEN 'gender' THEN m.meta_value END) AS gender,
    MIN(CASE m.meta_key WHEN 'idf_indig_tsi' THEN m.meta_value END) AS idf_indig_tsi,
    MIN(CASE m.meta_key WHEN 'idf_ct_ld' THEN m.meta_value END) AS idf_ct_ld,
    MIN(CASE m.meta_key WHEN 'comments' THEN m.meta_value END) AS comments

    FROM wp_users u
      LEFT JOIN wp_usermeta m ON u.ID = m.user_id
        AND m.meta_key IN ('title', 'first_name', 'last_name', 'suburb', 'state', 'country', 'postcode', 'contact_no', 'email', 'occupation', 'workplace', 'maternitybg', 'trainingdate', 'traininglocation', 'coltraining', 'isinstructor', 'gender', 'idf_indig_tsi', 'idf_ct_ld', 'comments')
    GROUP BY u.ID

希望这可以帮助人们寻找这个解决方案!

1 个答案:

答案 0 :(得分:2)

你真的不应该直接在WordPress中使用SQL,因为它们提供了许多功能来为你完成工作。这样做还可以使您的代码保持灵活,以便将来升级WordPress。

在您的情况下,您对get_user_meta()

感兴趣