WooCommerce Get All Products With No Tags

Hi Guys,

In WooCommerce, you can get all the products with no tags.
If you want to get all products without tags, you can use the following code snippet:
/******Start of the code******/


<?php
include 'wp-load.php';
global $wpdb;
$srNo = 1;
$productWithNoTagsArr = array();
$productIdsResults = $wpdb->get_results("SELECT ID, post_title, post_modified, meta_value as sku FROM " . $wpdb->prefix . "posts as wp left join " . $wpdb->prefix . "postmeta as wpm on wp.ID = wpm.post_id WHERE post_status='publish' AND post_type='product' AND wpm.meta_key='_sku'", ARRAY_A);

foreach ($productIdsResults as $productId) {
$prodId = $productId['ID'];
$terms = get_the_terms($prodId, 'product_tag');
$term_array = array();
if (!empty($terms) && !is_wp_error($terms)) {
foreach ($terms as $term) {
$term_array[] = $term->name;
}
}
if (empty($term_array)) {
$productWithNoTagsArr[$prodId]['id'] = $prodId;
$productWithNoTagsArr[$prodId]['name'] = $productId['post_title'];
$productWithNoTagsArr[$prodId]['sku'] = $productId['sku'];
$productWithNoTagsArr[$prodId]['last_updated'] = $productId['post_modified'];
}
}
?>

<h1 class="wp-heading-inline"><?php echo esc_html('Products With No tags'); ?></h1>
<hr class="wp-header-end"><br>
<table id="products_without_tags" cellpadding="5" cellspacing="5" width="100%">
<thead>
<tr>
<th width="3%" style="text-align:center">Sr. No.</th>
<th width="9%" style="text-align:center">Product ID</th>
<th width="15%">SKU</th>
<th width="31%">Product Name</th>
</tr>
</thead>
<tbody>
<?php
foreach ($productWithNoTagsArr as $row) {
$entityID = $row['id'];
$prdName = $row['name'];
$sku = $row['sku'];
?>
<tr>
<td style="text-align:center"><?php echo $srNo; ?></td>
<td style="text-align:center"><?php echo $entityID; ?></td>
<td style="text-align:center"><?php echo $sku; ?></td>
<td><?php echo $prdName; ?></td>
</tr>
<?php $srNo++;
}
?>
</tbody>
</table>

/******End of the code******/
That’s it you are done!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s