WordPress.org

Make WordPress Core

Opened 21 months ago

Closed 20 months ago

Last modified 20 months 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 Owned by: 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 21 months ago.
41010.2.patch (2.2 KB) - added by atanasangelovdev 21 months ago.
Added a unit test
41010.3.patch (2.0 KB) - added by boonebgorges 21 months ago.

Download all attachments as: .zip

Change History (14)

#1 @Howdy_McGee
21 months 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
21 months ago

#2 @bor0
21 months ago

  • Keywords has-patch added

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

#3 @peterwilsoncc
21 months 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
21 months 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
21 months ago

  • Milestone changed from Awaiting Review to 4.9

@bor0 I've put this against 4.9

@atanasangelovdev
21 months ago

Added a unit test

#6 @atanasangelovdev
21 months 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
21 months 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.


21 months ago

#9 @peterwilsoncc
20 months ago

  • Keywords commit added

@boonebgorges,

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

#10 @boonebgorges
20 months 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
20 months 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.