If you just want to check if a term with a specific slug exists in the database, then you can use term_exists()
. E.g.
<?php if ( term_exists( $cptTax, 'tax1' ) ) : ?>
Your form here.
<?php endif; ?>
But if you do meant to check if the term is in a specific terms array, then you can use wp_list_filter()
like so:
<?php
$found_terms = wp_list_filter( $tax1List, array( // set one or more conditions
'slug' => $cptTax,
) );
if ( ! empty( $found_terms ) ) : ?>
Your form here.
<?php endif; ?>
Or you could instead use array_filter()
to use your own filter function, like this:
$found_terms = array_filter( $tax1List, function ( $term ) use ( $cptTax ) {
return ( $cptTax === $term->slug ); // here, set your own custom logic
} );
if ( ! empty( $found_terms ) ) : ?>
Your form here.
<?php endif; ?>
And just so you know, your custom function (in_array_r()
) returned false because get_terms()
by default returns an array of objects where each is an instance of the WP_Term
class. So basically, the if
in your function would need a condition that looks like ( is_object( $item ) && $needle === $item->slug )
. However, for simple filtering, I would use the wp_list_filter()
above.