Home › Forums › General Issues › Get product category (Woocommerce) with a taxonomy field to filter the loop
Hi!
Currently, I am working on a website where I have to show posts with the category that is selected with the taxonomy field. With “normal” (single) posts, 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!
In your first example you are using the category__in argument which can only be used with the category taxonomy.
In WooCommerce the product category taxonomy is a custom taxonomy. Since it is a custom taxonomy instead of using category__in in your second example you should use a tax_query. Take a look at the tax_query documentation for wp_query, it is pretty intuitive to set up if you are not familiar with it.
Hope this is helpful!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.