I Try to get all products with the same ean code, but the query doesn't work and also doesn't give any errors. What am I missing? Here is the code I use.

if (is_product()) {
    global $product;
    $ean = $product->get_attribute('EAN');

    // Build query
    $query = new WP_Query(array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'orderby'        => 'meta_value_num',
        'meta_type'      => 'NUMERIC',
        'meta_key'       => '_price',
        'order'          => 'ASC',
        'tax_query'      => array(
            'relation' => 'OR',
                'taxonomy' => 'ean',
                'field'    => 'name',
                'terms'    => explode(',', $ean),
                'operator' => 'IN',
    if ($query->have_posts() && $query->found_posts > 0) {
    else {
      echo 'No related products found.';
    // Output the final query
    echo $query->request;
    // Check for query errors
    if ($query->is_main_query() && $query->is_tax() && empty($query->posts)) {
       echo 'Query error: ' . $query->get('error');

2 Answers 2


It looks like you want to list products with post_type=product etc.. AND have the same ean code. Therefore, in your place i would try with the line 'relation' => 'OR' changed to 'relation' => 'AND'


  • post_type is separate from the tax_query, and the relation here is part of the tax_query; they've only got one item in the tax_query so the relation is probably unnecessary.
    – Pat J
    Dec 20, 2023 at 13:40
  • exactly, the relation here is unnecessary. But I see when I remove the tax_query, that I get 890 results back
    – apero
    Dec 21, 2023 at 10:37

I found the solution to my problem. To query products apparently you shouldn't use WP_Query(), but instead use wc_get_products(). By doing this I am able to query products and get results. More info can be found here: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query#wc_product_query-methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.