2011-09-24 45 views
0

我正在編寫一個腳本,它將以可讀的佈局提取wp_usermeta。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導出?

UPDATE 25/SEP/2011,12:34 pm: 無所謂傢伙,我發現我的答案參考下面的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 

希望這有助於人們尋找這個解決方案!

+0

呃...你一定需要一個純粹的SQL解決方案?爲什麼不使用WP API呢? – vzwick

+0

我想用get_user_meta作爲WP API,並且生成到csv不起作用。 – user952543

回答

2

你真的不應該直接在WordPress中使用SQL,因爲它們提供了許多功能來爲你完成工作。這樣做也會使您的代碼更靈活,以便將來升級WordPress。

你的情況,你有興趣get_user_meta()

+0

我有一個腳本,這樣做,所以我怎麼用這個與Wordpress API?我已將它放在模板文件夾中,但似乎不起作用。我是否需要創建頁面並將模板名稱註釋放在頂部?此外,我嘗試使用get_user_meta在實際頁面本身上使用csv導出函數,但它以字符串的形式回顯出來,而不是輸出到csv中。有沒有將user_meta導出到csv的解決方案? – user952543