Make WordPress Core

Changeset 34998


Ignore:
Timestamp:
10/10/2015 02:12:40 AM (11 years ago)
Author:
boonebgorges
Message:

Return WP_Term objects from get_terms().

Props boonebgorges, flixos90, DrewAPicture.
See #14162.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy-functions.php

    r34997 r34998  
    992992 * @since 4.2.0 Introduced 'name' and 'childless' parameters.
    993993 * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter.
    994  *              Introduced the 'meta_query' and 'update_term_meta_cache' parameters.
     994 *              Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return
     995 *              a list of WP_Term objects.
    995996 *
    996997 * @global wpdb  $wpdb WordPress database abstraction object.
    … …  
    10531054 *                                                See `WP_Meta_Query`. Default empty.
    10541055 * }
    1055  * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
    1056  *                        do not exist.
     1056 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
     1057 *                            do not exist.
    10571058 */
    10581059function get_terms( $taxonomies, $args = '' ) {
    … …  
    14941495                        $_terms[ $term->term_id ] = $term->slug;
    14951496                }
     1497        } else {
     1498                $_terms = array_map( 'get_term', $terms );
    14961499        }
    14971500
  • trunk/tests/phpunit/tests/term/getTerms.php

    r34828 r34998  
    15731573        }
    15741574
     1575        /**
     1576         * @ticket 14162
     1577         */
     1578        public function test_should_return_wp_term_objects() {
     1579                register_taxonomy( 'wptests_tax', 'post' );
     1580                $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     1581
     1582                $found = get_terms( 'wptests_tax', array(
     1583                        'hide_empty' => false,
     1584                        'fields' => 'all',
     1585                ) );
     1586
     1587                $this->assertNotEmpty( $found );
     1588
     1589                foreach ( $found as $term ) {
     1590                        $this->assertInstanceOf( 'WP_Term', $term );
     1591                }
     1592        }
     1593
     1594        /**
     1595         * @ticket 14162
     1596         */
     1597        public function test_should_prime_individual_term_cache_when_fields_is_all() {
     1598                global $wpdb;
     1599
     1600                register_taxonomy( 'wptests_tax', 'post' );
     1601                $terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
     1602
     1603                $found = get_terms( 'wptests_tax', array(
     1604                        'hide_empty' => false,
     1605                        'fields' => 'all',
     1606                ) );
     1607
     1608                $num_queries = $wpdb->num_queries;
     1609                $term0 = get_term( $terms[0] );
     1610                $this->assertSame( $num_queries, $wpdb->num_queries );
     1611
     1612        }
     1613
    15751614        protected function create_hierarchical_terms_and_posts() {
    15761615                $terms = array();
Note: See TracChangeset for help on using the changeset viewer.