I often wonder why there isn’t a function in WordPress core that will get a post excerpt outside of the loop. Here is a function that will do just that:

You call the function by simply passing in the ID of the post whose excerpt you want to retrieve, like this:
$excerpt = get_post_excerpt( 21 );

To override the default excerpt length of 55 words, you can pass a second parameter:
$excerpt = get_post_excerpt( 21, 85 );

To change the excerpt more string from the default ellipsis (…), you can pass a third parameter:
$excerpt = get_post_excerpt( 21, 85, '----' );

By default the excerpt will also contain a link to the singular post. This can be disabled by passing a fourth parameter:
$excerpt = get_post_excerpt( 21, 85, '----', false );

Additionally there are three useful filters that can be used like this:
add_filter( 'get_post_excerpt_excerpt', 'callback_function_to_modify_the_post_excerpt' );

add_filter( 'get_post_excerpt_read_more_text', 'callback_function_to_modify_the_read_more_text' );

add_filter( 'get_post_excerpt_read_more_link', 'callback_function_to_modify_the_read_more_link' );

Reader Interactions


  1. Thank you so much for this! I spent about 6 hours yesterday trying to pull an excerpt using a post ID outside the loop, nothing would work! This did. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *