diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 06a9126616..cad8792c41 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2046,20 +2046,23 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { if ( 'all' == $fields || 'all_with_object_id' == $fields ) { $_terms = $wpdb->get_results( $query ); - foreach ( $_terms as &$term ) - $term = sanitize_term( $term, $taxonomy, 'raw' ); + foreach ( $_terms as $key => $term ) { + $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' ); + } $terms = array_merge( $terms, $_terms ); update_term_cache( $terms ); } else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { $_terms = $wpdb->get_col( $query ); $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; - foreach ( $_terms as &$term ) - $term = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); + foreach ( $_terms as $key => $term ) { + $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); + } $terms = array_merge( $terms, $_terms ); } else if ( 'tt_ids' == $fields ) { $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order"); - foreach ( $terms as &$tt_id ) - $tt_id = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. + foreach ( $terms as $key => $tt_id ) { + $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. + } } if ( ! $terms ) diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 2a990d62be..dde2200d55 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -8,7 +8,7 @@ class Tests_Term extends WP_UnitTestCase { function setUp() { parent::setUp(); - + _clean_term_filters(); // insert one term into every post taxonomy // otherwise term_ids and term_taxonomy_ids might be identical, which could mask bugs @@ -87,6 +87,26 @@ class Tests_Term extends WP_UnitTestCase { $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) ); } + function test_get_object_terms() { + $post_id = $this->factory->post->create(); + $terms_1 = array('foo', 'bar', 'baz'); + + wp_set_object_terms( $post_id, $terms_1, $this->taxonomy ); + add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) ); + $terms = wp_get_object_terms( $post_id, $this->taxonomy ); + remove_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) ); + foreach ( $terms as $term ) { + $this->assertInternalType( 'object', $term ); + } + } + + function filter_get_object_terms( $terms ) { + // var_dump reveals an array of objects + $term_ids = wp_list_pluck( $terms, 'term_id' ); + // last term is now an integer + return $terms; + } + function test_set_object_terms_by_id() { $ids = $this->factory->post->create_many(5);