Title: get_the_excerpt
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_the_excerpt( int|WP_Post $post = null ): string

## In this article

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

[ Back to top](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#wp--skip-link--target)

Retrieves the post excerpt.

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

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

Post ID or [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
object. Default is global $post.

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#return)󠁿

 string Post excerpt.

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

    ```php
    function get_the_excerpt( $post = null ) {
    	if ( is_bool( $post ) ) {
    		_deprecated_argument( __FUNCTION__, '2.3.0' );
    	}

    	$post = get_post( $post );
    	if ( empty( $post ) ) {
    		return '';
    	}

    	if ( post_password_required( $post ) ) {
    		return __( 'There is no excerpt because this is a protected post.' );
    	}

    	/**
    	 * Filters the retrieved post excerpt.
    	 *
    	 * @since 1.2.0
    	 * @since 4.5.0 Introduced the `$post` parameter.
    	 *
    	 * @param string  $post_excerpt The post excerpt.
    	 * @param WP_Post $post         Post object.
    	 */
    	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/6.9.4/src/wp-includes/post-template.php#L413)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-template.php#L413-L437)

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

 [apply_filters( ‘get_the_excerpt’, string $post_excerpt, WP_Post $post )](https://developer.wordpress.org/reference/hooks/get_the_excerpt/)

Filters the retrieved post excerpt.

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

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

Determines whether the post requires password and whether a correct password has been provided.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

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

Marks a function argument as deprecated and inform when it has been used.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

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

Retrieves post data given a post ID or post object.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#)

| Used by | Description | 
| [the_excerpt_embed()](https://developer.wordpress.org/reference/functions/the_excerpt_embed/)`wp-includes/embed.php` |

Displays the post excerpt for the embed template.

  | 
| [WP_Posts_List_Table::column_title()](https://developer.wordpress.org/reference/classes/wp_posts_list_table/column_title/)`wp-admin/includes/class-wp-posts-list-table.php` |

Handles the title column output.

  | 
| [the_excerpt_rss()](https://developer.wordpress.org/reference/functions/the_excerpt_rss/)`wp-includes/feed.php` |

Displays the post excerpt for the feed.

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

Displays the post excerpt.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/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. | 
| [0.71](https://developer.wordpress.org/reference/since/0.71/) | Introduced. |

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

 1.   [Skip to note 8 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-2457)
 2.    [Mikko Saari](https://profiles.wordpress.org/msaari/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-2457)
 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%2Ffunctions%2Fget_the_excerpt%2F%23comment-2457)
     Vote results for this note: 17[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%2Ffunctions%2Fget_the_excerpt%2F%23comment-2457)
 4.  If this function is used outside The Loop and the post doesn’t have a custom excerpt,
     this function will use [wp_trim_excerpt()](https://developer.wordpress.org/reference/functions/wp_trim_excerpt/)
     to generate an excerpt. That function uses [get_the_content()](https://developer.wordpress.org/reference/functions/get_the_content/),
     which must be used with The Loop and will cause problems if [get_the_excerpt()](https://developer.wordpress.org/reference/functions/get_the_excerpt/)
     is being used outside The Loop. In order to avoid the issues, use [setup_postdata()](https://developer.wordpress.org/reference/functions/setup_postdata/)
     prior to calling [get_the_excerpt()](https://developer.wordpress.org/reference/functions/get_the_excerpt/)
     to set up the global $post object.
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D2457%23feedback-editor-2457)
 6.   [Skip to note 9 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-1969)
 7.    [Barrett Golding](https://profiles.wordpress.org/hearvox/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-1969)
 8.  [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%2Ffunctions%2Fget_the_excerpt%2F%23comment-1969)
     Vote results for this note: 10[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%2Ffunctions%2Fget_the_excerpt%2F%23comment-1969)
 9.  **Use excerpt for HTML meta description**
      [html] <!– Use Post excerpt for meta
     description. –> <?php if ( [is_single()](https://developer.wordpress.org/reference/functions/is_single/)){?
     > <meta name="description" content="<?php echo wp_strip_all_tags( [get_the_excerpt()](https://developer.wordpress.org/reference/functions/get_the_excerpt/),
     true ); ?>" /> <?php } ?> [/html]
 10. (Setting the second parameter of [wp_strip_all_tags](https://developer.wordpress.org/reference/functions/wp_strip_all_tags/)
     to true removes left over line breaks and whitespace chars.)
 11.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D1969%23feedback-editor-1969)
 12.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-3320)
 13.   [Jonm511](https://profiles.wordpress.org/jonm511/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-3320)
 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%2Ffunctions%2Fget_the_excerpt%2F%23comment-3320)
     Vote results for this note: 4[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%2Ffunctions%2Fget_the_excerpt%2F%23comment-3320)
 15. **Limit Manual Excerpt displayed on Homepage to 260 characters but truncate after
     the last word**
 16. This code snippet must be placed in the theme where `the_excerpt()` is located.
     This is for modifing manually entered excerpts NOT automatic ones WordPress will
     grab from the content.
 17. It will display the first 260 characters of a manually entered excerpt but instead
     of ending on the 260th character it will truncate after the closest word. To change
     the number of characters simply change the value ‘260’ to another number. Remember
     to remove `the_excerpt()` in your theme and replace it with this:
 18.     ```php
         <?php
         $excerpt = get_the_excerpt();
     
         $excerpt = substr($excerpt, 0, 260);
         $result = substr($excerpt, 0, strrpos($excerpt, ' '));
         echo $result;
         ?> 
         ```
     
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D3320%23feedback-editor-3320)
 20.  [Skip to note 11 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-2095)
 21.   [prod3v3loper](https://profiles.wordpress.org/prodeveloper/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-2095)
 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%2Ffunctions%2Fget_the_excerpt%2F%23comment-2095)
     Vote results for this note: 2[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%2Ffunctions%2Fget_the_excerpt%2F%23comment-2095)
 23. Use
 24.     ```php
         has_excerpt()
         ```
     
 25.  to prevent a Notice: Undefined offset: -1 in post-template.php
 26.     ```php
         $excerpt = '';
         if (has_excerpt()) {
             $excerpt = wp_strip_all_tags(get_the_excerpt());
         }
         ```
     
 27. Otherwise, you could get an Undefined offset -1 warning, do not try to get_excerpt
     directly to check whether with isset or NULL, you’ll get an undefined offset -
     1 back
 28.  * Is this still true by the end of 2023? It’s just that `has_excerpt()` relates
        to the _user’s_ manually inserted excerpt (the so-called _teaser_), while `
        get_the_excerpt()` allegedly either returns the user’s teaser excerpt if it
        exists, _or_ automatically generates an automated excerpt (with tags stripped).
        At least, that’s how it looks like when viewing the source code as of writing
        this.
      * [Gwyneth Llewelyn](https://profiles.wordpress.org/gwynethllewelyn/) [2 years ago](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-6832)
 29.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D2095%23feedback-editor-2095)
 30.  [Skip to note 12 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-5705)
 31.   [Mohamed](https://profiles.wordpress.org/atta98/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-5705)
 32. [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%2Ffunctions%2Fget_the_excerpt%2F%23comment-5705)
     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%2Ffunctions%2Fget_the_excerpt%2F%23comment-5705)
 33. **Expandable excerpt function**
 34. **first the PHP**
      Add this function in function.php
 35.     ```php
         function expandable_excerpt($excerpt)
         {
         	$split = explode(" ", $excerpt); //convert string to array
         	$len = count($split); //get number of words
         	$words_to_show_first = 19; //Word to be dsiplayed first
         	if ($len > $words_to_show_first) { //check if it's longer the than first part
     
         		$firsthalf = array_slice($split, 0, $words_to_show_first);
         		$secondhalf = array_slice($split, $words_to_show_first, $len - 1);
         		$output = '<p class="event-excerpt" >';
         		$output .= implode(' ', $firsthalf) . '<span class="see-more-text">...see more</span>';
     
         		$output .= '<span class="excerpt-full hide">';
         		$output .= ' ' . implode(' ', $secondhalf);
         		$output .= '</span>';
         		$output .= '</p>';
         	} else {
         		$output = '<p class="event-excerpt">'  .   $excerpt . '</p>';
         	}
         	return $output;
         }
         ```
     
 36. **Required CSS**
      CSS to simply hide elements when needed `.excerpt-full.hide {
     display: none; } .see-more-text.hide { display: none; }
 37. **required JS**
      script to add/remove css classes when needed
 38.     ```javascript
         const itemSeeMore = document.querySelectorAll(
           "p.event-excerpt> span.see-more-text"
         );
         if (itemSeeMore) {
           itemSeeMore.forEach((item) => {
             item.addEventListener("click", () => {
               item.classList.toggle("hide");
               item.nextElementSibling.classList.toggle("hide");
             });
           });
         }
         ```
     
 39.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D5705%23feedback-editor-5705)
 40.  [Skip to note 13 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-6965)
 41.   [shanemac10](https://profiles.wordpress.org/shanemac10/)  [  2 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-6965)
 42. [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%2Ffunctions%2Fget_the_excerpt%2F%23comment-6965)
     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%2Ffunctions%2Fget_the_excerpt%2F%23comment-6965)
 43. **Fixing when `get_the_excerpt()` and `the_excerpt()` are showing Editor content,
     when the Editor has been turned off on the `post_type`**
 44. I ran into an issue with some legacy content in a custom post type, that originally
     had the content `'editor'` in the `register_post_type()` arguments `'supports'
     => (..., 'editor')`, but had since been removed in favor of using some custom 
     fields to better standardize the content structure in the templates. This had 
     the result of displaying vestigial content from the editor in the templates where`
     get_the_excerpt()` or `the_excerpt()` were called. The following is a filter to
     catch the `$post_excerpt` string before it gets returned, and to both check if`'
     editor'` is turned off on the `post_type`, and then try to find a suitable alternative
     field in `post_meta`. It borrows code from `wp_trim_excerpt()` to both trim length
     and then add the `[...]` as would be expected for the `get_the_excerpt()` and `
     the_excerpt()` results.
 45.     ```php
         /**
          * WordPress get_the_excerpt() and the_excerpt() do not check if the 'editor' is turned off on the post_type or not,
          * so vestigial content can be displayed. This checks if the 'editor' is turned on for the $post->post_type, and tries 
          * to find a suitable alternative field if applicable.
          */
         function wpdocs_custom_excerpt( $post_excerpt, $post ) {
         	$supports_editor_content = post_type_supports( $post->post_type, 'editor' );
     
         	if ( ! $supports_editor_content ) {
         		$post_meta = get_post_meta( $post->ID );
     
         		// Try to find alternative field to pull an excerpt replacement from...
         		$post_excerpt = $post_meta['overview'][0] ?? $post_meta['description'][0] ?? ''; // <-- Example: Customize this code here for your theme
     
         		if ( '' !== $post_excerpt ) {
         			// Trimming to excerpt_length via code lifted from wp_trim_excerpt...
         			// Ref: https://developer.wordpress.org/reference/functions/wp_trim_excerpt/
         			$excerpt_length = (int) _x( '55', 'excerpt_length' );
         			$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
         			$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
     
         			$post_excerpt = wp_trim_words( $post_excerpt, $excerpt_length, $excerpt_more );
         		}
     
         	}
     
         	return is_string( $post_excerpt ) ? $post_excerpt : '';
         }
         add_filter( 'get_the_excerpt', 'wpdocs_custom_excerpt', 10, 2 );
         ```
     
 46.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D6965%23feedback-editor-6965)
 47.  [Skip to note 14 content](https://developer.wordpress.org/reference/functions/get_the_excerpt/?output_format=md#comment-content-423)
 48.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/get_the_excerpt/#comment-423)
 49. [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%2Ffunctions%2Fget_the_excerpt%2F%23comment-423)
     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%2Ffunctions%2Fget_the_excerpt%2F%23comment-423)
 50. **Example**
      `get_the_excerpt()` can be used to retrieve and store the value in
     a variable, without outputting it to the page.
 51.     ```php
         <?php
         $my_excerpt = get_the_excerpt();
         if ( '' != $my_excerpt ) {
         	// Some string manipulation performed
         }
         echo $my_excerpt; // Outputs the processed value to the page
         ?>
         ```
     
 52.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_excerpt%2F%3Freplytocom%3D423%23feedback-editor-423)

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