SOLVED: Get related products (comma into category name)

Hi @MarekKmiecik ,

Thanks for reporting the issue.

I can replicate this problem locally.

This is because the comma will be treated as a different term when used in the tax_query.

Unfoturnetaly, {post_terms_xxx} will use the category name instead of slug or ID.

We will discuss internally so you can output slug or ID when using this dynamic tag.

As a workaround, you can add this function in your child theme and add into echo white list, then use {echo:get_current_post_term_ids('product_cat')} inside the taxonomy query like this

function get_current_post_term_ids($taxonomy = 'category') {
  $post_id = get_the_ID();
  
  if (!$post_id) {
      return '';
  }
  
  $terms = get_the_terms($post_id, $taxonomy);
  
  if (empty($terms) || is_wp_error($terms)) {
      return '';
  }
  
  $term_ids = array_map(function($term) {
      return $term->term_id;
  }, $terms);
  
  return implode(',', $term_ids);
}

Regards,
Jenn

2 Likes