Make WordPress Core

Changeset 36485

Timestamp:
02/06/2016 04:41:26 AM (9 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.

Location:
trunk
Files:
2 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
    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
     1126
     1127
     1128
    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
     1436
     1437
     1438
     1439
     1440
     1441
     1442
     1443
     1444
     1445
     1446
     1447
     1448
     1449
     1450
     1451
     1452
     1453
     1454
     1455
     1456
     1457
     1458
     1459
     1460
     1461
     1462
     1463
     1464
     1465
     1466
     1467
     1468
     1469
     1470
    14241471    }
    14251472
  • trunk/tests/phpunit/tests/term/getTerms.php

    r36348 r36485  
    12341234    }
    12351235
     1236
     1237
     1238
     1239
     1240
     1241
     1242
     1243
     1244
     1245
     1246
     1247
     1248
     1249
     1250
     1251
     1252
     1253
     1254
     1255
     1256
     1257
     1258
     1259
     1260
     1261
     1262
     1263
     1264
     1265
     1266
     1267
     1268
     1269
     1270
     1271
     1272
     1273
     1274
     1275
     1276
     1277
     1278
     1279
     1280
     1281
     1282
     1283
     1284
     1285
     1286
     1287
     1288
     1289
     1290
     1291
     1292
     1293
     1294
     1295
     1296
     1297
     1298
     1299
     1300
     1301
     1302
     1303
     1304
     1305
     1306
     1307
     1308
     1309
     1310
     1311
     1312
     1313
     1314
     1315
     1316
     1317
     1318
     1319
     1320
     1321
     1322
     1323
     1324
     1325
     1326
     1327
     1328
     1329
     1330
     1331
     1332
     1333
     1334
     1335
     1336
     1337
     1338
     1339
     1340
     1341
     1342
     1343
     1344
     1345
     1346
     1347
     1348
     1349
     1350
     1351
     1352
     1353
     1354
     1355
     1356
     1357
     1358
     1359
     1360
     1361
     1362
     1363
     1364
     1365
     1366
     1367
     1368
     1369
     1370
     1371
     1372
     1373
     1374
     1375
     1376
     1377
     1378
     1379
     1380
     1381
     1382
     1383
     1384
     1385
     1386
     1387
     1388
     1389
     1390
     1391
     1392
     1393
     1394
     1395
     1396
     1397
     1398
     1399
     1400
     1401
     1402
     1403
     1404
     1405
     1406
     1407
     1408
     1409
     1410
     1411
     1412
     1413
     1414
     1415
     1416
     1417
     1418
     1419
     1420
     1421
     1422
     1423
     1424
     1425
     1426
     1427
     1428
     1429
     1430
     1431
     1432
     1433
     1434
     1435
     1436
     1437
     1438
     1439
     1440
     1441
     1442
     1443
     1444
     1445
     1446
     1447
     1448
     1449
     1450
     1451
     1452
     1453
     1454
     1455
     1456
     1457
     1458
     1459
     1460
     1461
     1462
     1463
     1464
     1465
     1466
     1467
     1468
     1469
     1470
     1471
     1472
     1473
     1474
     1475
     1476
     1477
     1478
     1479
     1480
     1481
     1482
     1483
     1484
     1485
     1486
     1487
     1488
     1489
     1490
     1491
     1492
     1493
     1494
     1495
     1496
     1497
     1498
     1499
     1500
     1501
     1502
     1503
     1504
     1505
     1506
     1507
     1508
     1509
     1510
     1511
     1512
     1513
     1514
     1515
     1516
     1517
     1518
     1519
     1520
     1521
     1522
     1523
     1524
     1525
     1526
     1527
     1528
     1529
     1530
     1531
     1532
     1533
     1534
     1535
     1536
     1537
     1538
     1539
     1540
     1541
     1542
     1543
     1544
     1545
     1546
     1547
     1548
     1549
     1550
     1551
     1552
     1553
     1554
     1555
     1556
     1557
     1558
     1559
     1560
     1561
     1562
     1563
     1564
     1565
     1566
    12361567    public function test_hierarchical_false_with_parent() {
    12371568        $initial_terms = $this->create_hierarchical_terms();
Note: See TracChangeset for help on using the changeset viewer.