DAC*_*sby 5 mysql sql wordpress advanced-custom-fields
一些背景故事(与我的问题没有直接关系,但也许其他人可以使用我的方法)
我正在使用插件高级自定义字段在WordPress v3.9.1中工作.我已经从WordPress中需要格式化的旧数据库中导入了自定义值的CSV文件(使用WP Ultimate CSV Importer插件,免费版本),但有一个例外 - ACF Repeater Fields.该插件很棒,但还没有一种很好的导入数据的方法.
Repeater字段存储在数据库中,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
那是; 对于每个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
Run Code Online (Sandbox Code Playgroud)
我需要补充一下
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
简而言之
我该怎么做:
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
Run Code Online (Sandbox Code Playgroud)
**临时解决方案**
目前,我只是使用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 />';
}
Run Code Online (Sandbox Code Playgroud)
最简单的方法是导入隐藏的自定义字段(前导下划线)以及元值。如果您只想清理数据,您可以使用 aUNION SELECT
作为您的输入INSERT
- 您不需要meta_id
,它将自动分配。请注意,在执行插入操作之前,此 SQL 不会检查数据是否已存在,因此请确保您不会受到欺骗。
您可以通过查询所有带有“床”的条目来获取post_id
您需要的信息。使用此功能,您可以对其他值进行硬编码,并将它们别名为语句中的列,然后将其用于.wp_postmeta
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
Run Code Online (Sandbox Code Playgroud)
您还可以将其作为三个不同的INSERT
语句运行,而无需UNION
.
归档时间: |
|
查看次数: |
586 次 |
最近记录: |