Here I’ve written a function for you-
function the_dramatist_get_product_cat_posts( $args = array() ) {
$default_args = array(
'taxonomy' => 'product_cat',
'get_post_type' => 'post'
);
$t = array();
$param = wp_parse_args($args, $default_args);
$prod_terms = get_the_terms(get_the_ID(), $param['taxonomy']);
foreach ($prod_terms as $prod_term ) {
$t[] = $prod_term->term_id;
}
$posts = get_posts(array(
'post_type' => $param['get_post_type'],
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => $param['taxonomy'],
'field' => 'id',
'terms' => $t,
'include_children' => false
)
)
));
return $posts;
}
By calling this function like the_dramatist_get_product_cat_posts()
you’ll get all the posts related to current product’s product_cat
taxonomy terms. It will give you all the post object. Then you can run a foreach
or for
loop to organize the posts as you like. You can temporarily do a print_r
on this function to understand the returned data structure. It’ll help you to understand the data.
Hope that helps.