Make WordPress Core

Changeset 37860


Ignore:
Timestamp:
06/25/2016 04:22:55 PM (9 years ago)
Author:
boonebgorges
Message:

Fix orderby meta handling for WP_Term_Query.

In order to allow meta-related values of orderby to be handled properly,
the term query's meta_query object must run its get_sql() method before
orderby parsing.

Fixing this bug required addressing another bug in WP_Meta_Query, which
caused the table alias index not to be reset when calling get_sql()
multiple times on the same object.

Props littler.chicken.
Fixes #37151.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-meta-query.php

    r37688 r37860  
    328328        }
    329329
     330        $this->table_aliases = array();
     331
    330332        $this->meta_table     = $meta_table;
    331333        $this->meta_id_column = sanitize_key( $type . '_id' );
  • trunk/src/wp-includes/class-wp-term-query.php

    r37683 r37860  
    815815        $orderby = '';
    816816
     817        // Tell the meta query to generate its SQL, so we have access to table aliases.
     818        $this->meta_query->get_sql( 'term', 't', 'term_id' );
    817819        $meta_clauses = $this->meta_query->get_clauses();
    818820        if ( ! $meta_clauses || ! $orderby_raw ) {
  • trunk/tests/phpunit/tests/term/query.php

    r37683 r37860  
    6262        $this->assertEqualSets( array( $terms[0], $terms[2] ), $q->terms );
    6363    }
     64
     65    /**
     66     * @ticket 37151
     67     */
     68    public function test_order_by_meta_value_num() {
     69        register_taxonomy( 'wptests_tax', 'post' );
     70
     71        $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     72
     73        add_term_meta( $terms[0], 'foo', 10 );
     74        add_term_meta( $terms[1], 'foo', 1 );
     75        add_term_meta( $terms[2], 'foo', 100 );
     76
     77        $q = new WP_Term_Query( array(
     78            'taxonomy' => array( 'wptests_tax' ),
     79            'fields' => 'ids',
     80            'hide_empty' => false,
     81            'meta_key' => 'foo',
     82            'orderby' => 'meta_value_num',
     83        ) );
     84
     85        $found = array_map( 'intval', $q->terms );
     86        $this->assertSame( array( $terms[1], $terms[0], $terms[2] ), $found );
     87    }
    6488}
Note: See TracChangeset for help on using the changeset viewer.