user3612498
user3612498

Reputation: 157

PHP error: Warning: count(): Parameter must be an array or an object that implements Countable

I updated my Wordpress site from PHP 5.6 to 7.2 and noticed an error in my Wordpress backend after the update. It says:

Warning:  count(): Parameter must be an array or an object that implements 
Countable in <b>/homepages/36/d362586048/htdocs/genag/wp- 
content/themes/genag- 
theme/framework/admin/functions/functions.mediauploader.php on line 127

I have included the code from that line and 2 lines below it. Any help would be appreciated.

if ( count( $_posts ) ) {
$_id = $_posts->ID;
} else {

Upvotes: 3

Views: 18846

Answers (3)

Dakshim Chhabra
Dakshim Chhabra

Reputation: 98

In PHP 7.2, count() method does not support Null as a parameter.

I have got the same error, in one of my old Avada theme based wordpress website.

I have solved it using the following modification.

if ( (!empty($_posts)) && (count( $_posts ) ) { $_id = $_posts->ID; } else {

Upvotes: 2

Aji
Aji

Reputation: 111

http://php.net/manual/en/function.count.php

You can use is_countable() function in php to check whether an object supports count functionality.

Upvotes: 0

Matthew Page
Matthew Page

Reputation: 756

$_posts appears to be an object, you should use it like an object $_posts->ID. So it cannot be counted like an array.

if ( $_posts ) {

Should do the job

Upvotes: 5

Related Questions