get_the_excerpt() not returning anything when post has no excerpt

When trying to call get_the_excerpt() the page does not return anything on posts that have no excerpt.

I thought WordPress usually ‘fake’ it and create one using the first x characters from the_content().

Read More

Or have things changed?

Related posts

Leave a Reply

2 comments

  1. Double check that you don’t have a check for has_excerpt() that’s hiding the “auto-generated” excerpt. Even if get_the_excerpt() returns something made from post_content, has_excerpt() still returns false if the excerpt is empty.

    If that’s not the case, see if there’s a function that filters on get_the_excerpt that could be effecting this.

    To answer your question, wp_trim_excerpt(), the function that “fakes” an excerpt, filters get_the_content() which the_excerpt() is just a wrapper function for. So that’s not the issue.

  2. In my case, I had,

    function mytheme_excerpt_length() {
      return POST_EXCERPT_LENGTH;
    }
    add_filter('excerpt_length','mytheme_excerpt_length');
    

    and POST_EXCERPT_LENGTH was defined somewhere, which got deleted by mistake. So, excerpt was returning 0 characters.

    So, just return a value greater than 0 and it should fix the issue.