WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/01/2019 08:47:21 PM (2 years ago)
Author:
adamsilverstein
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/query.php

    r43571 r45584  
    738738        return $term;
    739739    }
     740
     741    /**
     742     * @ticket 41246
     743     */
     744    public function test_terms_pre_query_filter_should_bypass_database_query() {
     745        global $wpdb;
     746
     747        add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
     748
     749        $num_queries = $wpdb->num_queries;
     750
     751        $q       = new WP_Term_Query();
     752        $results = $q->query(
     753            array(
     754                'fields' => 'ids',
     755            )
     756        );
     757
     758        remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
     759
     760        // Make sure no queries were executed.
     761        $this->assertSame( $num_queries, $wpdb->num_queries );
     762
     763        // We manually inserted a non-existing term and overrode the results with it.
     764        $this->assertSame( array( 555 ), $q->terms );
     765    }
     766
     767    public static function filter_terms_pre_query( $terms, $query ) {
     768        return array( 555 );
     769    }
    740770}
Note: See TracChangeset for help on using the changeset viewer.