I’m trying to use custom fields on the backend to set the taxonomies in a WP_Query for a category archive on the front end.
In the page template. this gets my categories from the custom field:
$terms = get_field('add_categories_custom_template', $term->term_id);
if( $terms ):
foreach( $terms as $term ):
echo $term; echo ',';
endforeach;
endif;
That gives me an output like this:
12,345,900,
But how do I get that output into the 'terms' => array
for the tax_query
? What I need is the array of IDs to be output this way:
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('12,345,900'), // output this way
'operator' => 'IN',
)
)
But doing this
$terms = get_field('add_categories_custom_template', $term->term_id);
if( $terms ):
foreach( $terms as $term ):
$my_terms = $term; echo ',';
endforeach;
endif;
and this
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array($my_terms),
'operator' => 'IN',
)
)
only outputs the first $term (i.e., 12) and not the subsequent terms (345 and 900).