I would like to create a condition to manually hide product categories within the shop. I already started from a code base but I am trying to understand why if I put the is_product_category()
condition the error comes out as I specified in the question with PHP Version 8.0
This is the code:
add_filter( 'get_terms', 'sct_hide_cat', 10, 3 );
function sct_hide_cat( $terms, $taxonomies, $args ) {
//global $product;
$exclude = [50, 22, 20, 31, 35, 45, 40, 65, 37, 40, 3434];
$new_terms = [];
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() || is_product_category() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->term_id, $exclude ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
$terms
and see if everything is expected, likely it is not.get_terms
is a filter applied byget_terms()
which doesn't always return an array, and even when it's an array, it's not always an array of objects. And in your case, the"term_id" on int
in the error implied that the$terms
was an array of term IDs instead of objects. So you can't simply use$term->term_id
, because$term
could be a term ID, name, etc. and is not necessarily an object.