Make WordPress Core


Ignore:
Timestamp:
01/31/2015 03:47:51 PM (9 years ago)
Author:
boonebgorges
Message:

Improve support for ordering WP_Query results by postmeta.

WP_Meta_Query clauses now support a 'name' parameter. When building a
WP_Query object, the value of 'orderby' can reference this 'name', so that
it's possible to order by any clause in a meta_query, not just the first one
(as when using 'orderby=meta_value'). This improvement also makes it possible
to order by multiple meta query clauses (or by any other eligible field plus
a meta query clause), using the array syntax for 'orderby' introduced in [29027].

Props Funkatronic, boonebgorges.
Fixes #31045.

File:
1 edited

Legend:

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

    r31212 r31312  
    935935
    936936    /**
     937     * A flat list of clauses, keyed by clause 'name'.
     938     *
     939     * @since 4.2.0
     940     * @var array
     941     */
     942    protected $clauses = array();
     943
     944    /**
    937945     * Constructor.
    938946     *
    939947     * @since 3.2.0
     948     * @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query.
     949     *
    940950     * @access public
    941951     *
     
    958968     *                               'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
    959969     *                               Default is 'CHAR'.
     970     *         @type string $name    Optional. A unique identifier for the clause. If provided, `$name` can be
     971     *                               referenced in the `$orderby` parameter of the parent query.
    960972     *     }
    961973     * }
     
    13751387        $clause['alias'] = $alias;
    13761388
     1389        // Determine the data type.
     1390        $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
     1391        $meta_type  = $this->get_cast_for_type( $_meta_type );
     1392        $clause['cast'] = $meta_type;
     1393
     1394        // Store the clause in our flat array.
     1395        $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
     1396        $this->clauses[ $clause_name ] =& $clause;
     1397
    13771398        // Next, build the WHERE clause.
    13781399
     
    13891410        if ( array_key_exists( 'value', $clause ) ) {
    13901411            $meta_value = $clause['value'];
    1391             $meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
    13921412
    13931413            if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     
    14491469
    14501470        return $sql_chunks;
     1471    }
     1472
     1473    /**
     1474     * Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
     1475     *
     1476     * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
     1477     * a value of 'orderby' corresponding to a meta clause.
     1478     *
     1479     * @since 4.2.0
     1480     * @access public
     1481     *
     1482     * @return array
     1483     */
     1484    public function get_clauses() {
     1485        return $this->clauses;
    14511486    }
    14521487
Note: See TracChangeset for help on using the changeset viewer.