15

How to Find and Count Orphaned WooCommerce Product Variations

Powered by WPCodeBox

This snippet queries the WordPress database to find and count orphaned WooCommerce product variations, which are product variations whose main product has been deleted.


<?php

// This snippet should only run in "Manual" mode.

global $wpdb;

$orphaned_variations_results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} o LEFT OUTER JOIN {$wpdb->posts} r ON o.post_parent = r.ID WHERE r.id IS null AND o.post_type = 'product_variation'");

$ov_count = count($orphaned_variations_results);

echo "There are $ov_count variations on your site.";

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.