Changeset 34679
- Timestamp:
- 09/29/2015 03:51:11 AM (8 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy-functions.php
r34538 r34679 782 782 * 783 783 * @since 2.3.0 784 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. 784 785 * 785 786 * @global wpdb $wpdb WordPress database abstraction object. … … 788 789 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' 789 790 * @param string|int $value Search for this term value 790 * @param string $taxonomy Taxonomy Name791 * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'. 791 792 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 792 793 * @param string $filter Optional, default is raw or no WordPress defined filter will applied. … … 794 795 * Will return false if $taxonomy does not exist or $term was not found. 795 796 */ 796 function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {797 function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 797 798 global $wpdb; 798 799 799 if ( ! taxonomy_exists($taxonomy) ) 800 // 'term_taxonomy_id' lookups don't require taxonomy checks. 801 if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) { 800 802 return false; 803 } 804 805 $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 801 806 802 807 if ( 'slug' == $field ) { … … 812 817 $value = (int) $value; 813 818 $field = 'tt.term_taxonomy_id'; 819 820 // No `taxonomy` clause when searching by 'term_taxonomy_id'. 821 $tax_clause = ''; 814 822 } else { 815 823 $term = get_term( (int) $value, $taxonomy, $output, $filter ); … … 820 828 } 821 829 822 $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) );830 $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE $field = %s $tax_clause LIMIT 1", $value ) ); 823 831 if ( ! $term ) 824 832 return false; 833 834 // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db. 835 if ( 'term_taxonomy_id' === $field ) { 836 $taxonomy = $term->taxonomy; 837 } 825 838 826 839 wp_cache_add( $term->term_id, $term, $taxonomy ); -
trunk/tests/phpunit/tests/term/getTermBy.php
r34628 r34679 37 37 $this->assertSame( $t, $found->term_id ); 38 38 } 39 40 /** 41 * @ticket 30620 42 */ 43 public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id() { 44 global $wpdb; 45 46 register_taxonomy( 'wptests_tax', 'post' ); 47 $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 48 $term = get_term( $t, 'wptests_tax' ); 49 50 $new_ttid = $term->term_taxonomy_id + 1; 51 52 // Offset just to be sure. 53 $wpdb->update( 54 $wpdb->term_taxonomy, 55 array( 'term_taxonomy_id' => $new_ttid ), 56 array( 'term_id' => $t ) 57 ); 58 59 $found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' ); 60 $this->assertSame( $t, $found->term_id ); 61 } 39 62 }
Note: See TracChangeset
for help on using the changeset viewer.