一些背後的故事(不是直接到我的問題有關,但也許別人可以使用我的方法)插入多個靜態值
我使用的是高級插件在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 />';
}