Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/term/getTermBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading