Opened 6 years ago
Last modified 5 years ago
#47314 new feature request
Add "parent" parameter to get_term_by
Reported by: | leogermani | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Taxonomy | Keywords: | |
Focuses: | Cc: |
Description
There has been some discussion around some inconsistencies in the way term_exists()
handles the input string and sometimes can have unexpeceted results. The most recent discussions about this can be seen in #47099, #45333 and #32248.
The current recommendation is to use get_term_by()
instead, so you can explicitly inform what field you are checking and get the right result.
However, get_term_by()
does not support a parent
argument, which makes it not a full term_exists()
replacement.
In #47099 @boonebgorges suggested we opened an enhancement ticket to discuss this addition to this function. And here it is! :)
Looking at the function and all parameters it gets, we would have to decide how to support this.
This would be the first question to ask if one was to try an approach for this. Adding as a new parameter does not look so good because there already many and it would force the developer to use all of them if he/she wanted to use parent
. However adding support for a field like name_and_parent
or something like this, and having an array passed as the $value
doesn't look so pretty either.
What do you think?
Change History (2)
#1
in reply to:
↑ description
;
follow-up:
↓ 2
@
5 years ago
#2
in reply to:
↑ 1
@
5 years ago
Replying to SergeyBiryukov:
One option that comes to mind is to accept an array as the
$field
parameter, something like this:
get_term_by( array( 'field' => 'name', 'parent' => $parent_term_id ), 'Child term', $taxonomy )At that point, though, it seems like we're just reinventing
get_terms()
with'number' => 1
:
get_terms( array( 'name' => 'Child term', 'parent' => $parent_term_id, 'taxonomy' => $taxonomy, 'number' => 1 )
Thanks for the reply!
get_term_by
already uses get_terms
internally... so it is basically a shortcut to get_terms
with some fixed parameters indeed. So you have a point, if we add too many options to it, it misses the point of being a shortcut.
I have been using get_terms
as a replacement for term_exists
and it works well.
I think we have to ask:
- should we deprecate
term_exists()
? I understand it's not desirable to change its behavior because it could break things
I think that yes, we should deprecate it (if fixing it by adding a 'field' parameter isn't really an option). We have a function that is not reliable.
If we deprecate it, we should recommend the new way of doing so, which could be
- recommending
get_terms
, - adding
parent
option toget_term_by
or - creating a new function.
I particularly think that this is something very useful and would be nice to have something specific rather than having to use get_terms
.
Replying to leogermani:
One option that comes to mind is to accept an array as the
$field
parameter, something like this:At that point, though, it seems like we're just reinventing
get_terms()
with'number' => 1
: