0
function test_sort_terms_hierarchicaly( Array &$cats, Array &$into, $parentId = 0 )
          {
            if ( is_array( $cats ) ) {
                foreach ( $cats as $i => $cat ) {
                    if ( $cat -> parent == $parentId ) {
                        $into[ $cat -> term_id ] = $cat;
                        unset( $cats[ $i ] );
                    }
                }
            }

The above function checks if the posts iterated have parent category set or not so that a corresponding checkbox can be built with the child categories below the parent category. The first argument is passed like so,

$terms = get_terms( $select_taxonomy, array( 'hide_empty' => 0 ) );
$categoryHierarchy = array();
$this -> test_sort_terms_hierarchicaly( $terms, $categoryHierarchy );

The return type of get_terms() is an array of objects:

                       array(4) {
  [0]=>
  object(WP_Term)#1167 (10) {
    ["term_id"]=>
    int(3)
    ["name"]=>
    string(4) "CAT1"
    ["slug"]=>
    string(4) "cat1"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(3)
    ["taxonomy"]=>
    string(16) "wppg-product-cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(6)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
  [1]=>
  object(WP_Term)#1166 (10) {
    ["term_id"]=>
    int(4)
    ["name"]=>
    string(4) "CAT2"
    ["slug"]=>
    string(4) "cat2"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(4)
    ["taxonomy"]=>
    string(16) "wppg-product-cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(6)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
  [2]=>
  object(WP_Term)#1165 (10) {
    ["term_id"]=>
    int(5)
    ["name"]=>
    string(4) "CAT3"
    ["slug"]=>
    string(4) "cat3"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(5)
    ["taxonomy"]=>
    string(16) "wppg-product-cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(6)
    ["count"]=>
    int(1)
    ["filter"]=>
    string(3) "raw"
  }
  [3]=>
  object(WP_Term)#1169 (10) {
    ["term_id"]=>
    int(6)
    ["name"]=>
    string(10) "PARENT_CAT"
    ["slug"]=>
    string(10) "parent_cat"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(6)
    ["taxonomy"]=>
    string(16) "wppg-product-cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(0)
    ["count"]=>
    int(0)
    ["filter"]=>
    string(3) "raw"
  }
}

Yet still I'm getting this email even if the argument passed is an array. What am I doing wrong here?

Uncaught TypeError: Argument 1 passed to WPPG_Class::test_sort_terms_hierarchicaly() must be of the type array, object given

1 Answer 1

1

Firstly, this isn't a WordPress error. It's a PHP error. WordPress is not going to return type errors. That all happens and the language level. If you used the same values outside of WordPress you'd get the same error.

Regarding your specific issue, keep in mind that get_terms() will return a WP_Error object if the taxonomy is invalid or if your code is run before the taxonomy is registered. That's most likely what's happening here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.