Because get_the_terms
will not always return an array; it will be false
if the post has no terms, or a WP_Error
if the taxonomy does not exist – always sanity check the result before looping:
$terms_slugs = array();
$terms = get_the_terms( $post->id, 'type', /* Note: get_the_terms does not take a 3rd argument ==> */ array( 'parent' => 0 ) );
if ( is_array( $terms ) ) {
foreach ( $terms as $term ) {
$terms_slugs[] = $term->slug;
}
}
Better yet, in this case you can use the awesome helper wp_list_pluck()
:
if ( is_array( $terms = get_the_terms( $post->ID, 'type' ) ) )
$terms_slugs = wp_list_pluck( $terms, 'slug' );
else
$terms_slugs = array();