Make WordPress Core

Changeset 31270


Ignore:
Timestamp:
01/23/2015 02:56:04 PM (12 years ago)
Author:
boonebgorges
Message:

Introduce 'parent' parameter to wp_get_object_terms().

Props mikeschinkel.
Fixes #15675.

Location:
trunk
Files:
2 edited

Legend:

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

    r31248 r31270  
    25562556 * @since 2.3.0
    25572557 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
     2558 *              Introduced `$parent` parameter.
    25582559 *
    25592560 * @global wpdb $wpdb WordPress database abstraction object.
     
    25702571 *                           term objects being returned, 'ids' will return an array of integers, and 'names' an array
    25712572 *                           of strings.
     2573 *     @type int    $parent  Optional. Limit results to the direct children of a given term ID.
    25722574 * }
    25732575 * @return array|WP_Error The requested term data or empty array if no terms found.
     
    25922594        $object_ids = array_map('intval', $object_ids);
    25932595
    2594         $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
     2596        $defaults = array(
     2597                'orderby' => 'name',
     2598                'order'   => 'ASC',
     2599                'fields'  => 'all',
     2600                'parent'  => '',
     2601        );
    25952602        $args = wp_parse_args( $args, $defaults );
    25962603
     
    26532660                $select_this = 't.*, tt.*, tr.object_id';
    26542661        }
    2655         $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
     2662
     2663        $where = array(
     2664                "tt.taxonomy IN ($taxonomies)",
     2665                "tr.object_id IN ($object_ids)",
     2666        );
     2667
     2668        if ( '' !== $args['parent'] ) {
     2669                $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
     2670        }
     2671
     2672        $where = implode( ' AND ', $where );
     2673
     2674        $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE $where $orderby $order";
    26562675
    26572676        $objects = false;
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r31236 r31270  
    357357        }
    358358
     359        /**
     360         * @ticket 15675
     361         */
     362        public function test_parent() {
     363                $t1 = $this->factory->term->create( array(
     364                        'taxonomy' => $this->taxonomy,
     365                ) );
     366                $t2 = $this->factory->term->create( array(
     367                        'taxonomy' => $this->taxonomy,
     368                ) );
     369                $t3 = $this->factory->term->create( array(
     370                        'taxonomy' => $this->taxonomy,
     371                        'parent' => $t1,
     372                ) );
     373                $t4 = $this->factory->term->create( array(
     374                        'taxonomy' => $this->taxonomy,
     375                        'parent' => $t2,
     376                ) );
     377
     378                $p = $this->factory->post->create();
     379
     380                wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
     381
     382                $found = wp_get_object_terms( $p, $this->taxonomy, array(
     383                        'parent' => $t1,
     384                        'fields' => 'ids',
     385                ) );
     386
     387                $this->assertEquals( array( $t3 ), $found );
     388        }
     389
     390        /**
     391         * @ticket 15675
     392         */
     393        public function test_parent_0() {
     394                $t1 = $this->factory->term->create( array(
     395                        'taxonomy' => $this->taxonomy,
     396                ) );
     397                $t2 = $this->factory->term->create( array(
     398                        'taxonomy' => $this->taxonomy,
     399                ) );
     400                $t3 = $this->factory->term->create( array(
     401                        'taxonomy' => $this->taxonomy,
     402                        'parent' => $t1,
     403                ) );
     404                $t4 = $this->factory->term->create( array(
     405                        'taxonomy' => $this->taxonomy,
     406                        'parent' => $t2,
     407                ) );
     408
     409                $p = $this->factory->post->create();
     410
     411                wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
     412
     413                $found = wp_get_object_terms( $p, $this->taxonomy, array(
     414                        'parent' => 0,
     415                        'fields' => 'ids',
     416                ) );
     417
     418                $this->assertEqualSets( array( $t1, $t2 ), $found );
     419        }
     420
    359421        public function filter_get_object_terms( $terms ) {
    360422                $term_ids = wp_list_pluck( $terms, 'term_id' );
Note: See TracChangeset for help on using the changeset viewer.