WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/19/2014 06:53:55 PM (7 years ago)
Author:
boonebgorges
Message:

Make orderby=meta_value compatible with meta_query when relation=OR.

Passing orderby=meta_value and meta_key=foo to WP_Query should require that
each matched post have a meta value with the key 'foo'. To make this
requirement compatible with meta_query params that have the relation OR, we
nest the meta_query param, and join it using AND to a meta_query clause
generated from the meta_key/meta_compare/meta_type query vars.

Fixes #25538.

File:
1 edited

Legend:

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

    r29953 r29964  
    10581058        $meta_query = array();
    10591059
    1060         // Simple query needs to be first for orderby=meta_value to work correctly.
     1060        /*
     1061         * For orderby=meta_value to work correctly, simple query needs to be
     1062         * first (so that its table join is against an unaliased meta table) and
     1063         * needs to be its own clause (so it doesn't interfere with the logic of
     1064         * the rest of the meta_query).
     1065         */
     1066        $primary_meta_query = array();
    10611067        foreach ( array( 'key', 'compare', 'type' ) as $key ) {
    1062             if ( !empty( $qv[ "meta_$key" ] ) )
    1063                 $meta_query[0][ $key ] = $qv[ "meta_$key" ];
     1068            if ( ! empty( $qv[ "meta_$key" ] ) ) {
     1069                $primary_meta_query[ $key ] = $qv[ "meta_$key" ];
     1070            }
    10641071        }
    10651072
    10661073        // WP_Query sets 'meta_value' = '' by default.
    1067         if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] && ( ! is_array( $qv[ 'meta_value' ] ) || $qv[ 'meta_value' ] ) )
    1068             $meta_query[0]['value'] = $qv[ 'meta_value' ];
    1069 
    1070         if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    1071             $meta_query = array_merge( $meta_query, $qv['meta_query'] );
     1074        if ( isset( $qv['meta_value'] ) && '' !== $qv['meta_value'] && ( ! is_array( $qv['meta_value'] ) || $qv['meta_value'] ) ) {
     1075            $primary_meta_query['value'] = $qv['meta_value'];
     1076        }
     1077
     1078        $existing_meta_query = isset( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ? $qv['meta_query'] : array();
     1079
     1080        if ( ! empty( $primary_meta_query ) && ! empty( $existing_meta_query ) ) {
     1081            $meta_query = array(
     1082                'relation' => 'AND',
     1083                $primary_meta_query,
     1084                $existing_meta_query,
     1085            );
     1086        } else if ( ! empty( $primary_meta_query ) ) {
     1087            $meta_query = array(
     1088                $primary_meta_query,
     1089            );
     1090        } else if ( ! empty( $existing_meta_query ) ) {
     1091            $meta_query = $existing_meta_query;
    10721092        }
    10731093
Note: See TracChangeset for help on using the changeset viewer.