Introduce 'parent' parameter to `wp_get_object_terms()`.
Props mikeschinkel. Fixes #15675. git-svn-id: https://develop.svn.wordpress.org/trunk@31270 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
63ee24789d
commit
10d3b42211
|
@ -2555,6 +2555,7 @@ function wp_delete_category( $cat_ID ) {
|
||||||
*
|
*
|
||||||
* @since 2.3.0
|
* @since 2.3.0
|
||||||
* @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
|
* @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
|
||||||
|
* Introduced `$parent` parameter.
|
||||||
*
|
*
|
||||||
* @global wpdb $wpdb WordPress database abstraction object.
|
* @global wpdb $wpdb WordPress database abstraction object.
|
||||||
*
|
*
|
||||||
|
@ -2569,6 +2570,7 @@ function wp_delete_category( $cat_ID ) {
|
||||||
* 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
|
* 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
|
||||||
* term objects being returned, 'ids' will return an array of integers, and 'names' an array
|
* term objects being returned, 'ids' will return an array of integers, and 'names' an array
|
||||||
* of strings.
|
* of strings.
|
||||||
|
* @type int $parent Optional. Limit results to the direct children of a given term ID.
|
||||||
* }
|
* }
|
||||||
* @return array|WP_Error The requested term data or empty array if no terms found.
|
* @return array|WP_Error The requested term data or empty array if no terms found.
|
||||||
* WP_Error if any of the $taxonomies don't exist.
|
* WP_Error if any of the $taxonomies don't exist.
|
||||||
|
@ -2591,7 +2593,12 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||||
$object_ids = array($object_ids);
|
$object_ids = array($object_ids);
|
||||||
$object_ids = array_map('intval', $object_ids);
|
$object_ids = array_map('intval', $object_ids);
|
||||||
|
|
||||||
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
|
$defaults = array(
|
||||||
|
'orderby' => 'name',
|
||||||
|
'order' => 'ASC',
|
||||||
|
'fields' => 'all',
|
||||||
|
'parent' => '',
|
||||||
|
);
|
||||||
$args = wp_parse_args( $args, $defaults );
|
$args = wp_parse_args( $args, $defaults );
|
||||||
|
|
||||||
$terms = array();
|
$terms = array();
|
||||||
|
@ -2652,7 +2659,19 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||||
} elseif ( 'all_with_object_id' == $fields ) {
|
} elseif ( 'all_with_object_id' == $fields ) {
|
||||||
$select_this = 't.*, tt.*, tr.object_id';
|
$select_this = 't.*, tt.*, tr.object_id';
|
||||||
}
|
}
|
||||||
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
|
|
||||||
|
$where = array(
|
||||||
|
"tt.taxonomy IN ($taxonomies)",
|
||||||
|
"tr.object_id IN ($object_ids)",
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( '' !== $args['parent'] ) {
|
||||||
|
$where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$where = implode( ' AND ', $where );
|
||||||
|
|
||||||
|
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE $where $orderby $order";
|
||||||
|
|
||||||
$objects = false;
|
$objects = false;
|
||||||
if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
|
if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
|
||||||
|
|
|
@ -356,6 +356,68 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
|
||||||
$this->assertEquals( array( $t2, $t3, $t1 ), $found );
|
$this->assertEquals( array( $t2, $t3, $t1 ), $found );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 15675
|
||||||
|
*/
|
||||||
|
public function test_parent() {
|
||||||
|
$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,
|
||||||
|
'parent' => $t1,
|
||||||
|
) );
|
||||||
|
$t4 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => $this->taxonomy,
|
||||||
|
'parent' => $t2,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$p = $this->factory->post->create();
|
||||||
|
|
||||||
|
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
|
||||||
|
|
||||||
|
$found = wp_get_object_terms( $p, $this->taxonomy, array(
|
||||||
|
'parent' => $t1,
|
||||||
|
'fields' => 'ids',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEquals( array( $t3 ), $found );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 15675
|
||||||
|
*/
|
||||||
|
public function test_parent_0() {
|
||||||
|
$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,
|
||||||
|
'parent' => $t1,
|
||||||
|
) );
|
||||||
|
$t4 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => $this->taxonomy,
|
||||||
|
'parent' => $t2,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$p = $this->factory->post->create();
|
||||||
|
|
||||||
|
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
|
||||||
|
|
||||||
|
$found = wp_get_object_terms( $p, $this->taxonomy, array(
|
||||||
|
'parent' => 0,
|
||||||
|
'fields' => 'ids',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEqualSets( array( $t1, $t2 ), $found );
|
||||||
|
}
|
||||||
|
|
||||||
public function filter_get_object_terms( $terms ) {
|
public function filter_get_object_terms( $terms ) {
|
||||||
$term_ids = wp_list_pluck( $terms, 'term_id' );
|
$term_ids = wp_list_pluck( $terms, 'term_id' );
|
||||||
// all terms should still be objects
|
// all terms should still be objects
|
||||||
|
|
Loading…
Reference in New Issue