You’re assuming that get_terms()
returns an object with a count
property, but it doesn’t.
From the docs:
Return
(WP_Term[]|int[]|string[]|string|WP_Error) Array of terms, a count thereof as a numeric string, or WP_Error if any of the taxonomies do not exist. See the function description for more information.
Also, the way you’re calling get_terms()
is deprecated. There should be only one argument, an array that includes the taxonomy name.
So here’s how I’d update the code:
$orderby = 'name';
$order="asc";
$hide_empty = false;
$cat_args = array(
'taxonomy' => 'product_cat',
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => 18,
);
$context['categories'] = get_terms( $cat_args );
// Gets the count in a separate call.
$cat_args['fields'] = 'count';
$total_in_term = get_terms( $cat_args );
$context['TotalInTerm'] = $total_in_term;