【问题标题】:Remove duplicate post variation in WooCommerce删除 WooCommerce 中重复的帖子变体
【发布时间】:2022-11-07 10:25:46
【问题描述】:

需要一个查询来删除所有产品中类似这种情况的重复帖子变体:

【问题讨论】:

  • 欢迎来到堆栈溢出!您的问题缺少从Stack Overflow 用户那里获得任何帮助的信息。请收下tour,并通读help center,了解How to Ask 好问题吗?最大限度地提高您获得问题答案的机会。如果您遇到特定问题并且遇到困难,请发送问题描述,包括minimal reproducible example,人们会很乐意为您提供帮助。

标签: woocommerce duplicates product-variations


【解决方案1】:

只需将此代码添加到活动主题的functions.php 文件中即可加载产品编辑页面,然后删除代码。请在下面的代码中更改产品 ID。

<?php if (isset( $_GET['post'])) {
    
$current_list = array();
$args = array(
        'posts_per_page' => 500,
        'post_type' => 'product_variation',
        'orderby' => 'title',
        'order' => 'asc',
        'offset' => 0,
        'ID' => 1631819251, //Parent ID
      );
      $the_query = new WP_Query( $args );
      
      if ($the_query->have_posts()) {
      while ( $the_query->have_posts() ) :
        $the_query->the_post(); ?>
        <?php the_title(); ?><br>
        <?php $this_variation_title = get_the_title();
              if (in_array($this_variation_title, $current_list)) {
                 echo $this_variation_title.' duplicate deleted.<br>';
                 wp_delete_post(get_the_ID());
              } else {
                  array_push($current_list, $this_variation_title);
                  echo 'Variation added to array.<br>';
              } ?>
<?php endwhile; ?>
<?php } ?>
<?php wp_reset_postdata();
} ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多