Support » Fixing WordPress » Group by with get_the_terms

  • I don’t have want to find out a bug or anything, I’m just bad at coding.

    What have have is this code, to find my categories in the fourth level, and print out the descriptions:

    <?php while ( have_posts() ) :
            $array = get_the_terms( get_the_ID(), 'product_category' );
            $fourth_level = array_filter($array, function ($t) {
                if($t->parent != 0
                    && get_term($t->parent, 'product_category')->parent  != 0
                    && get_term(get_term($t->parent, 'product_category')->parent, 'product_category')->parent != 0
                    && get_term(get_term(get_term($t->parent, 'product_category')->parent, 'product_category')->parent, 'product_category')->parent == 0) return true;
                else return false;
            foreach($fourth_level as $company)
                    // Print out company info \\
                    echo "<a href='" . $company->slug . "/?cat=" . $_GET["cat"] . "'>" . $company->description . "</a>";

    The problem with the code is this: Every time a category (company) has more than one post (product) the code prints out multiple category descriptions.

    What I need to do is to use the GROUP BY command with SQL, but I don’t know where to use it. Could someone please help?
    Thanks in advance!

  • The topic ‘Group by with get_the_terms’ is closed to new replies.