#42860 closed defect (bug) (fixed)
PHP 7.2 warning - Parameter must be an array or an object that implements Countable in /wp-includes/class-wp-query.php on line 3035
Reported by: | lisota | Owned by: | jorbin |
---|---|---|---|
Milestone: | 4.9.3 | Priority: | normal |
Severity: | normal | Version: | 5.1 |
Component: | Query | Keywords: | fixed-major |
Focuses: | Cc: |
Description
Upgrading to PHP 7.2 results in the following PHP warning from class-wp-query.php
PHP Warning: count(): Parameter must be an array or an object that implements Countable in /wp-includes/class-wp-query.php on line 3035
Attachments (4)
Change History (30)
#2
@
7 years ago
- Keywords reporter-feedback added
Thanks for the report.
- Are you able to determine the source of the error (ie. where the query is called from)?
- Does it still occur with all your plugins deactivated?
- What's the value of
$this->posts
at this point?
Thanks
#4
@
7 years ago
It looks like the $this->posts
variable was left default null
, causing the warning when called with count()
.
Attached a patch that might solve it, but we should really be waiting for the reporter's information. A backtrace can explain a lot.
#5
@
7 years ago
Above travis URL is wrong. Please see this: https://travis-ci.org/Ayesh/wordpress-develop/builds/314425033
#6
follow-up:
↓ 7
@
7 years ago
After looking over this, I can only assume that there's a caching plugin in play which has set $this->request
to null
or ''
which causes $this->posts
to be set to the same thing instead of an array as expected.
The correct fix here is probably to ensure that $this->posts
is always set to an array, even when $this->request
is emptied out.
#7
in reply to:
↑ 6
@
7 years ago
Replying to dd32:
The correct fix here is probably to ensure that
$this->posts
is always set to an array, even when$this->request
is emptied out.
The comment in WP_Query::set_found_posts() specifically allows for $this->posts
to not be an array though:
// Bail if posts is an empty array. Continue if posts is an empty string, // null, or false to accommodate caching plugins that fill posts later.
I think we should just check if is_array()
before calling count()
.
#8
@
7 years ago
- Version changed from 4.9.1 to trunk
Let me see if I can grab a backtrace. I rolled out PHP 7.2 on our production site, and experienced a number of errors related to 7.2, including this one, and promptly rolled back.
#10
@
7 years ago
I agree with doing an array check first, could be something very simple:
$this->found_posts = 0;
if ( is_array( $this->posts ) ) {
$this->found_posts = count( $this->posts );
}
This ticket was mentioned in Slack in #core by desrosj. View the logs.
7 years ago
#13
@
7 years ago
- Owner set to jorbin
- Resolution set to fixed
- Status changed from new to closed
In 42581:
#14
@
7 years ago
- Keywords fixed-major added; reporter-feedback needs-unit-tests removed
- Resolution fixed deleted
- Status changed from closed to reopened
#17
@
7 years ago
To clarify a bit, [42581] does keep the previous behavior, I'm just not sure it's correct. If $this->posts
is false
or an empty string, I'd expect found_posts
to be 0.
#18
in reply to:
↑ 16
;
follow-up:
↓ 21
@
7 years ago
Replying to SergeyBiryukov:
[42581] doesn't look complete to me. Per comment:7, an empty string or
false
should be treated the same asnull
.
That's not how count
works. It would be a breaking change to set it as 0 in those instances.
From http://php.net/manual/en/function.count.php#refsect1-function.count-returnvalues
Returns the number of elements in array_or_countable. When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned.
This ticket was mentioned in Slack in #core by jorbin. View the logs.
7 years ago
#21
in reply to:
↑ 18
@
7 years ago
- Keywords fixed-major added; needs-patch removed
Replying to jorbin:
It would be a breaking change to set it as 0 in those instances.
Right, on second thought I agree breaking back compat is not worth it. Let's add 42860.diff then.
This ticket was mentioned in Slack in #core by jorbin. View the logs.
7 years ago
#25
@
7 years ago
Is this really fixed? I'm still getting this warning in sites under PHP 7.2
For what it's worth, the line in question is
$this->found_posts = count( $this->posts );
insidefunction set_found_posts()