Make WordPress Core


Ignore:
Timestamp:
05/28/2016 03:09:09 AM (10 years ago)
Author:
boonebgorges
Message:

Allow term meta lazy-loading to be selectively disabled in WP_Query.

The process of lazy-loading can be resource intensive for object that have
terms in large numbers of taxonomies and are running a persistent object cache.
This new parameter allows the feature to be disabled in these cases.

Props DBrumbaugh10Up.
See #36953.

File:
1 edited

Legend:

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

    r36567 r37589  
    151151        }
    152152
     153        /**
     154         * @ticket 36593
     155         */
     156        public function test_lazy_load_term_meta_should_fall_back_on_update_post_term_cache() {
     157                $q = new WP_Query( array(
     158                        'update_post_term_cache' => true,
     159                ) );
     160
     161                $this->assertTrue( $q->get( 'lazy_load_term_meta' ) );
     162
     163                $q = new WP_Query( array(
     164                        'update_post_term_cache' => false,
     165                ) );
     166
     167                $this->assertFalse( $q->get( 'lazy_load_term_meta' ) );
     168        }
     169
     170        /**
     171         * @ticket 36593
     172         */
     173        public function test_lazy_load_term_meta_false() {
     174                global $wpdb;
     175
     176                $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
     177
     178                register_taxonomy( 'wptests_tax', 'post' );
     179                $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     180                wp_set_object_terms( $p, $terms, 'wptests_tax' );
     181                foreach ( $terms as $t ) {
     182                        add_term_meta( $t, 'foo', 'bar' );
     183                }
     184
     185                $q = new WP_Query( array(
     186                        'cache_results' => true,
     187                        'update_post_term_cache' => true,
     188                        'lazy_load_term_meta' => false,
     189                ) );
     190
     191                if ( $q->have_posts() ) {
     192                        while ( $q->have_posts() ) {
     193                                $q->the_post();
     194
     195                                // Requests will hit the database.
     196                                $num_queries = $wpdb->num_queries;
     197                                $this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
     198                                $num_queries++;
     199                                $this->assertSame( $num_queries, $wpdb->num_queries );
     200
     201                                $this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
     202                                $num_queries++;
     203                                $this->assertSame( $num_queries, $wpdb->num_queries );
     204                        }
     205                }
     206        }
     207
    153208        public function test_adding_term_meta_should_bust_get_terms_cache() {
    154209                $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
Note: See TracChangeset for help on using the changeset viewer.