Make WordPress Core

Opened 21 months ago

Last modified 21 months ago

#56590 new defect (bug)

TypeError error in get_the_content when $elements['page'] and $elements['pages'] are null

Reported by: cantbelieveitsnotbutter's profile cantbelieveitsnotbutter Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0.2
Component: Posts, Post Types Keywords: has-patch changes-requested needs-testing-info
Focuses: Cc:


if ( $elements['page'] > count( $elements['pages'] ) ) in file wp-includes/post-template.php (currently on line 319) throws the following error when those array items are null:

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /wp-includes/post-template.php:319

A case where they would be null is a search request with no results where a plugin like Timber is used to assign $post a non-WordPress-core value. Adding the condition if ( ! is_null( $elements['page'] ) && ! is_null( $elements['pages'] ) ) solves the problem.

Change History (3)

#1 @cantbelieveitsnotbutter
21 months ago

Another, and perhaps better, solution is to use type conditions, e.g., if ( 'integer' === gettype( $elements['page'] ) && 'array' === gettype( $elements['pages'] ) ).

This ticket was mentioned in PR #3266 on WordPress/wordpress-develop by houblon.

21 months ago

  • Keywords has-patch added

Adds an if statement for the variable types of $elements['page'] and $elements['pages'] in function get_the_content, to address cases where they would be a different type than expected, including null, such as for a search request with no results where a plugin like Timber is used to assign $post a non-WordPress-core value.

Trac ticket:

#3 @ironprogrammer
21 months ago

  • Component changed from Bootstrap/Load to Posts, Post Types
  • Keywords changes-requested needs-testing-info added

Welcome to Trac, @cantbelieveitsnotbutter, and thank you for the ticket and follow-up PR! 🙌🏻🙌🏻

I believe this ticket would do well to have detailed steps to reproduce the error you're encountering. You mentioned a specific plugin, but adding repro info with setup/sample code that triggers the error would be very helpful. I've added keyword needs-testing-info to this effect.

While it may be premature to consider a patch -- the error should ideally be reproducible by additional contributors -- I did notice that there are some coding standards checks and E2E test issues that should be reviewed. Please refer to WordPress's PHP Coding Standards for more info.

Note: See TracTickets for help on using tickets.