2

I'm currently showing the products of one WooCommerce product category (events) with the following code:

<?php $args = array( 'post_type' => 'product', 'product_cat' => 'events');
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

Is it possible to get a product category by name instead of slug or ID?

Since I'm using a translation plugin (WPML) slugs have been added per language (for example "events_eng").

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

2

You could use another trick is to get the language extension used by WPML in the category slug and then to add it to your slug, this way:

<?php 
    // Set here your category slug
    $cat_slug = 'events';

    // Get the current language
    $lang = explode("-", get_bloginfo('language'));

    // Adding the language extension to the slug
    $cat_slug .= '_' . $lang[0];

    $args = array( 'post_type' => 'product', 'product_cat' => $cat_slug);
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>

If for the main language you don't have a language extension in the category slug, you can add a condition in this code to avoid adding it to this particular language.

This code is tested and it's fully functional.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399