I want to get the product tags of the woocommerce products in an array, for doing if/else logic with it (in_array), but my code doesn’t work:
<?php
$aromacheck = array() ;
$aromacheck = get_terms( 'product_tag') ;
// echo $aromacheck
?>
When echoing $aromacheck, I only get empty Array, though the product tags are existing – visible in the post class.
How can I get the product tags in an array correctly?
Solution (thanks Noman and nevius):
/* Get the product tag */
$terms = get_the_terms( $post->ID, 'product_tag' );
$aromacheck = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$aromacheck[] = $term->slug;
}
}
/* Check if it is existing in the array to output some value */
if (in_array ( "value", $aromacheck ) ) {
echo "I have the value";
}
You need to loop through the array and create a separate array to check
in_array
becauseget_terms
returnobject
with in array.So, After loop through the array.
You can use in_array().
Suppose
$term_array
contains tag blackI had to parse an args-array to the get_terms function. Maybe this help other aswell.