WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#40671 closed defect (bug) (fixed)

Calling get_term() with just ID, then calling it again with ID and a different taxonomy returns the same term

Reported by: GunGeekATX Owned by: boonebgorges
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.7.4
Component: Taxonomy Keywords:
Focuses: Cc:

Description

I have a category term ID of 29, and calling get_term() with that ID returns the category. However, if get_term() is called again with both ID and 'post_tag' taxonomy, the original category term is returned.

It appears that the $_term variable is not getting cleared out in WP_Term::get_instance() when there is a term already in the cache, so it's always using that term even when the taxonomy does not match.

Some sample code to replicate this:

<?php
add_action( 'init', function() {

        // Use a valid category term to replicate this.
        $category_term_id = 29;

        echo '<pre>';

        // Shows the correct category term.
        $term = get_term( $category_term_id );
        var_dump( $term );

        // Still shows the category term, but should be empty
        // due to the different taxonomy.
        $term = get_term( $category_term_id, 'post_tag' );
        var_dump( $term );

        die();
} );

Working on a patch, bug was originally brought to light by @ninnypants in our company Slack while working on a project.

Attachments (3)

40671.patch (784 bytes) - added by GunGeekATX 3 years ago.
40671-2.patch (520 bytes) - added by GunGeekATX 3 years ago.
Correct patch without my var_dump
40671-3.patch (528 bytes) - added by GunGeekATX 3 years ago.
Updated patch made from correct directory

Download all attachments as: .zip

Change History (8)

@GunGeekATX
3 years ago

@GunGeekATX
3 years ago

Correct patch without my var_dump

@GunGeekATX
3 years ago

Updated patch made from correct directory

#1 @GunGeekATX
3 years ago

@boonebgorges Adam Silverstein suggested I ping you on this ticket

#2 @boonebgorges
3 years ago

  • Milestone changed from Awaiting Review to 4.9

@GunGeekATX Thanks for the ping, and for the ticket + patch. I've confirmed the issue and written a unit test to match. Your suggested fix looks good to me - I'm going to make a minor modification to the order in which it runs, and go with it.

#3 @boonebgorges
3 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 40979:

Taxonomy: Ignore cached term value when it doesn't match the queried taxonomy.

When a cache entry is found that matches the requested $term_id, but
doesn't match an explicitly provided $taxonomy, that cache entry
should be ignored.

Props GunGeekATX.
Fixes #40671.

#4 @boonebgorges
3 years ago

In 42364:

Don't do a strict taxonomy check in get_category_link().

Prior to version 4.9, a quirk in the implementation of get_term() caused
get_category_link( 123 ) to fetch the taxonomy archive link for term 123
even if 123 is not in the 'category' taxonomy. The quirk was fixed in [40979];
see #40671. This bugfix introduced a regression for theme authors who were
expecting the old behavior.

By lifting the 'category' restriction, we allow the template function to work
in the old way.

Fixes #42717. See #42771.

#5 @boonebgorges
3 years ago

In 42369:

Don't do a strict taxonomy check in get_category_link().

Prior to version 4.9, a quirk in the implementation of get_term() caused
get_category_link( 123 ) to fetch the taxonomy archive link for term 123
even if 123 is not in the 'category' taxonomy. The quirk was fixed in [40979];
see #40671. This bugfix introduced a regression for theme authors who were
expecting the old behavior.

By lifting the 'category' restriction, we allow the template function to work
in the old way.

Merges [42364], [42365] to the 4.9 branch.

Fixes #42717. See #42771.

Note: See TracTickets for help on using tickets.