为具有特定类别id的所有帖子删除mysql中的自定义字段
我正在尝试删除所有自定义字段与键'key1‘从帖子,是在类别id '66’。
使用下面的mysql查询,我可以使用'key1‘键删除所有自定义字段:从wp_postmeta中删除meta_key = 'key1’
如何在查询中指定类别?
发布于 2019-06-12 05:08:02
请尝试使用以下代码:
$pro_args = [];
$pro_args['post_type'] = 'post'; //Replace your post type
$pro_args['post_status'] = 'publish';
$pro_args['posts_per_page'] = -1;
$pro_args['tax_query'] = array(
array(
'taxonomy' => 'category', //Replace your taxonomy name
'field' => 'id',
'terms' => array( 66 ),
)
);
// Optional
$pro_args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'key1',
'compare' => 'EXISTS',
)
);
$pro_query = new WP_Query( $pro_args );
if ( $pro_query->have_posts() ) {
while ( $pro_query->have_posts() ) { $pro_query->the_post();
delete_post_meta( get_the_ID(), 'key1' );
}
}
wp_reset_postdata();https://wordpress.stackexchange.com/questions/340201
复制相似问题