I'm currently working with php 7.2 and when I use get_the_content() or get_the_excerpt() outside of a single template, in the functions.php for example, I get the following Warning:

Warning: count(): Parameter must be an array or an object that implements Countable in /Applications/MAMP/htdocs/wordpress/wp-kona/wp-includes/post-template.php on line 284

What's wrong with it? Is it a wordpress core bug? Am I missing something.

  • What is your full use of the function? Mar 21 '18 at 11:45
  • 1
    Are you calling these functions outside the loop?
    – kero
    Mar 21 '18 at 11:47
  • I think this is unimportant. I'm just using it to check if the content has some specefic shortcode for example. Yes, it's outside the loop. But how can I access to the content (with the id) then outside the loop.
    – Benmay
    Mar 21 '18 at 11:49
  • 1
    Can you show the specific usage that is causing the issue. To rule out any syntax issues or incorrect usage of the function. Mar 21 '18 at 11:50
  • This is a bug in core WP. See core.trac.wordpress.org/ticket/42814 Mar 21 '18 at 15:23

Yes, I already knew the cause of what you're experiencing:

The $page and $pages global variable have not been set up and a call to get_the_content or get_the_excerpt would return that error: count(pages)

That said, pages global variable is set to null, calling count(pages) triggers that error because a null value cannot be counted.

To fix this, one can easily tell that you're calling get_the_content or get_the_excerpt outside the loop.

So you have to set up the post data correctly before usage, that's what the loop is meant to do:

while ( have_post(): the_post() ) :
    // get_the_content() or get_the_excerpt() will work fine now
  • This answer does not address the OP's PHP error related to the count() function. Jan 3 '19 at 23:27
  • I've already stated why count() trigger that error. The http://php.net/manual/en/function.count.php count() function counts all elements in an array or something in an object. Anything else will throw an error. Jan 4 '19 at 6:25
  • @benmay to explore this error, set the $page and $pages global var before calling get_the_content() or get_the_excerpt() and see what happens next: global $post; $page, $pages; $pages = 1; $page = 1; Now call get_the_content() and see for yourself. Jan 4 '19 at 18:11
  • Thanks for helping me see that answer John! I deleted my answer because it was based on a misunderstanding of the issue and didn't solve the problem. Jan 5 '19 at 15:03
  • @JustinWaulters you are welcome. Glad I could be of little help Jan 10 '19 at 2:07

As of v5.2, this will no longer be an issue. get_the_content() and wp_trim_excerpt() are getting an optional new parameter (third and second respectively) of $post so you can use it outside the loop, bringing it in line with the functionality of all the other get_the_* functions. It only took 16 years... Reference on Trac


This bug related to WordPress core bug https://core.trac.wordpress.org/ticket/42814

Most often it occurs when you call the function get_the_excerpt or get_the_content outside the Loop provided that excerpt or content is empty.

So to fix it yourself you should find error place and check excerpt/content existing manually.

For example:

if(has_excerpt($post_id)) {
    $meta[] = sprintf('<meta name="description" content="%s">',
  • Please see this comment on the page you linked to: core.trac.wordpress.org/ticket/42814#comment:16 - seems like this exact issue. They still can not reproduce it as an issue with WP core. I my memory serves me right count() used to show a notice if you gave it a string, now it seems to be a warning. Jan 3 '19 at 23:44

If someone bumps in to this issue, the solution is to get the_content from the meta field and run it trough the_content filter like this:

apply_filters('the_content', get_post_field('post_content', $post->id));

I made a short video explayining the whole thing if you wait untill the TLDR section... https://www.youtube.com/watch?v=vMguTNzFoUk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.