WordPress.org

Make WordPress Core

Opened 7 months ago

Closed 6 weeks ago

Last modified 6 weeks ago

#46322 closed defect (bug) (duplicate)

PHP 7.2 Warning

Reported by: good3n Owned by: desrosj
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: php73
Focuses: Cc:

Description

I get the PHP warning

Warning: count(): Parameter must be an array or an object that implements Countable in /path/to/wordpress/wp-includes/post-template.php on line 293

every time I programmatically create a post with wp_insert_post()

If I go back into the post via the admin UI and update it, the warning goes away.

I'm assuming because PHP 7.2 treats this as NULL.

If I edit the /wp-includes/post-template.php file it removes the warning.

Before (Warning):

<?php
if ( $page > count( $pages ) ) { // if the requested page doesn't exist
  $page = count( $pages ); // give them the highest numbered page that DOES exist
}

After (No Warning):

<?php
if(!empty($pages)) {
  if ( $page > count( $pages ) ) { // if the requested page doesn't exist
    $page = count( $pages ); // give them the highest numbered page that DOES exist
  }
}

Change History (6)

#1 @desrosj
7 months ago

  • Keywords php73 added
  • Owner set to desrosj
  • Status changed from new to reviewing

#2 @jorbin
7 months ago

@good3n Can you provide the code you are using to programmatically create a post?

Does this happen with a default theme and no plugins active on the site?

#3 @swissspidy
7 months ago

Sounds like a duplicate of #42814

#4 @wowthemesnet
6 months ago

I understand it's a duplicate but that ticket is closed.

Here is how I was able replicate the error Warning: count(): Parameter must be an array or an object that implements Countable in ... post-template.php on line 293 on the default TwentyNinenteen theme:

The template was created following Pippin's [EDD guide](https://docs.easydigitaldownloads.com/article/234-creating-a-customer-account-page)

PHP 7.2 , 7.3

Last edited 6 months ago by wowthemesnet (previous) (diff)

#5 @l3rady
6 months ago

Similar issue reported over at: #46445

Last edited 6 weeks ago by SergeyBiryukov (previous) (diff)

#6 @SergeyBiryukov
6 weeks ago

  • Component changed from General to Posts, Post Types
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from reviewing to closed

Hi there, welcome to WordPress Trac! Thanks for the ticket.

  • #42814 fixed this for get_the_excerpt().
  • #47824 is a follow-up fix for get_the_content().

Closing as a duplicate of the above tickets.

Last edited 6 weeks ago by SergeyBiryukov (previous) (diff)
Note: See TracTickets for help on using tickets.