Make WordPress Core

Opened 7 years ago

Last modified 3 years ago

#39678 new defect (bug)

get_term_by slug with "0" as value

Reported by: fausonealessio's profile fausonealessio Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch has-unit-tests
Focuses: Cc:


Executing this code


my expectations were to obtain a taxonomy term, considering that one actually exists in my database, but instead the function returned false due to the PHP empty function returning true (see
The empty function behaves according to documentation ( but the string "0" should be accepted to be searched if it is possible to use it to create a taxonomy term.

Change History (2)

This ticket was mentioned in PR #1654 on WordPress/wordpress-develop by costdev.

3 years ago

  • Keywords has-patch has-unit-tests added

wp_insert_term() and wp_update_term() did not allow a term to be given a slug of (string) '0'.

This was due to:

  1. Sanitizing the provided value; this would turn (int) 0 into (string) '0'.
  2. Using empty() to check for an empty value; this returns true for (string) '0'.

This gave a false sense of security, as it correctly captured a disallowed value of (int) 0 and generated a slug from the term name. Unfortunately, this process also incorrectly prevented a value of (string) '0', which the docs suggest is an allowed value.

This patch:

  1. Stores whether or not the slug was (string) '0' prior to sanitization and adds this to an existing conditional (only in one case).
  2. Adds additional checks for '0' === $args['slug'], '0' !== $slug, etc. as needed.
  3. Adds unit tests for wp_insert_term() and wp_update_term().

Trac ticket:

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

3 years ago

Note: See TracTickets for help on using tickets.