admin管理员组文章数量:1426459
Delete a custom field in mysql for all posts with specific category id
I am trying to delete all custom fields with the key 'key1' from posts that are in category id '66'.
I can delete all custom fields with the key 'key1' with the following mysql query:
DELETE FROM wp_postmeta
WHERE meta_key = 'key1'
How do I specify the category in that query?
Delete a custom field in mysql for all posts with specific category id
I am trying to delete all custom fields with the key 'key1' from posts that are in category id '66'.
I can delete all custom fields with the key 'key1' with the following mysql query:
DELETE FROM wp_postmeta
WHERE meta_key = 'key1'
How do I specify the category in that query?
Share Improve this question asked Jun 11, 2019 at 16:40 PhilipPhilip 1132 silver badges11 bronze badges1 Answer
Reset to default 0Please try with below code :
$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();
本文标签: Delete a custom field in mysql for all posts with specific category id
版权声明:本文标题:Delete a custom field in mysql for all posts with specific category id 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411687a2657498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论