Last active
December 4, 2017 10:01
-
-
Save damiencarbery/ffe8783e2a4e6744752b475fa4cc98b9 to your computer and use it in GitHub Desktop.
List WooCommerce Products that do not have any categories assigned.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Asked in Advanced WooCommerce group. | |
// https://www.facebook.com/groups/advanced.woocommerce/permalink/1907439122603831/ | |
// Restrict who can access this script. | |
$permitted_ips = array('12.34.56.78', ); | |
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) { | |
header('HTTP/1.0 403 Forbidden'); | |
die(); | |
} | |
?> | |
<?php $time_start = microtime(true); ?> | |
<!DOCTYPE html> | |
<html lang="en-US"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>List Products Without A Category</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
</head> | |
<body> | |
<h1>List Products Without A Category</h1> | |
<?php | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment and Template */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
$prods_args = array( | |
'post_type' => 'product', | |
'no_found_rows' => true, // Skips SQL to count rows - a speed improvement. | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => true, // Don't move sticky posts to top - a speed improvement. | |
'posts_per_page' => -1, | |
'fields' => 'ids', // Only return product IDs | |
); | |
$pq = new WP_Query( $prods_args ); | |
// No need for a loop - retrieve all the IDs in one go. | |
$product_ids = $pq->posts; | |
if ( $pq->have_posts() ) { | |
echo '<ul>'; | |
foreach ( $product_ids as $id ) { | |
$product_cats = wp_get_post_terms( $id, 'product_cat', array("fields" => "ids") ); | |
if ( 0 == count( $product_cats ) ) { | |
printf( '<li><a href="%s%d&action=edit">%s</a></li>', admin_url( 'post.php?post='), $id, get_the_title( $id ) ); | |
} | |
} | |
echo '</ul>'; | |
} | |
// Some stats for fun. | |
echo '<p>Memory usage: ', intval(memory_get_usage() / (1024 * 1024)), "MB\n"; | |
echo '<br />Peak memory usage: ', intval(memory_get_peak_usage() / (1024 * 1024)), "MB</p>\n"; | |
$time_end = microtime(true); | |
$time = $time_end - $time_start; | |
echo "<p>Process Time: {$time} seconds.</p>"; | |
?> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$prods_args = array( | |
'post_type' => 'product', | |
'no_found_rows' => true, // Skips SQL to count rows - a speed improvement. | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => true, // Don't move sticky posts to top - a speed improvement. | |
'posts_per_page' => -1, | |
'fields' => 'ids', // Only return product IDs | |
); | |
$pq = new WP_Query( $prods_args ); | |
// No need for a loop - retrieve all the IDs in one go. | |
$product_ids = $pq->posts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment