WordPress.org

Make WordPress Core

Changeset 30042


Ignore:
Timestamp:
10/28/2014 02:56:33 PM (6 years ago)
Author:
boonebgorges
Message:

Allow 'slug' param of get_terms() to accept an array.

Props jfarthing84, dlh.
Fixes #23636.

Location:
trunk
Files:
2 edited

Legend:

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

    r30031 r30042  
    15951595 *                                           term objects), 'ids' or 'names' (returns an array of integers
    15961596 *                                           or strings, respectively. Default 'all'.
    1597  *     @type string       $slug              Slug to return term(s) for. Default empty.
     1597 *     @type string|array $slug              Optional. Slug(s) to return term(s) for. Default empty.
    15981598 *     @type bool         $hierarchical      Whether to include terms that have non-empty descendants (even
    15991599 *                                           if $hide_empty is set to true). Default true.
     
    18051805
    18061806    if ( ! empty( $args['slug'] ) ) {
    1807         $slug = sanitize_title( $args['slug'] );
    1808         $where .= " AND t.slug = '$slug'";
     1807        if ( is_array( $args['slug'] ) ) {
     1808            $slug = array_map( 'sanitize_title', $args['slug'] );
     1809            $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')";
     1810        } else {
     1811            $slug = sanitize_title( $args['slug'] );
     1812            $where .= " AND t.slug = '$slug'";
     1813        }
    18091814    }
    18101815
  • trunk/tests/phpunit/tests/term/getTerms.php

    r29845 r30042  
    353353    }
    354354
     355    public function test_get_terms_by_slug() {
     356        $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) );
     357        $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) );
     358
     359        $found = get_terms( 'post_tag', array(
     360            'hide_empty' => false,
     361            'fields' => 'ids',
     362            'slug' => 'foo',
     363        ) );
     364
     365        $this->assertEquals( array( $t1 ), $found );
     366    }
     367
     368    /**
     369     * @ticket 23636
     370     */
     371    public function test_get_terms_by_multiple_slugs() {
     372        $t1 = $this->factory->tag->create( array( 'slug' => 'foo' ) );
     373        $t2 = $this->factory->tag->create( array( 'slug' => 'bar' ) );
     374        $t3 = $this->factory->tag->create( array( 'slug' => 'barry' ) );
     375
     376        $found = get_terms( 'post_tag', array(
     377            'hide_empty' => false,
     378            'fields' => 'ids',
     379            'slug' => array( 'foo', 'barry' )
     380        ) );
     381
     382        $this->assertEquals( array( $t1, $t3 ), $found );
     383    }
     384
    355385    public function test_get_terms_hierarchical_tax_hide_empty_false_fields_ids() {
    356386        // Set up a clean taxonomy.
Note: See TracChangeset for help on using the changeset viewer.