skip to Main Content

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!

2

Answers


  1. 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,
      )
    );
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search