2014-07-14 42 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运行此。