Skip to content

Instantly share code, notes, and snippets.

@SitesByYogi
Created December 27, 2024 12:04
Show Gist options
  • Save SitesByYogi/7a18ed230d821bf4c4e190fc94f05479 to your computer and use it in GitHub Desktop.
Save SitesByYogi/7a18ed230d821bf4c4e190fc94f05479 to your computer and use it in GitHub Desktop.
Creates a list of all published product URLs on your Website
<?php
function list_all_product_and_category_urls() {
// Get all products
$products = wc_get_products(array(
'limit' => -1, // Retrieve all products
'status' => 'publish' // Only published products
));
// Get all categories
$categories = get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => false, // Include empty categories
));
// Begin output
echo '<h2>Product URLs</h2>';
echo '<ul>';
foreach ($products as $product) {
echo '<li><a href="' . esc_url(get_permalink($product->get_id())) . '">' . esc_html($product->get_name()) . '</a></li>';
}
echo '</ul>';
echo '<h2>Category URLs</h2>';
echo '<ul>';
foreach ($categories as $category) {
echo '<li><a href="' . esc_url(get_term_link($category)) . '">' . esc_html($category->name) . '</a></li>';
}
echo '</ul>';
}
// Use shortcode to display the list
add_shortcode('product_and_category_urls', 'list_all_product_and_category_urls');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment