WordPress.org

Make WordPress Core

Changeset 45888


Ignore:
Timestamp:
08/23/2019 04:04:07 PM (4 weeks ago)
Author:
boonebgorges
Message:

Taxonomy: Ensure consistency of hide_empty in term queries when taxonomy is excluded.

When querying for terms in hierarchical categories using hide_empty=true,
results have historically included parent terms which are themselves
unattached to any objects (are "empty") but which have non-empty descendent
terms. Because this process involves walking the descendant tree, we avoid it
when we detect that the queried taxonomies are not hierarchical. (This
behavior was introduced in [5525].)

When the taxonomy parameter of get_terms() was made optional - see #35495,
[36614] - it affected the mechanism for avoiding unneccessary tree walks,
since there may not be any explicitly declared taxonomies to run through
is_taxonomy_hierarchical(). As a result, term queries excluding taxonomy
did not check descendants, and empty parents with non-empty children were not
included in hide_empty results.

We correct the behavior by crawling term descendants when the taxonomy
argument is absent, which means that we're querying for terms in all taxonomies.

Props smerriman.
Fixes #37728.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r45723 r45888  
    327327                }
    328328            }
     329        } else {
     330            // When no taxonomies are provided, assume we have to descend the tree.
     331            $has_hierarchical_tax = true;
    329332        }
    330333
  • trunk/tests/phpunit/tests/term/query.php

    r45584 r45888  
    768768        return array( 555 );
    769769    }
     770
     771    /**
     772     * @ticket 37728
     773     */
     774    public function test_hide_empty_should_include_empty_parents_of_nonempty_children() {
     775        register_taxonomy(
     776            'wptests_tax',
     777            'post',
     778            array(
     779                'hierarchical' => true,
     780            )
     781        );
     782
     783        $t1 = self::factory()->term->create(
     784            array(
     785                'taxonomy' => 'wptests_tax',
     786            )
     787        );
     788
     789        $t2 = self::factory()->term->create(
     790            array(
     791                'taxonomy' => 'wptests_tax',
     792                'parent'   => $t1,
     793            )
     794        );
     795
     796        $p = self::factory()->post->create();
     797
     798        wp_set_object_terms( $p, $t2, 'wptests_tax' );
     799
     800        $q = new WP_Term_Query(
     801            array(
     802                'taxonomy'   => 'wptests_tax',
     803                'hide_empty' => true,
     804                'fields'     => 'ids',
     805            )
     806        );
     807
     808        $this->assertContains( $t1, $q->terms );
     809    }
     810
     811    /**
     812     * @ticket 37728
     813     */
     814    public function test_hide_empty_should_include_empty_parents_of_nonempty_children_when_category_is_unspecified() {
     815        register_taxonomy(
     816            'wptests_tax',
     817            'post',
     818            array(
     819                'hierarchical' => true,
     820            )
     821        );
     822
     823        $t1 = self::factory()->term->create(
     824            array(
     825                'taxonomy' => 'wptests_tax',
     826            )
     827        );
     828
     829        $t2 = self::factory()->term->create(
     830            array(
     831                'taxonomy' => 'wptests_tax',
     832                'parent'   => $t1,
     833            )
     834        );
     835
     836        $p = self::factory()->post->create();
     837
     838        wp_set_object_terms( $p, $t2, 'wptests_tax' );
     839
     840        $q = new WP_Term_Query(
     841            array(
     842                'hide_empty' => true,
     843                'fields'     => 'ids',
     844            )
     845        );
     846
     847        $this->assertContains( $t1, $q->terms );
     848    }
    770849}
Note: See TracChangeset for help on using the changeset viewer.