From b3b10b9aeb8c261e87a66df7c880fb9761e86381 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Tue, 23 Jun 2026 17:37:35 +0530 Subject: [PATCH 1/2] Taxonomy: Add 'pre_get_term_by' filter to short-circuit get_term_by() --- src/wp-includes/taxonomy.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 80f457de0e6f7..655fa14b078df 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1098,6 +1098,25 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { */ function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { + /** + * Get all Term data from database by Term field and data. + * + * This allows one to short-circuit the default logic, perhaps by + * replacing it with a routine that is more optimal for your setup. + * + * @since TBD + * + * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' + * @param string|int $value Search for this term value + * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'. + * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N + * @param string $filter Optional, default is raw or no WordPress defined filter will applied. + */ + $pre = apply_filters( 'pre_get_term_by', null, $field, $value, $taxonomy, $output, $filter ); + if ( null !== $pre ) { + return $pre; + } + // 'term_taxonomy_id' lookups don't require taxonomy checks. if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) { return false; From 02f7599d0dfbcd7a96dfe9561d804a61d6471322 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Tue, 23 Jun 2026 21:17:43 +0530 Subject: [PATCH 2/2] Tests: Add unit test for the 'pre_get_term_by' filter --- tests/phpunit/tests/term/getTermBy.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/phpunit/tests/term/getTermBy.php b/tests/phpunit/tests/term/getTermBy.php index 32974d13c54e2..96ee70aebc1e0 100644 --- a/tests/phpunit/tests/term/getTermBy.php +++ b/tests/phpunit/tests/term/getTermBy.php @@ -58,6 +58,23 @@ public function test_get_term_by_unknown() { $this->assertFalse( $term2 ); } + /** + * @ticket 36978 + */ + public function test_get_term_by_pre_filter() { + add_filter( 'pre_get_term_by', array( $this, 'filter_pre_get_term_by' ) ); + + $term = get_term_by( 'slug', 'foo', 'category' ); + + remove_filter( 'pre_get_term_by', array( $this, 'filter_pre_get_term_by' ) ); + + $this->assertSame( 'short-circuited', $term ); + } + + public function filter_pre_get_term_by() { + return 'short-circuited'; + } + /** * @ticket 33281 */