0

When I running Query Monitor on one of my page, I get a notice "Array to string conversion" for the following line:

      $terms = get_the_terms( $post->ID , array( 'product-categories') );

I then check the document for get_the_terms at https://developer.wordpress.org/reference/functions/get_the_terms/, the second parameter is a string.

However, after reading further, I find some samples in the above link and some are using string directly, like below:

$terms = get_the_terms( $post->ID , array( 'teams_positions') );

but some uses an array, like below:

$post_categories = get_the_terms( $post->ID, 'category' );

So I am totally confused.

2
  • The documentation is generated from the source code, so always trust that over random examples elsewhere. But where are you seeing these examples? Are you sure it’s the same function? Dec 7, 2021 at 8:42
  • @JacobPeattie, The samples are just in the above link, at developer.wordpress.org/reference/functions/get_the_terms/…, and the one with $terms = get_the_terms( $post->ID , array( 'teams_positions') ) still get two upvotes.
    – alancc
    Dec 7, 2021 at 22:18

1 Answer 1

1

Going by the documentation it should only be string.

Notice this line $terms = get_object_term_cache( $post->ID, $taxonomy );

The get_object_term_cache( int $id, string $taxonomy ) always expects the second (required) argument to be a string, so if you pass an array as the second argument to get_the_terms you will get the Array to string conversion notice.

Second argument should always be a string, those example could be old and the function was probably updated in newer versions.

For further information I would check wordpress change logs in the versions, maybe there you will find this function with a second parameter that used to accept array as well as string.

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.