Make WordPress Core


Ignore:
Timestamp:
02/06/2016 04:41:26 AM (11 years ago)
Author:
boonebgorges
Message:

Allow get_terms() results to ordered by metadata.

The $orderby parameter of get_terms() now accepts the following values,
related to term meta:

  • 'meta_value'
  • 'meta_value_num'
  • the value of the $meta_key parameter
  • any key from the $meta_query array

This brings order-by-meta support for terms in line with post, comment, and
user queries.

As a byproduct of these improvements, $meta_key and $meta_value parameters
have been introduced to get_terms(). They interact with $meta_query in the
same way as in WP_Query and other query classes.

Props jadpm, eherman24.
Fixes #34996.

File:
1 edited

Legend:

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

    r36400 r36485  
    10611061 *              Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return
    10621062 *              a list of WP_Term objects.
     1063 * @since 4.5.0 Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
    10631064 *
    10641065 * @global wpdb  $wpdb WordPress database abstraction object.
    … …  
    10721073 *                                                'term_group', 'term_id', 'id', 'description'), 'count' for term
    10731074 *                                                taxonomy count, 'include' to match the 'order' of the $include param,
    1074  *                                                or 'none' to skip ORDER BY. Defaults to 'name'.
     1075 *                                                'meta_value', 'meta_value_num', the value of `$meta_key`, the array
     1076 *                                                keys of `$meta_query`, or 'none' to omit the ORDER BY clause.
     1077 *                                                Defaults to 'name'.
    10751078 *     @type string       $order                  Whether to order terms in ascending or descending order.
    10761079 *                                                Accepts 'ASC' (ascending) or 'DESC' (descending).
    … …  
    11201123 *     @type array        $meta_query             Meta query clauses to limit retrieved terms by.
    11211124 *                                                See `WP_Meta_Query`. Default empty.
     1125 *     @type string       $meta_key               Limit terms to those matching a specific metadata key. Can be used in
     1126 *                                                conjunction with `$meta_value`.
     1127 *     @type string       $meta_value             Limit terms to those matching a specific metadata value. Usually used
     1128 *                                                in conjunction with `$meta_key`.
    11221129 * }
    11231130 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
    … …  
    14151422        $join = '';
    14161423        $distinct = '';
    1417         if ( ! empty( $args['meta_query'] ) ) {
    1418                 $mquery = new WP_Meta_Query( $args['meta_query'] );
    1419                 $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
    1420 
     1424
     1425        $mquery = new WP_Meta_Query();
     1426        $mquery->parse_query_vars( $args );
     1427        $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' );
     1428        $meta_clauses = $mquery->get_clauses();
     1429
     1430        if ( ! empty( $meta_clauses ) ) {
    14211431                $join  .= $mq_sql['join'];
    14221432                $where .= $mq_sql['where'];
    14231433                $distinct .= "DISTINCT";
     1434
     1435                // 'orderby' support.
     1436                $allowed_keys = array();
     1437                $primary_meta_key   = null;
     1438                $primary_meta_query = reset( $meta_clauses );
     1439                if ( ! empty( $primary_meta_query['key'] ) ) {
     1440                        $primary_meta_key = $primary_meta_query['key'];
     1441                        $allowed_keys[] = $primary_meta_key;
     1442                }
     1443                $allowed_keys[] = 'meta_value';
     1444                $allowed_keys[] = 'meta_value_num';
     1445                $allowed_keys   = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
     1446
     1447                if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], $allowed_keys ) ) {
     1448                        switch( $args['orderby'] ) {
     1449                                case $primary_meta_key:
     1450                                case 'meta_value':
     1451                                        if ( ! empty( $primary_meta_query['type'] ) ) {
     1452                                                $orderby = "ORDER BY CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
     1453                                        } else {
     1454                                                $orderby = "ORDER BY {$primary_meta_query['alias']}.meta_value";
     1455                                        }
     1456                                        break;
     1457
     1458                                case 'meta_value_num':
     1459                                        $orderby = "ORDER BY {$primary_meta_query['alias']}.meta_value+0";
     1460                                        break;
     1461
     1462                                default:
     1463                                        if ( array_key_exists( $args['orderby'], $meta_clauses ) ) {
     1464                                                // $orderby corresponds to a meta_query clause.
     1465                                                $meta_clause = $meta_clauses[ $args['orderby'] ];
     1466                                                $orderby = "ORDER BY CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
     1467                                        }
     1468                                        break;
     1469                        }
     1470                }
    14241471        }
    14251472
Note: See TracChangeset for help on using the changeset viewer.