SOLVED: Get related products (comma into category name)

I try create query to get related products.
From this tutorial: https://www.youtube.com/watch?v=_9nCps8EeHM
Every works fine, but not to categories which contains “comma” into name.
As a result of the error, the products are not displayed.

I use filter to display query vars.
As You can see category name is changed.
Comma and everything on the right side is removed.
If I change comma to dot, or remove comma, everything works fine.

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
	echo'<pre>'; var_dump( $query_vars ); echo'</pre>';
	return $query_vars;
}, 10, 3 );

If taxonomy name not contain comma, everything works fine:

This is screen if taxonomy contains comma:

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

Hi Marek,

We fixed this issue in Bricks 2.0 alpha, now available as a manual download in your account (see changelog).

Please let us know if you are still experiencing issues.

As with any pre-stable release, please do not use it on a production/live website. It is only meant for testing in a local or staging environment.