Opened 4 years ago
Closed 3 months ago
#55358 closed defect (bug) (fixed)
Passing int term term_exists parent param not respected
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Taxonomy | Keywords: | good-first-bug has-patch has-unit-tests |
| Focuses: | Cc: |
Description
If a developer calls terms_exists, with an int as term and parent as an int, the parent value is not respected. So example this will not work.
$term = term_exists( 123, 'category', 1);
The code will check to see if term 123 exists, but will ignore parent value. This may result in correct / unexpected results.
Attachments (3)
Change History (17)
#2
@
4 years ago
- Keywords has-patch added; needs-patch needs-unit-tests removed
Hey, I've added a patch for that. Now it is going to include $parent in the query even when $term is int
#4
@
21 months ago
- Milestone changed from Awaiting Review to Future Release
@lgadzhev Thanks for your patch. Is it possible you could provide unit tests?
This ticket was mentioned in PR #6790 on WordPress/wordpress-develop by @thehercules.
17 months ago
#5
Fixes Ticket #55358
This PR addresses an issue in the term_exists function where the parent_term was not being respected when a term with an integer ID was passed along with a parent term ID. The function would only check if the term with the specified ID existed, ignoring the parent-child relationship.
Changes Made:
- Updated the term_exists function to include the parent parameter in the query arguments when the term is an integer and a parent term ID is specified.
- Added a condition to check for the presence of a parent term ID and include it in the query arguments.
#6
@
17 months ago
@spacedmonkey I have created a Patch https://github.com/WordPress/wordpress-develop/pull/6790. Could you review it and suggest changes if any.
#7
@
10 months ago
@thehercules Sorry for the late reply.
Looking at your PR. How about instead of adding
if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
$args['parent'] = (int) $parent_term;
}
into the if ( is_int( $term ) ) { statement. Move it before like this.
$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent_term );
if ( ! empty( $taxonomy ) && is_numeric( $parent_term ) ) {
$args['parent'] = (int) $parent_term;
}
if ( is_int( $term ) ) {
that way you would not have repeated logic.
I would like to see a unit test here if possible as well. Thanks for your contribution.
#8
follow-up:
↓ 9
@
8 months ago
- Keywords needs-unit-tests added; needs-testing removed
This ticket needs unit tests.
#9
in reply to:
↑ 8
@
6 months ago
Replying to spacedmonkey:
This ticket needs unit tests.
Hello, i was going to apply the patch and test it, but noticed the codebase changed a LOT.
A wrote a plugin to test if the term_exists() function now properly checks if a term exists under a specified parent when using a term ID.
Testing involved creating a parent term and a child term within a hierarchical taxonomy (e.g., Categories). The child term’s ID was passed to term_exists() alongside the parent’s ID. The function returned the child term’s data only if it was correctly nested under the parent, confirming the parent parameter is respected. This proves the original issue (parent check being ignored) no longer occurs. The ticket can be closed.
The plugin that i used to test is here:
https://github.com/N1co1asB1ancon1/test_term_exists_work_wp_bug
You have to create a parent and a child in the posts->category, put their respective ID's into the plugin and execute the plugin, for me it printed:
Testing term_exists() with child slug (123) and parent ID (2) in taxonomy "category": Result: Found term and respected parent. (Expected correct behavior)
That it guys, what do you think of it? Mind you, this is my very first ever OS contribution. Thank you
This ticket was mentioned in PR #8830 on WordPress/wordpress-develop by @hugod.
6 months ago
#10
- Keywords has-unit-tests added; needs-unit-tests removed
Adds a PHPUnit tests for term_exists() with $term as integer and non null $parent_term.
Follows https://github.com/WordPress/wordpress-develop/pull/6790
Trac ticket: https://core.trac.wordpress.org/ticket/55358
#11
@
5 months ago
- Milestone changed from Future Release to 6.9
This ticket was addressed during WordCamp Toulouse contributor day. Unit tests were added so moving it for 6.9 consideration.
This ticket was mentioned in PR #9092 on WordPress/wordpress-develop by @vijendrajat.
5 months ago
#12
This PR fixes a bug in the term_exists function where the parent_term wasn’t being considered correctly. When a term ID (as an integer) was passed along with a parent term ID, the function would only check if the term ID existed and completely ignore whether it was under the specified parent.
- Updated the
term_existslogic to include the parent in the query when both a term ID and parent term ID are provided. - Added a condition to ensure that the parent is checked as part of the query when relevant.
Trac ticket: https://core.trac.wordpress.org/ticket/55358
@
3 months ago
Makes term_exists() respect the $parent parameter ✅ Ensures accurate checks for terms under different parents ✅ Prevents false “term already exists” errors in hierarchical taxonomies ✅ Improves reliability for wp_insert_term() and related functions
#13
@
3 months ago
At Contributor day - Tested PR - 9092
https://github.com/WordPress/wordpress-develop/pull/9092
Manually tested
Parent term id = 1.
Child term id = 2.
term_exists ( 2 , 'category', 1 ); // Array of child - (this is good, 2 is a child of 1) term_exists ( 2 , 'category', 2 ); // NULL - (this is good, 2 is not a child of 2) term_exists ( 2 , 'category', 'lol' ); // Array of child - (this is good, no term 'lol' - return child term.)
PHPUnit tests passing.
npm run test:php -- --group 55358
Looks more like a problem in the docs to me than an actual bug: if you give the ID as integer you don't need to restrict by parent; if you search by string, restricting by parent can definitely be helpful.