Skip to content

Instantly share code, notes, and snippets.

@rustam-swe
Last active September 30, 2020 11:23
Show Gist options
  • Save rustam-swe/4898e0a8b4ec05571a1410bf2848ff34 to your computer and use it in GitHub Desktop.
Save rustam-swe/4898e0a8b4ec05571a1410bf2848ff34 to your computer and use it in GitHub Desktop.
<?php
/**
* Gets all products by category ID
* Original source: https://wordpress.stackexchange.com/questions/143582/get-the-product-list-of-a-given-category-id
*/
<ul class="relatedlinks">
<?php
$args = [
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'terms' => $product_category_id
]
]
];
$related_products = new WP_Query($args);
if ($related_products) {
while ($related_products->have_posts()):
$related_products->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_postdata();
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment