1. How to add new custom fields in batches
insert into wp_postmeta (post_id, meta_key, meta_value)
select ID, '新自定义字段', '自定义字段值' from wp_posts where post_type = 'post';
2. Modify custom field names in batches
UPDATE 'wp_postmeta' SET 'meta_key'='新的自定义域名称' WHERE 'meta_key' = '旧的自定义域名称';
3. Remove duplicate custom fields
1. By directly modifying the database command
delete from wp_postmeta
where meta_id in (
select *
from (
select meta_id
from wp_postmeta a
where a.meta_key = 'views'
and meta_id not in (
select min(meta_id)
from wp_postmeta b
where b.post_id = a.post_id
and b.meta_key = 'views'
)
) as x
);
Notice:
views represents the field you want to modify, wp_posta is a table in the database, and the custom field is stored in this table. Please pay attention to the previous wp_, which is the prefix that WordPress sets when creating the database. If your prefix is not wp _ instead of bb _, then wp_ postmeta needs to be changed to bb _postmeta
2. Use php code to remove duplicate fields
If you have no way to operate the database through phpMyAdmin, then you can use the following method.
1. Create a new file named remove-duplicate-custom-fields.php in the root directory of the website, copy the following code to the file, and save:
<?php
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
define( 'WP_DEBUG_DISPLAY', true );
ini_set( 'display_errors', true );
$allposts = get_posts('numberposts=-1&post_type=post&post_status=any');
$keys = array('views','test_meta');//要检索的自定义字段
foreach ( $keys as $key ) {
foreach( $allposts as $postinfo) {
// 获取(上面所填写的)自定义字段的值
$postmeta = get_post_meta($postinfo->ID, $key);
if (!empty($postmeta) ) {
// 删除这篇文章的(上面所填写的)自定义字段
delete_post_meta($postinfo->ID, $key);
// 插入一个且只有一个(上面所填写的)自定义字段
update_post_meta($postinfo->ID, $key, $postmeta[0]);
}
}
}
?>
Pay attention to modify the fields in line 8. In this example, the two fields 'views' and 'test_meta' are deleted, please modify them yourself (multiple fields are separated by half-width English commas).
2. Access http://your domain name/remove-duplicate-custom-fields.php through the browser, wait for a while, and the redundant duplicate fields can be deleted!