Additional 'orderby' values for `wp_get_object_terms()`.
Adds support for ordering by 'taxonomy', 'term_taxonomy_id', and 'parent'. Props ChriCo. Fixes #28688. git-svn-id: https://develop.svn.wordpress.org/trunk@31236 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
9ade002c6f
commit
9501fb3213
|
@ -2562,7 +2562,7 @@ function wp_delete_category( $cat_ID ) {
|
|||
* @param array|string $args {
|
||||
* Array of arguments.
|
||||
* @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group',
|
||||
* or 'term_order'. Default 'name'.
|
||||
* 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'.
|
||||
* @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
|
||||
* @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
|
||||
* 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
|
||||
|
@ -2612,17 +2612,13 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
|||
$order = $args['order'];
|
||||
$fields = $args['fields'];
|
||||
|
||||
if ( 'count' == $orderby ) {
|
||||
$orderby = 'tt.count';
|
||||
} elseif ( 'name' == $orderby ) {
|
||||
$orderby = 't.name';
|
||||
} elseif ( 'slug' == $orderby ) {
|
||||
$orderby = 't.slug';
|
||||
} elseif ( 'term_group' == $orderby ) {
|
||||
$orderby = 't.term_group';
|
||||
} elseif ( 'term_order' == $orderby ) {
|
||||
if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
|
||||
$orderby = "t.$orderby";
|
||||
} else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
|
||||
$orderby = "tt.$orderby";
|
||||
} else if ( 'term_order' === $orderby ) {
|
||||
$orderby = 'tr.term_order';
|
||||
} elseif ( 'none' == $orderby ) {
|
||||
} else if ( 'none' === $orderby ) {
|
||||
$orderby = '';
|
||||
$order = '';
|
||||
} else {
|
||||
|
|
|
@ -227,6 +227,108 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
|
|||
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28688
|
||||
*/
|
||||
public function test_orderby_parent() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$t1 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
$t2 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
$t3 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
|
||||
$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
|
||||
|
||||
$term_1 = get_term( $t1, $this->taxonomy );
|
||||
$term_2 = get_term( $t2, $this->taxonomy );
|
||||
$term_3 = get_term( $t3, $this->taxonomy );
|
||||
|
||||
global $wpdb;
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
|
||||
|
||||
$found = wp_get_object_terms( $p, $this->taxonomy, array(
|
||||
'orderby' => 'parent',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28688
|
||||
*/
|
||||
public function test_orderby_taxonomy() {
|
||||
register_taxonomy( 'wptests_tax_2', 'post' );
|
||||
register_taxonomy( 'wptests_tax_3', 'post' );
|
||||
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$t1 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
$t2 = $this->factory->term->create( array(
|
||||
'taxonomy' => 'wptests_tax_3',
|
||||
) );
|
||||
$t3 = $this->factory->term->create( array(
|
||||
'taxonomy' => 'wptests_tax_2',
|
||||
) );
|
||||
|
||||
wp_set_object_terms( $p, $t1, $this->taxonomy );
|
||||
wp_set_object_terms( $p, $t2, 'wptests_tax_3' );
|
||||
wp_set_object_terms( $p, $t3, 'wptests_tax_2' );
|
||||
|
||||
$found = wp_get_object_terms( $p, array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ), array(
|
||||
'orderby' => 'taxonomy',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28688
|
||||
*/
|
||||
public function test_orderby_tt_id() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
$t1 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
$t2 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
$t3 = $this->factory->term->create( array(
|
||||
'taxonomy' => $this->taxonomy,
|
||||
) );
|
||||
|
||||
// term_taxonomy_id will only have a different order from term_id in legacy situations.
|
||||
$term_1 = get_term( $t1, $this->taxonomy );
|
||||
$term_2 = get_term( $t2, $this->taxonomy );
|
||||
$term_3 = get_term( $t3, $this->taxonomy );
|
||||
|
||||
global $wpdb;
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100004 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
|
||||
$wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
|
||||
|
||||
$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
|
||||
|
||||
$found = wp_get_object_terms( $p, $this->taxonomy, array(
|
||||
'orderby' => 'term_taxonomy_id',
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
|
||||
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
|
||||
}
|
||||
|
||||
public function test_order_desc() {
|
||||
$p = $this->factory->post->create();
|
||||
|
||||
|
|
Loading…
Reference in New Issue