[wp-trac] [WordPress Trac] #47824: get_the_content() still causes a PHP warning outside of the loop

WordPress Trac noreply at wordpress.org
Sat Aug 3 21:17:43 UTC 2019


#47824: get_the_content() still causes a PHP warning outside of the loop
-------------------------------+----------------------------------------
 Reporter:  SergeyBiryukov     |      Owner:  (none)
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  5.3
Component:  Posts, Post Types  |    Version:  5.2
 Severity:  normal             |   Keywords:  has-patch needs-unit-tests
  Focuses:                     |
-------------------------------+----------------------------------------
 Background: #42814 / [44941]

 Current state of things on PHP 7.2+:
 * Calling `the_excerpt()` before the loop works as expected, displaying
 the excerpt of a post in the `$post` global.
 * Calling `the_content()` before the loop results in a PHP warning:
 `count(): Parameter must be an array or an object that implements
 Countable in wp-includes/post-template.php on line 310`

 `get_the_excerpt()`, when called without an explicit post object, falls
 back to the `$post` global, then passes the post object to
 `wp_trim_excerpt()`, which passes it to `get_the_content()`, and the
 latter [source:tags/5.2.2/src/wp-includes/post-template.php?marks=282#L279
 calls generate_postdata()].

 `get_the_content()`, when called without an explicit post object, falls
 back to the `$post` global too, but makes an incorrect assumption that
 other globals (specifically `$pages`) are always set up in that case.

 If calling `the_content()` outside of the loop should be disallowed, then
 an appropriate `_doing_it_wrong()` message should be added instead of the
 cryptic PHP warning.

 If calling `the_content()` outside of the loop should still be supported
 and properly fall back to the `$post` global, then `in_the_loop()` check
 should be added to the condition that determines when to rely on the
 globals.

 I'm leaning towards the latter, for consistency with `get_the_excerpt()`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47824>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list