最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Delete a custom field in mysql for all posts with specific category id

programmeradmin3浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please 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();
发布评论

评论列表(0)

  1. 暂无评论