Make WordPress Core

Opened 12 years ago

Closed 10 years ago

#21606 closed defect (bug) (worksforme)

term_exists returns 0 and null

Reported by: barrykooij's profile barrykooij Owned by: barrykooij's profile barrykooij
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

The function term_exists in wp-includes/taxonomy.php has two different return values, 0 and null. The function does a few checks and returns 0 if the condition of the check is not met. If the checks are met, the return value will be the result of a $wpdb->get_row or $wpdb->get_var. If the term is not found in the database the methods get_row and get_var will return null.

Because of the different type of return values it's not possible to do a strict check on the return value to see if a term was found. I've changed the return 0 in term_exists to return null, so that if there is no term found the result of term_exists will always be null.

Attachments (2)

taxonomy.php.patch (1.2 KB) - added by barrykooij 12 years ago.
Taxonomy Patch
21606.diff (2.3 KB) - added by nacin 12 years ago.

Download all attachments as: .zip

Change History (9)

@barrykooij
12 years ago

Taxonomy Patch

#1 @scribu
12 years ago

  • Milestone changed from Awaiting Review to 3.5

#2 follow-up: @ocean90
12 years ago

Should probably do the same with post_exists.

#3 in reply to: ↑ 2 @barrykooij
12 years ago

Replying to ocean90:

Should probably do the same with post_exists.

Thanks, created a new ticket for this. Please see #21607

Last edited 12 years ago by barrykooij (previous) (diff)

#4 @scribu
12 years ago

You can upload another patch on this ticket, since it seems similar enough.

#5 @SergeyBiryukov
12 years ago

Because of the different type of return values it's not possible to do a strict check on the return value to see if a term was found.

Consistency is good, but I wonder where the need for a strict check comes from.

It should be possible to check with if ( ! term_exists( 'events', 'post_tag' ) ) if a term was not found, unless I'm missing something.

@nacin
12 years ago

#6 @nacin
12 years ago

  • Milestone changed from 3.5 to Future Release

#7 @boonebgorges
10 years ago

  • Milestone Future Release deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Since [29865], term_exists() has returned null in all cases where no term is found. This is the opposite of what was suggested in 21606.diff (which suggests returning 0 in these cases), but it does make return values consistent in case of failure, which is what matters for this ticket.

Note: See TracTickets for help on using tickets.