【问题标题】:Remove duplicate post variation in WooCommerce删除 WooCommerce 中重复的帖子变体
【发布时间】:2022-11-07 10:25:46
【问题描述】:
需要一个查询来删除所有产品中类似这种情况的重复帖子变体:
【问题讨论】:
标签:
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();
} ?>