Home > Back-end >  using a ACF field for terms in params array
using a ACF field for terms in params array

Time:01-05

I am trying to pass the category id from an ACF field to the params array in a custom product template, but I just get the number echoed on to the page, rather than the actual product feed.

`<?php

            $params = array(
                'posts_per_page' => -1,
                'post_type' => 'product',
                'post_status' => 'publish',
                'tax_query'   => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => the_field('cat_id'),
                        'operator' => 'IN'
                    
                    )
                )
            );
            $wc_query = new WP_Query($params);
            ?>

            <?php if ($wc_query->have_posts()) : ?>
                <?php while ($wc_query->have_posts()) :
                    $wc_query->the_post();
                ?>`

CodePudding user response:

use get_field() function:

$params = array(
                'posts_per_page' => -1,
                'post_type' => 'product',
                'post_status' => 'publish',
                'tax_query'   => array(
                    'relation' => 'OR',
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'    => get_field('cat_id'),
                        'operator' => 'IN'
                    
                    )
                )
            );
  • Related