Introduce term_taxonomy_id
parameter for WP_Term_Query
.
Allows the fetching of terms based on `term_taxonomy_id`, or an array of `term_taxonomy_ids`. Props spacedmonkey. Fixes #37074. git-svn-id: https://develop.svn.wordpress.org/trunk@37683 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
beebded867
commit
b9d46425c0
@ -92,6 +92,7 @@ class WP_Term_Query {
|
||||
* Sets up the term query, based on the query vars passed.
|
||||
*
|
||||
* @since 4.6.0
|
||||
* @since 4.6.0 Introduced 'term_taxonomy_id' parameter.
|
||||
* @access public
|
||||
*
|
||||
* @param string|array $query {
|
||||
@ -136,6 +137,8 @@ class WP_Term_Query {
|
||||
* Default empty.
|
||||
* @type string|array $slug Optional. Slug or array of slugs to return term(s) for.
|
||||
* Default empty.
|
||||
* @type int|array $term_taxonomy_id Optional. Term taxonomy ID, or array of term taxonomy IDs,
|
||||
* to match when querying terms.
|
||||
* @type bool $hierarchical Whether to include terms that have non-empty descendants (even
|
||||
* if $hide_empty is set to true). Default true.
|
||||
* @type string $search Search criteria to match terms. Will be SQL-formatted with
|
||||
@ -183,6 +186,7 @@ class WP_Term_Query {
|
||||
'count' => false,
|
||||
'name' => '',
|
||||
'slug' => '',
|
||||
'term_taxonomy_id' => '',
|
||||
'hierarchical' => true,
|
||||
'search' => '',
|
||||
'name__like' => '',
|
||||
@ -473,6 +477,15 @@ class WP_Term_Query {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $args['term_taxonomy_id'] ) ) {
|
||||
if ( is_array( $args['term_taxonomy_id'] ) ) {
|
||||
$tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
|
||||
$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
|
||||
} else {
|
||||
$this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $args['name__like'] ) ) {
|
||||
$this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
|
||||
}
|
||||
|
64
tests/phpunit/tests/term/query.php
Normal file
64
tests/phpunit/tests/term/query.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group taxonomy
|
||||
*/
|
||||
class Tests_Term_Query extends WP_UnitTestCase {
|
||||
/**
|
||||
* @ticket 37074
|
||||
*/
|
||||
public function test_term_taxonomy_id_single() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
// Manually change the term_taxonomy_id to something else.
|
||||
$wpdb->update(
|
||||
$wpdb->term_taxonomy,
|
||||
array( 'term_taxonomy_id' => 12345 ),
|
||||
array( 'term_id' => $terms[0] )
|
||||
);
|
||||
|
||||
$q = new WP_Term_Query( array(
|
||||
'term_taxonomy_id' => 12345,
|
||||
'fields' => 'ids',
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $terms[0] ), $q->terms );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 37074
|
||||
*/
|
||||
public function test_term_taxonomy_id_array() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
// Manually change the term_taxonomy_id to something else.
|
||||
$wpdb->update(
|
||||
$wpdb->term_taxonomy,
|
||||
array( 'term_taxonomy_id' => 12345 ),
|
||||
array( 'term_id' => $terms[0] )
|
||||
);
|
||||
|
||||
$wpdb->update(
|
||||
$wpdb->term_taxonomy,
|
||||
array( 'term_taxonomy_id' => 6789 ),
|
||||
array( 'term_id' => $terms[2] )
|
||||
);
|
||||
|
||||
$q = new WP_Term_Query( array(
|
||||
'term_taxonomy_id' => array( 12345, 6789 ),
|
||||
'fields' => 'ids',
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $terms[0], $terms[2] ), $q->terms );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user