Title: get_the_excerpt
Published: April 25, 2014
Last modified: August 20, 2026

---

# apply_filters( ‘get_the_excerpt’, string $post_excerpt, WP_Post $post )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#user-contributed-notes)

Filters the retrieved post excerpt.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#parameters)󠁿

 `$post_excerpt`string

The post excerpt.

`$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)

Post object.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#more-information)󠁿

The `get_the_excerpt` filter is used to filter the excerpt of the post after it 
is retrieved from the database and before it is returned from the [get_the_excerpt()](https://developer.wordpress.org/reference/functions/get_the_excerpt/)
function.

When the `get_the_excerpt` filter is called, the filter function is passed a single
argument containing the post excerpt.

    ```php
    function filter_function_name( $excerpt ) {
    # ...
    }
    add_filter( 'get_the_excerpt', 'filter_function_name' );
    ```

Where ‘`filter_function_name`‘ is the function WordPress should call when the excerpt
is being retrieved. Note that the filter function **must** return the excerpt after
it is finished processing, or page sections showing an excerpt will be blank, and
other plugins also filtering the excerpt may generate errors.

The ‘`filter_function_name`‘ should be a unique function name. It cannot match any
other function name already declared.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#source)󠁿

    ```php
    return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post-template.php#L436)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post-template.php#L436-L436)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_REST_Attachments_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_attachments_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php` |

Prepares a single attachment output for response.

  | 
