2014-07-14 118 views
5

一些背後的故事(不是直接到我的問題有關,但也許別人可以使用我的方法)插入多個靜態值

我使用的是高級插件在WordPress v3.9.1工作自定義字段。我已經從舊數據庫中導入了一個自定義值的CSV文件(使用WP Ultimate CSV Importer插件,免費版),這是我在WordPress中需要的格式,但有一個例外--ACF Repeater Fields。這個插件非常棒,但是還沒有一個導入大量數據的好方法。

中繼字段存儲在數據庫中,如下所示:

meta_id post_id meta_key   meta_value 
3894  4697 beds    2 
3895  4697 _beds    field_53bcfe244a98d 
4051  4697 _beds_0_other  field_53c2273053218 
4050  4697 beds_0_other  1 
4051  4697 _beds_1_other  field_53c2273053218 
4050  4697 beds_1_other  3 

5894  4698 beds    2 
5895  4698 _beds    field_53bcfe244a98d 
5051  4698 _beds_0_other  field_53c2273053218 
5050  4698 beds_0_other  1 
5051  4698 _beds_1_other  field_53c2273053218 
5050  4698 beds_1_other  3 

即;爲每個post_id有一個名爲「牀」的Repeater字段。 「在」中繼器字段是1字段,重複兩次。每個字段有2個數據庫條目 - 一個字段引用(用於管理保存字段 - 總是每個字段相同)和一個值。不像設置那麼直觀,但它是圍繞WordPress的默認表系統設計的。

實際問題

現在,我有我的舊數據庫導入字段是這樣的:

meta_id post_id meta_key   meta_value 
####  4697 beds    2 
####  4697 beds_0_other  1 
####  4697 beds_1_other  3 

####  4698 beds    2 
####  4698 beds_0_other  1 
####  4698 beds_1_other  3 

我需要添加

meta_id post_id meta_key   meta_value 
####  4697 _beds    field_53bcfe244a98d 
####  4697 _beds_1_other  field_53c2273053218 
####  4697 _beds_0_other  field_53c2273053218 

####  4698 _beds    field_53bcfe244a98d 
####  4698 _beds_1_other  field_53c2273053218 
####  4698 _beds_0_other  field_53c2273053218 

meta_key和meta_value是靜態 - 它們永遠不會改變(在創建字段後,它會保留相同的字段_ ##直到刪除)。 meta_id是自動增量。

問題是我有200多個post_id值,每個值需要50個靜態條目 - 不想硬編碼。我可以通過選擇所需的ID如下:

SELECT DISTINCT ID 
FROM `wp_posts` 
WHERE post_type = "community" 

// Returns: 
post_id 
4697 
4698 

在短期

我怎樣才能做到以下幾點:

INSERT INTO `table` (`meta_id`, `post_id`, `meta_key`, `meta_value`) 
VALUES 
// foreach related distinct ID in wp_posts 
(NULL, 'ID', "_beds", "field_53bcfe244a98d"), 
(NULL, 'ID', "_beds_0_other", "field_53c2273053218"), 
(NULL, 'ID', "_beds_1_other", "field_53c2273053218") 
// end foreach 

**臨時解決方案**

對於目前,我只是傾銷所有使用PHP &將數據上傳到PHPMyAdmin中的數據。可以編寫一個插入的PHP循環,但是我正在尋找一種我可以使用的MySQL解決方案,而無需上傳新的php文件(或sql)。

$ids = array("4697", "4698"); 

echo ' 
INSERT INTO `table` (`meta_id`, `post_id`, `meta_value`, `meta_key`) 
VALUES<br />'; 
foreach ($ids as $id){ 
    echo ' 
(NULL, "'. $id .'", "1", "beds"), 
(NULL, "'. $id .'", "field_53bcfe244a98d", "_beds"), 
(NULL, "'. $id .'", "field_53c2273053218", "_beds_0_other"), 
<br />'; 

} 

回答

1

最簡單的方法就是導入隱藏的自定義字段(前導下劃線)以及元值。如果您只想清理數據,您可以使用UNION SELECT作爲INSERT的輸入 - 您不需要meta_id,它會自動分配。請注意,在執行插入操作之前,此SQL不會檢查數據是否已經存在,因此請確保您不會以愚蠢的方式結束。

您可以通過查詢wp_postmeta獲得所需的post_id所有meta_key「牀」的條目。使用此post_id,您可以硬編碼其他值並將它們別名爲SELECT語句中的列,然後將其用於INSERT的值。

-- modify the wp_ prefix as needed 
INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`) 
-- get "_beds" key/value for all entries with meta key of "beds" 
SELECT post_id, '_beds' AS meta_key, 'field_53bcfe244a98d' AS meta_value 
FROM wp_postmeta WHERE meta_key = 'beds' 
UNION 
-- get "_beds_0_other" key/value for all entries with meta key of "beds" 
SELECT post_id, '_beds_0_other' AS meta_key, 'field_53c2273053218' AS meta_value 
FROM wp_postmeta WHERE meta_key = 'beds' 
UNION 
-- get "_beds_1_other" key/value for all entries with meta key of "beds" 
SELECT post_id, '_beds_1_other' AS meta_key, 'field_53c2273053218' AS meta_value 
FROM wp_postmeta WHERE meta_key = 'beds' 
-- order results (you can view what will be inserted if you run the SELECT without the INSERT) 
ORDER BY post_id 

你也可以爲三個不同的INSERT聲明沒有UNION運行此。