mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Taxonomy: add a new 'terms_pre_query' filter to short circuit WP_Term_Query 'get_terms' queries.
Add a new `terms_pre_query` filter which returns null by default. Return a non-null value to bypass WordPress's default `get_terms` queries. Props jarocks, boonebgorges, spacedmonkey. Fixes #41246. git-svn-id: https://develop.svn.wordpress.org/trunk@45584 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -737,4 +737,34 @@ class Tests_Term_Query extends WP_UnitTestCase {
|
||||
|
||||
return $term;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 41246
|
||||
*/
|
||||
public function test_terms_pre_query_filter_should_bypass_database_query() {
|
||||
global $wpdb;
|
||||
|
||||
add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
$q = new WP_Term_Query();
|
||||
$results = $q->query(
|
||||
array(
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
|
||||
|
||||
// Make sure no queries were executed.
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// We manually inserted a non-existing term and overrode the results with it.
|
||||
$this->assertSame( array( 555 ), $q->terms );
|
||||
}
|
||||
|
||||
public static function filter_terms_pre_query( $terms, $query ) {
|
||||
return array( 555 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user