get_the_terms() not returning expected result

by APAD1   Last Updated March 29, 2018 17:08 PM

I have created 3 custom taxonomies to tag my posts with. On my single.php template I am attempting to count the number of terms, using get_the_terms() for each custom taxonomy that is assigned to the post, but it is not producing the result I am expecting.

I have two custom taxonomies called "sizes" and "colors" and I am getting the number of terms from these taxonomies like this:

<?php
    $sizeCount = count(get_the_terms($post->ID, 'sizes'));
    $colorCount = count(get_the_terms($post->ID, 'colors'));

    echo $sizeCount . ' ' . $colorCount;
?>

The issue is that this always seems to return at least 1(never 0), even if the post isn't tagged with any of the terms from that taxonomy. For instance, I have a post tagged with 1 size term and 0 color terms. However, this returns 1 1 when it should be returning 1 0. If I tag it with another size, the sizeCount correctly outputs and I get 2 1.

Does anyone know why I am getting a 1 even when the post is not tagged with any terms from that taxonomy?

Tags : terms


Related Questions


Why does wp_remove_object_terms not work?

Updated April 01, 2018 06:08 AM


Custom Taxonomy Archive BUG

Updated April 25, 2017 21:08 PM

Filter Terms by FacetWP

Updated February 03, 2018 20:08 PM