I have this code which I am using to loop through all products, then it should print the $current_product
array if a product has term 'composite' from the 'product_type' taxonomy - it doesn't.
BUT if I replace $product->ID
in the has_term
function with the actual number it works fine and prints the array. Why?!
public function test12() {
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
);
$products = get_posts( $args ); ?>
<?php foreach( $products as $product ) {
if( has_term( 'composite', 'product_type', $product->ID ) ) {
$current_product = array(
'ID' => $product->ID,
'post_status' => 'publish',
);
echo 'has';
print_r($current_product);
}
}
}
var_dump($product->ID);
?$product
is aWP_Post
instance, you can pass that directly as 3rd argument tohas_term
rather than$product->ID
.