WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/25/2018 01:22:44 AM (3 years ago)
Author:
boonebgorges
Message:

Taxonomy: Improve cache handling when querying for terms using all_with_object_id.

When a term query using fields=all_with_object_id hits the cache, the
cached stdClass objects must be converted to WP_Term objects. This
was overlooked when WP_Term_Query was refactored to support object
queries in [38667].

Props dlh.
Fixes #44221.

File:
1 edited

Legend:

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

    r43049 r43313  
    316316        foreach ( $query->terms as $term ) {
    317317            $this->assertSame( $t, $term->term_id );
     318        }
     319    }
     320
     321    /**
     322     * @ticket 44221
     323     */
     324    public function test_all_with_object_id_should_return_term_objects() {
     325        register_taxonomy( 'wptests_tax_1', 'post' );
     326        $posts = self::factory()->post->create_many( 2 );
     327        $t     = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
     328
     329        foreach ( $posts as $p ) {
     330            wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
     331        }
     332
     333        $query = new WP_Term_Query();
     334        $args = array(
     335            'taxonomy'   => 'wptests_tax_1',
     336            'object_ids' => $posts,
     337            'fields'     => 'all_with_object_id',
     338        );
     339
     340        $terms = $query->query( $args );
     341        $this->assertNotEmpty( $terms );
     342        foreach ( $terms as $term ) {
     343            $this->assertInstanceOf( 'WP_Term', $term );
     344            $this->assertObjectHasAttribute( 'object_id', $term );
     345        }
     346
     347        // Run again to check the cached response.
     348        $terms = $query->query( $args );
     349        $this->assertNotEmpty( $terms );
     350        foreach ( $terms as $term ) {
     351            $this->assertInstanceOf( 'WP_Term', $term );
     352            $this->assertObjectHasAttribute( 'object_id', $term );
    318353        }
    319354    }
Note: See TracChangeset for help on using the changeset viewer.