#42327 closed defect (bug) (fixed)
WP_Term_Query returns an empty array instead of 0
Reported by: | xParham | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 5.1 | Priority: | normal |
Severity: | normal | Version: | 2.8.4 |
Component: | Taxonomy | Keywords: | has-unit-tests has-patch |
Focuses: | Cc: |
Description
When running a term query using the parent
or child_of
query parameters, and count
as the term fields
to query for, if the parent has no descendants the query doesn't respect the fields
parameter and returns an empty array instead of 0.
Attachments (2)
Change History (13)
#5
@
7 years ago
@westonruter That's when the WP_Term_Query
was introduced which borrowed the code from get_terms()
. Avoiding the query and returning an empty array if the queried parent/child_of term has no descendants had been in get_terms()
since [6118] but there was no count
option back then. The bug was actually introduced in [13491] via #10746 when the count
option for the fields
parameter was added but the part which handled the early bailing of the function wasn't updated to return 0 instead of an empty array if a count
is the expected return value.
#7
@
7 years ago
- Keywords has-unit-tests has-patch added
I think the terms
property should also be set as an empty array, so I slightly modified the patch from 42327.patch to:
if ( 'count' == $args['fields'] ) { return 0; } else { $this->terms = array(); return $this->terms; }
to avoid the mismatch between the query()
method returning an empty array and the terms
property being null
, for the case when fields
is not count
.
42327.2.diff contains tests.
#8
@
7 years ago
- Milestone changed from Awaiting Review to 5.0
Thanks for the patch and especially for the excellent tests.
#9
@
7 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 42209:
I have attached a patch.
This in particular for me, is causing a fatal error on the
edit-tags.php
screen, when a filter that I have on the main query for the terms list sets theparent
parameter to one with no descendants.Having an array returned instead of 0 will make the
WP_List_Table::set_pagination_args()
to fail because the pagination args set inWP_Terms_List_Table::prepare_items()
useswp_count_terms()
which returns an array instead of integer in this case, which is then passed as thetotal_items
for the pagination args:Fatal error: Unsupported operand types in /wp-admin/includes/class-wp-list-table.php on line 284