一些背后的故事(不是直接到我的问题有关,但也许别人可以使用我的方法)插入多个静态值
我使用的是高级插件在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 />';
}