1. 새 사용자 지정 필드를 일괄 추가하는 방법
insert into wp_postmeta (post_id, meta_key, meta_value)
select ID, '新自定义字段', '自定义字段值' from wp_posts where post_type = 'post';
2. 사용자 지정 필드 이름을 일괄 수정
UPDATE 'wp_postmeta' SET 'meta_key'='新的自定义域名称' WHERE 'meta_key' = '旧的自定义域名称';
3. 중복 사용자 지정 필드 제거
1. 데이터베이스 명령을 직접 수정하여
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
);
알아채다:
views는 수정할 필드를 나타내고, wp_posta는 데이터베이스의 테이블이며, 사용자 정의 필드는 이 테이블에 저장됩니다. bb _ 대신 wp _ 가 아닌 경우 wp_ postmeta를 bb _postmeta 로 변경해야 합니다.
2. PHP 코드를 사용하여 중복 필드 제거
phpMyAdmin을 통해 데이터베이스를 운영할 방법이 없다면 다음과 같은 방법을 사용할 수 있습니다.
1. 웹사이트의 루트 디렉터리에 remove-duplicate-custom-fields.php라는 새 파일을 만들고 다음 코드를 파일에 복사한 후 저장합니다.
<?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]);
}
}
}
?>
8행의 필드를 수정하는 데 주의하십시오. 이 예에서는 두 개의 필드 'views' 및 'test_meta'가 삭제되었습니다. 직접 수정하십시오(여러 필드는 반각 영어 쉼표로 구분됨).
2. 브라우저를 통해 http://your domain name/remove-duplicate-custom-fields.php에 접속하고 잠시 기다리면 중복된 중복 필드를 삭제할 수 있습니다!