Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#41010 closed defect (bug) (fixed)

wp_get_object_terms() returns duplicate terms if more than one taxonomy is given in args

Reported by: mranner's profile mranner Owned by: boonebgorges's profile boonebgorges
Milestone: 4.8.1 Priority: normal
Severity: normal Version: 4.8
Component: Taxonomy Keywords: has-patch dev-feedback commit
Focuses: Cc:

Description

works correct

$list = wp_get_object_terms([ 104627 ], [ 'produkte' ]);
print_r($list);

returns all terms for produkte and herkunft and merges all terms related to the post

$list = wp_get_object_terms([ 104627 ], [ 'produkte', 'herkunft' ]);
print_r($list);

Problem is unset( $taxonomies[ $index ] ) followed by $terms = array_merge( $terms, get_terms( $args ) );

foreach ( $taxonomies as $index => $taxonomy ) {} iterates for all given taxonomies and leaves an empty $taxonomies array. get_terms() with an empty array for $argstaxonomy? returns "all" related terms and merges with the terms of the previews loop run.

Michael

Attachments (3)

41010.patch (701 bytes) - added by bor0 7 years ago.
41010.2.patch (2.2 KB) - added by atanasangelovdev 7 years ago.
Added a unit test
41010.3.patch (2.0 KB) - added by boonebgorges 7 years ago.

Download all attachments as: .zip

Change History (14)

#1 @Howdy_McGee
7 years ago

I'm not sure I follow the latter half of your issue. I've tested wp_get_object_terms() with Post array( 'category', 'post_tag' ); and I get the expected results, no duplicates. Can you list steps to reproduce this issue?

@bor0
7 years ago

#2 @bor0
7 years ago

  • Keywords has-patch added

Please use the following code to reproduce: https://gist.github.com/bor0/109e091c4aa625cbe670766db53ae5e1.

#3 @peterwilsoncc
7 years ago

  • Keywords needs-unit-tests added

@bor0,

Thanks for including the gist with some test data. In the WordPress Develop repository (git | svn) there is a tests folder for phpunit.

Are you able to take a look at converting your gist to unit tests.

Pete

#4 @bor0
7 years ago

@peterwilsoncc sounds good to me.

I'll try to supply a UT sometime today or tomorrow.

In the meanwhile, can you assign a milestone for this so that it doesn't get lost?

Thanks!

#5 @peterwilsoncc
7 years ago

  • Milestone changed from Awaiting Review to 4.9

@bor0 I've put this against 4.9

@atanasangelovdev
7 years ago

Added a unit test

#6 @atanasangelovdev
7 years ago

  • Keywords needs-unit-tests removed

I've added a unit test to the patch with comments about what triggers the error (the unsupported "args" key).
Not sure about the test name - it probably needs a better one.

#7 @boonebgorges
7 years ago

  • Keywords dev-feedback added
  • Milestone changed from 4.9 to 4.8.1

@bor0 and @atanasangelovdev - Thanks for the patch and especially for the unit test, which helped me to understand the issue better.

This is a regression caused by [40514] in 4.7.5. For this reason, I think it's appropriate to fix in 4.8.1. In order to include in a minor release, I'd appreciate a review by another committer. @peterwilsoncc Since you've commented here, would you mind verifying the issue and the fix offered in 41010.3.patch (which does some slight modification to the code style in the unit test to better align with WP standards)?

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 years ago

#9 @peterwilsoncc
7 years ago

  • Keywords commit added

@boonebgorges,

I've checked this out, verified the issue exists and the fix works.

#10 @boonebgorges
7 years ago

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

In 41037:

Taxonomy: Avoid duplicates when querying for terms in taxonomies registered with $args parameter.

[40514] introduced a bug that caused term queries to return some duplicates
when the $taxonomies array contained only taxonomies that were originally
registered with an $args array. We fix this bug by ensuring that
recursive get_terms() queries stop when all queried $taxonomies have
already been referenced.

Props bor0, atanasangelovdev.
Fixes #41010.

#11 @boonebgorges
7 years ago

In 41038:

Taxonomy: Avoid duplicates when querying for terms in taxonomies registered with $args parameter.

[40514] introduced a bug that caused term queries to return some duplicates
when the $taxonomies array contained only taxonomies that were originally
registered with an $args array. We fix this bug by ensuring that
recursive get_terms() queries stop when all queried $taxonomies have
already been referenced.

Ports [41037] to the 4.8 branch.

Props bor0, atanasangelovdev.
Fixes #41010.

Note: See TracTickets for help on using tickets.