Get product category (Woocommerce) with the taxonomy field (ACF) to filter the loop

Currently, I am working on a website where I have to show posts with the category that is selected with the taxonomy field; I use Advanced Custom Fields for this. With “normal” (single) posts and custom post types, it works like a charm. To show how it works:

<?php
    // get the current taxonomy term
    $term = get_queried_object();

    $catact = get_field('actueel_category', $term);

    $loop = new WP_Query( array(
        'post_type' => 'actueel',
        'posts_per_page' => 2,
        'category__in' => $catact,
      )
    );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <a href="<?php the_permalink();?>">

        <div class="post">

          <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
          <div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
          </div>

          <div class="theme__inner__content">
            <h4><?php the_title();?></h4>
            <span class="more">lees meer</span>
          </div>

        </div>

      </a>

  <?php endwhile; wp_reset_query(); ?>

Now, when I try to do the same with Woocommerce Products, it doesn’t work. Here is the code I use for that:

<?php
  // get the current taxonomy term
  $term = get_queried_object();

  $catpro = get_field('product_category', $term);

    $loop = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => 2,
        'product_cat' => $catpro,
      )
    );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <a href="<?php the_permalink();?>">

        <div class="post">

          <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
          <div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
          </div>

          <div class="theme__inner__content">
            <h4><?php the_title();?></h4>
            <span class="more">lees meer</span>
          </div>

        </div>

      </a>

  <?php endwhile; wp_reset_query(); ?>

Is there something that I’m not getting?

In the Admin area: I use the taxonomy field, both outputs are being displayed as Term ID’s. For the regular post I’ve selected the “category” taxonomy and for the product the “product_cat” taxonomy.

Can someone think with me here? I can’t seem to solve it. Maybe I’m overlooking something.

Thanks in advance!

Solutions

You are using product_cat parameter in WP_Query. which is not supported.

Use category__in in product loop args.

$loop = new WP_Query( array(
    'post_type' => 'product',
    'posts_per_page' => 2,
    'category__in' => $catpro,
  )
);

Using

'product_cat' => $catpro,

in WP_Query is a wrong. That method of using taxonomy parameter works only for category taxonomy - and that's legacy support coming from very older versons of WordPress. That's why for non-category taxonomy in WP_Query you need to use tax_query.

f.e.

 $loop = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => 2,
        'tax_query' => array(
               array(
                 'taxonomy' => 'product_cat',
                 'field'    => 'term_id',
                 'terms'    => $catpro,
               ),
             ),
           )
         );

But don't forget to tune FIELD and TERMS values. As i don't know what your $catpro variable is, i just wrote the code as an example. field can have term_id, slug, name values.

For more examples, check https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

Similar questions

ACF wp_query, get the final field of the chain, which is located in => repeater-> field-> repeater-> FIELD
The final field is PASSWORD, as I mark in the image This is the code of how I tried to get the field..
Display an ACF field values in WooCommerce product archive loop
i'm currently playing around on localhost since i'm going to design an ecommerce for a client who owns a records store. I've installed Elementor, WooCommerce and ACF, and at first tried to use elementor custom skin to create a custom loop for my products, where i easily added the field i wanted with dynamic data. However, this turned out to be a ni...
ACF WP_Query Filter by Taxonomy Field
I am trying to filter a CPT to display fields by Taxonomy, I am currently using the following code:- However, this is not returning any results. This is what I am trying to filter:- So the name of my taxonomy field is called 'make' and for test purposes I just want to display all the posts that have a Taxonomy of 'Jaguar' Please advise.
Can I filter a wp query by custom taxonomy and the status of an ACF file field?
I'm struggling a bit with this one. I've got a custom post type called jobs, which use a custom taxonomy to mark the job status; booked, complete, etc. Custom fields have been done with ACF. What I'd like to do is show a list of jobs that have a certain job status, that don't yet have a file uploaded to them. Example; Job status = complete. Report ...
How to add acf form post categry field in acf form front end but category dropdown not showing
Category dropdown not showing on front end.Please suggest or let me know where i am wrong in my code.
How to display ACF taxonomy checkbox links on WooCommerce product category archive
I am using the Advanced Custom Fields plugin, and have several custom fields that appear on the WooCommerce product category edit screen (where you add/edit product categories), which then display the output on the archive-product.php category template. I have managed to display all of my custom fields on the front-end product category archives wit...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.