WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 3 months ago

#46660 reopened defect (bug)

PHP Warning: Countable Error in update_metadata()

Reported by: betimademi Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch reporter-feedback
Focuses: Cc:

Description (last modified by SergeyBiryukov)

i have updated to the latest wordpress and php 7.3 but somehow i get this error over and over again

PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /srv/users/.../apps/.../public/wp-includes/meta.php on line 210: 

// Compare existing value to new value if no prev value given and the key exists only once.
	if ( empty( $prev_value ) ) {
		$old_value = get_metadata( $meta_type, $object_id, $meta_key );
		if ( count( $old_value ) == 1 ) {
			if ( $old_value[0] === $meta_value ) {
				return false;
			}
		}
	}

Attachments (1)

46660.patch (602 bytes) - added by shashwatmittal 5 months ago.

Download all attachments as: .zip

Change History (11)

#1 @garrett-eclipse
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi @betimademi

The get_metadata function returns either a string, array or boolean. If it returns either a string or boolean and you call the count function it's going to error. So do a check in your conditional for is_array prior to your use of count to avoid this error.

As well the Core Trac here is for development of the core and not support. For further assistance with your issue please consult the appropriate channels;
Support - https://wordpress.org/support/
Forums - https://wordpress.org/support/forums/

All the best

#2 follow-up: @jorbin
5 months ago

  • Milestone set to 5.2
  • Resolution invalid deleted
  • Status changed from closed to reopened

The code that @betimademi used is in the update_metadata function.

#3 @jorbin
5 months ago

  • Keywords needs-patch added

Thanks for the ticket @betimademi. By any chance, do you have a stack trace for this? It would be good to add a simple test case that we can use for update_metadata to make sure we get and keep this fixed.

#4 @garrett-eclipse
5 months ago

My bad sorry @betimademi I read that as custom code. I see now that @jorbin pointed it out that it's part of core. Please retract my previous point to support. Seems like in this case the $old_value just needs to undergo the is_array check before using count here;
https://github.com/WordPress/WordPress/blob/2dce7181572186e8aed3de418519fefadd427afd/wp-includes/meta.php#L210

#5 in reply to: ↑ 2 @betimademi
5 months ago

Yes its correct @jorbin its in update_metadata function in meta.php file, and its part of core. I saw this error in php7.3.error.log

#6 @SergeyBiryukov
5 months ago

  • Component changed from General to Options, Meta APIs
  • Description modified (diff)

#7 @shashwatmittal
5 months ago

  • Keywords has-patch added; needs-patch removed

#8 @pento
5 months ago

  • Keywords reporter-feedback added
  • Milestone changed from 5.2 to Future Release

Thank you for the patch, @shashwatmittal!

Before we commit it, however, I'd like to know how to reproduce this error: after a bit of debugging, I was unable to determine how get_metadata() was returning a non-array value when it's called in update_metadata(). If there's something causing the the meta cache to become corrupted, for example, we need to fix that, rather than paper over it.

@betimademi: Could I get you to add this to your wp-config.php file?

<?php
set_error_handler(
        function( $errno, $errstr, $errfile, $errline ) {
                error_log( "PHP Warning: $errstr in $errofile on line $errline" );
                error_log( print_r( debug_backtrace(), true ) );
                return true;
        },
        E_USER_WARNING
);

This will add the stack trace to your PHP log when an error occurs. I recommend you monitor your log file size carefully, as this extra logging could cause it to fill up very quickly.

Once you have the stack trace related to the original warning, please post it to this ticket, so we can investigate further.

#9 @ocean90
3 months ago

  • Summary changed from PHP Warning: count() to PHP Warning: Countable Error in update_metadata()

#10 @ocean90
3 months ago

#47395 was marked as a duplicate.

Note: See TracTickets for help on using tickets.