WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#29851 closed defect (bug) (fixed)

term_exists gives back the wrong term when it has parents

Reported by: allaerd Owned by: boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 4.0
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

when i have this terms

server->hp->ram

and i call

term_exists( 'hp' , 'product_cat', 0);

i expect to get back false

when i have this

hp->ram

i expect to get back a positive result

Attachments (1)

29851.patch (2.3 KB) - added by boonebgorges 6 years ago.

Download all attachments as: .zip

Change History (6)

@boonebgorges
6 years ago

#1 @boonebgorges
6 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.1
  • Owner set to boonebgorges
  • Status changed from new to accepted

Confirmed. The problem is that passing a parent of 0 causes the parent clause to be bypassed. That means that there is no way to use term_exists() and filter by top-level terms.

29851.patch contains a unit test demonstrating the problem, as well as a suggested fix. In brief: change the default value of $parent from 0 to null, and build the parent SQL clause whenever the value of $parent is numeric. Use of term_exists() with no parent param will remain the same, as will its use when passing a non-zero integer. It fixes this bug (passing a 0 or '0' is now interpreted correctly).

allaerd, is it possible for you to test this change to see if it solves your problem?

#2 @allaerd
6 years ago

Hi boonebgorges

i tested the solution and it now works as expected!

Thanks and regards,

Allaerd

#3 @boonebgorges
6 years ago

Thanks for the confirmation, allaerd.

#4 @boonebgorges
6 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 29863:

Fix term_exists() for parent = 0.

Passing a 0 (or '0') as the 'parent' param of term_exists() should limit
results to terms with no parent.

Adds unit test.

Fixes #29851.

#5 @nacin
6 years ago

[29863] looks good.

Note: See TracTickets for help on using tickets.