mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-23 17:48:01 +01:00
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. Built from https://develop.svn.wordpress.org/trunk@31312 git-svn-id: http://core.svn.wordpress.org/trunk@31293 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
3c416b8c87
commit
ca30c725a0
@ -933,10 +933,20 @@ class WP_Meta_Query {
|
|||||||
*/
|
*/
|
||||||
protected $table_aliases = array();
|
protected $table_aliases = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A flat list of clauses, keyed by clause 'name'.
|
||||||
|
*
|
||||||
|
* @since 4.2.0
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $clauses = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @since 4.2.0 Introduced the `$name` parameter, for improved `$orderby` support in the parent query.
|
||||||
|
*
|
||||||
* @access public
|
* @access public
|
||||||
*
|
*
|
||||||
* @param array $meta_query {
|
* @param array $meta_query {
|
||||||
@ -957,6 +967,8 @@ class WP_Meta_Query {
|
|||||||
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
|
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
|
||||||
* 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
|
* 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
|
||||||
* Default is 'CHAR'.
|
* Default is 'CHAR'.
|
||||||
|
* @type string $name Optional. A unique identifier for the clause. If provided, `$name` can be
|
||||||
|
* referenced in the `$orderby` parameter of the parent query.
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
@ -1374,6 +1386,15 @@ class WP_Meta_Query {
|
|||||||
// Save the alias to this clause, for future siblings to find.
|
// Save the alias to this clause, for future siblings to find.
|
||||||
$clause['alias'] = $alias;
|
$clause['alias'] = $alias;
|
||||||
|
|
||||||
|
// Determine the data type.
|
||||||
|
$_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
|
||||||
|
$meta_type = $this->get_cast_for_type( $_meta_type );
|
||||||
|
$clause['cast'] = $meta_type;
|
||||||
|
|
||||||
|
// Store the clause in our flat array.
|
||||||
|
$clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
|
||||||
|
$this->clauses[ $clause_name ] =& $clause;
|
||||||
|
|
||||||
// Next, build the WHERE clause.
|
// Next, build the WHERE clause.
|
||||||
|
|
||||||
// meta_key.
|
// meta_key.
|
||||||
@ -1388,7 +1409,6 @@ class WP_Meta_Query {
|
|||||||
// meta_value.
|
// meta_value.
|
||||||
if ( array_key_exists( 'value', $clause ) ) {
|
if ( array_key_exists( 'value', $clause ) ) {
|
||||||
$meta_value = $clause['value'];
|
$meta_value = $clause['value'];
|
||||||
$meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
|
|
||||||
|
|
||||||
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
|
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
|
||||||
if ( ! is_array( $meta_value ) ) {
|
if ( ! is_array( $meta_value ) ) {
|
||||||
@ -1450,6 +1470,21 @@ class WP_Meta_Query {
|
|||||||
return $sql_chunks;
|
return $sql_chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a flattened list of sanitized meta clauses, indexed by clause 'name'.
|
||||||
|
*
|
||||||
|
* This array should be used for clause lookup, as when the table alias and CAST type must be determined for
|
||||||
|
* a value of 'orderby' corresponding to a meta clause.
|
||||||
|
*
|
||||||
|
* @since 4.2.0
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_clauses() {
|
||||||
|
return $this->clauses;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identify an existing table alias that is compatible with the current
|
* Identify an existing table alias that is compatible with the current
|
||||||
* query clause.
|
* query clause.
|
||||||
|
@ -2233,8 +2233,9 @@ class WP_Query {
|
|||||||
|
|
||||||
$primary_meta_key = '';
|
$primary_meta_key = '';
|
||||||
$primary_meta_query = false;
|
$primary_meta_query = false;
|
||||||
if ( ! empty( $this->meta_query->queries ) ) {
|
$meta_clauses = $this->meta_query->get_clauses();
|
||||||
$primary_meta_query = reset( $this->meta_query->queries );
|
if ( ! empty( $meta_clauses ) ) {
|
||||||
|
$primary_meta_query = reset( $meta_clauses );
|
||||||
|
|
||||||
if ( ! empty( $primary_meta_query['key'] ) ) {
|
if ( ! empty( $primary_meta_query['key'] ) ) {
|
||||||
$primary_meta_key = $primary_meta_query['key'];
|
$primary_meta_key = $primary_meta_query['key'];
|
||||||
@ -2243,6 +2244,7 @@ class WP_Query {
|
|||||||
|
|
||||||
$allowed_keys[] = 'meta_value';
|
$allowed_keys[] = 'meta_value';
|
||||||
$allowed_keys[] = 'meta_value_num';
|
$allowed_keys[] = 'meta_value_num';
|
||||||
|
$allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! in_array( $orderby, $allowed_keys ) ) {
|
if ( ! in_array( $orderby, $allowed_keys ) ) {
|
||||||
@ -2260,29 +2262,36 @@ class WP_Query {
|
|||||||
case 'ID':
|
case 'ID':
|
||||||
case 'menu_order':
|
case 'menu_order':
|
||||||
case 'comment_count':
|
case 'comment_count':
|
||||||
$orderby = "$wpdb->posts.{$orderby}";
|
$orderby_clause = "$wpdb->posts.{$orderby}";
|
||||||
break;
|
break;
|
||||||
case 'rand':
|
case 'rand':
|
||||||
$orderby = 'RAND()';
|
$orderby_clause = 'RAND()';
|
||||||
break;
|
break;
|
||||||
case $primary_meta_key:
|
case $primary_meta_key:
|
||||||
case 'meta_value':
|
case 'meta_value':
|
||||||
if ( ! empty( $primary_meta_query['type'] ) ) {
|
if ( ! empty( $primary_meta_query['type'] ) ) {
|
||||||
$sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] );
|
$orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
|
||||||
$orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})";
|
|
||||||
} else {
|
} else {
|
||||||
$orderby = "$wpdb->postmeta.meta_value";
|
$orderby_clause = "{$primary_meta_query['alias']}.meta_value";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'meta_value_num':
|
case 'meta_value_num':
|
||||||
$orderby = "$wpdb->postmeta.meta_value+0";
|
$orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$orderby = "$wpdb->posts.post_" . $orderby;
|
if ( array_key_exists( $orderby, $meta_clauses ) ) {
|
||||||
|
// $orderby corresponds to a meta_query clause.
|
||||||
|
$meta_clause = $meta_clauses[ $orderby ];
|
||||||
|
$orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
|
||||||
|
} else {
|
||||||
|
// Default: order by post field.
|
||||||
|
$orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $orderby;
|
return $orderby_clause;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2813,6 +2822,12 @@ class WP_Query {
|
|||||||
|
|
||||||
$where .= $search . $whichauthor . $whichmimetype;
|
$where .= $search . $whichauthor . $whichmimetype;
|
||||||
|
|
||||||
|
if ( ! empty( $this->meta_query->queries ) ) {
|
||||||
|
$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
|
||||||
|
$join .= $clauses['join'];
|
||||||
|
$where .= $clauses['where'];
|
||||||
|
}
|
||||||
|
|
||||||
$rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
|
$rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
|
||||||
if ( ! isset( $q['order'] ) ) {
|
if ( ! isset( $q['order'] ) ) {
|
||||||
$q['order'] = $rand ? '' : 'DESC';
|
$q['order'] = $rand ? '' : 'DESC';
|
||||||
@ -3030,12 +3045,6 @@ class WP_Query {
|
|||||||
$where .= ')';
|
$where .= ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !empty( $this->meta_query->queries ) ) {
|
|
||||||
$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
|
|
||||||
$join .= $clauses['join'];
|
|
||||||
$where .= $clauses['where'];
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Apply filters on where and join prior to paging so that any
|
* Apply filters on where and join prior to paging so that any
|
||||||
* manipulations to them are reflected in the paging by day queries.
|
* manipulations to them are reflected in the paging by day queries.
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.2-alpha-31311';
|
$wp_version = '4.2-alpha-31312';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user