PHP warning after install

Welcome @MoJones. As has been discovered in this thread this is caused by theme code doing something unexpected. It’s usually also a good idea to search for these error messages and see if they have been reported by WP users. In this case: #47824 (get_the_content() still causes a PHP warning outside of the loop) – WordPress Trac

This issue has been fixed in WP 5.5 (so the same warning would have also appeared in WP 5.4 and earlier). I’ll put it on the list to fix in an upcoming CP release but it depends on some other changes so we’ll need to be careful with that (and we are too close to our upcoming 1.2.0 release so it won’t make it in there).

I wouldn’t recommend changing wp-includes/post-template.php like that - those couple of lines of code have to do with several different scenarios, mostly related to displaying lists of posts across multiple pages. Also, any changes made there will be overwritten on the next upgrade.

Instead if you can find where your theme code calls the_content() or possibly the_excerpt() outside of “the loop” (i.e. not in between while ( have_posts() ) and endwhile) then we can make a modification there. The file to look in depends on where this issue occurs. For example, if you see this warning when viewing single blog posts or pages on your site then you probably want single.php or page.php.

Anyway once you find a call to any of these four functions the_content(), get_the_content(), the_excerpt(), get_the_excerpt() that is not contained within while ( have_posts() ) you should be able to modify that line as follows.

  • Before: the_content();
  • After: $GLOBALS['pages'] = array(); the_content();

Hope that helps… at least, that should be enough to avoid the current warning if I’ve got my understanding of the issue right.

6 Likes