| [WP_REST_Posts_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Prepares a single post output for response.

  | 
| [get_the_excerpt()](https://developer.wordpress.org/reference/functions/get_the_excerpt/)`wp-includes/post-template.php` |

Retrieves the post excerpt.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.5.0](https://developer.wordpress.org/reference/since/4.5.0/) | Introduced the `$post` parameter. | 
| [1.2.0](https://developer.wordpress.org/reference/since/1.2.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#comment-content-2201)
 2.    [IAmAdamTaylor](https://profiles.wordpress.org/ad_taylor/)  [  10 years ago  ](https://developer.wordpress.org/reference/hooks/get_the_excerpt/#comment-2201)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-2201)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-2201)
 4.  When using this filter on a string stored in a variable, the string **will not**
     be trimmed by the [`excerpt_length`](https://developer.wordpress.org/reference/hooks/excerpt_length/)
     filter.
 5.  This is expected behaviour. By passing a long string through the `get_the_excerpt`
     filter, you are simulating the case where you have entered the same long text 
     into the `Excerpt` field in the `Edit Post` screen. When a custom excerpt is present
     on the post, no trimming is done (though other content filters are applied).
 6.  If you want to simulate the automatic excerpt trimming on arbitrary text, you 
     can pass the text to [`wp_trim_words()`](https://developer.wordpress.org/reference/functions/wp_trim_words/)
     yourself, with a function like this:
 7.      ```php
         function wpcodex_format_custom_excerpt( $text ) {
           /**
            * Filters the number of words in an excerpt.
            *
            * @since 2.7.0
            *
            * @param int $number The number of words. Default 55.
            */
           $excerpt_length = apply_filters( 'excerpt_length', 55 );
           /**
            * Filters the string in the "more" link displayed after a trimmed excerpt.
            *
            * @since 2.9.0
            *
            * @param string $more_string The string shown within the more link.
            */
           $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
           $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
     
           // Format the text
           $text = apply_filters( 'get_the_excerpt', $text );
     
           return $text;
         }
         ```
     
 8.  The function above can be used like this:
 9.      ```php
         $text = "Etiam laoreet libero sit amet sem tempor, vel dictum odio bibendum. Aenean odio ligula, placerat sodales dui non, tempor dictum lorem. Vestibulum rutrum, velit a placerat imperdiet, erat massa porta urna, a convallis diam lorem non sapien. Vivamus in risus non quam aliquet blandit nec fermentum dolor. Duis ultricies lectus eu cursus fermentum. Sed eget convallis odio. Ut ut dolor nec nisi varius blandit a eget justo. Integer sed tellus eget leo pretium ultricies. Nullam rhoncus ex sit amet dolor pellentesque feugiat. Nullam a eros orci. Etiam egestas est erat, eu pellentesque sapien dignissim vel. Nulla malesuada commodo justo, at egestas purus egestas fermentum.
     
         Vestibulum vitae metus ullamcorper, vehicula urna eu, ullamcorper leo. Sed sit amet eros eget metus bibendum blandit. Praesent ac lacinia purus. Donec laoreet tempus dui id faucibus. Nulla laoreet cursus laoreet. Nulla et arcu ex. Pellentesque cursus non metus a volutpat. Donec pretium orci et metus molestie, ac feugiat lacus auctor. Ut vehicula eu augue eu gravida. In scelerisque risus in rutrum vestibulum. Phasellus egestas augue quis enim varius, ut sagittis massa auctor.";
         $text = wpcodex_format_custom_excerpt( $text );
         ```
     
 10.  * Note: When using this _workaround_, `[wp_trim_words()](https://developer.wordpress.org/reference/functions/wp_trim_words/)`
        will remove all HTML tags from the excerpt by applying `[wp_strip_all_tags()](https://developer.wordpress.org/reference/functions/wp_strip_all_tags/)`.
      * [frzsombor](https://profiles.wordpress.org/frzsombor/) [2 years ago](https://developer.wordpress.org/reference/hooks/get_the_excerpt/#comment-7312)
 11.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%3Freplytocom%3D2201%23feedback-editor-2201)
 12.  [Skip to note 5 content](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#comment-content-4561)
 13.   [Steven Lin](https://profiles.wordpress.org/stevenlinx/)  [  6 years ago  ](https://developer.wordpress.org/reference/hooks/get_the_excerpt/#comment-4561)
 14. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-4561)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-4561)
 15. Examples migrated from Codex:
 16. **Custom “More” Link**
 17. This example from the twentyeleven theme appends a custom “Read more” link to 
     post excerpts. See [has_excerpt()](https://developer.wordpress.org/reference/functions/has_excerpt/)
     and [is_attachment()](https://developer.wordpress.org/reference/functions/is_attachment/).
 18.     ```php
         /**
          * Adds a pretty "Continue Reading" link to custom post excerpts.
          *
          * To override this link in a child theme, remove the filter and add your own
          * function tied to the get_the_excerpt filter hook.
          */
         function twentyeleven_custom_excerpt_more( $output ) {
           if ( has_excerpt() && ! is_attachment() ) {
             $output .= twentyeleven_continue_reading_link();
           }
           return $output;
         }
         add_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
         ```
     
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%3Freplytocom%3D4561%23feedback-editor-4561)
 20.  [Skip to note 6 content](https://developer.wordpress.org/reference/hooks/get_the_excerpt/?output_format=md#comment-content-6086)
 21.   [Lukasz](https://profiles.wordpress.org/wpfed/)  [  4 years ago  ](https://developer.wordpress.org/reference/hooks/get_the_excerpt/#comment-6086)
 22. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-6086)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%23comment-6086)
 23. **Fallback excerpt for posts with AUTOMATED excerpt:**
 24.     ```php
         /**
         * Filters the retrieved post excerpt.
         *
         * @param string  $post_excerpt The post excerpt.
         * @param WP_Post $post         Post object.
         */
         function wpdocs_excerpt_fallback( $post_excerpt ) {
         	if ( ! $post_excerpt ) {
         		return '(no excerpt present, click "read more" to view post)';
         	}
         	return $post_excerpt;
         }
         add_filter( 'get_the_excerpt', 'wpdocs_excerpt_fallback' );
         ```
     
 25.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F%3Freplytocom%3D6086%23feedback-editor-6086)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fget_the_excerpt%2F)
before being able to contribute a note or feedback.