The Observation Deck

WordPress: Get the Excerpt Automatically Using the Post ID Outside of the Loop

In WordPress the_excerpt() needs to be run inside of the loop. If you try to use the_excerpt outside of the loop you get a big fat nada.

In addition we find it to be the easiest for end users to automatically generate excerpts based on post content, instead of requiring them to use the excerpt field.

So, here’s a simple function we wrote to get the excerpt outside of the loop.

Step 1: Add this fucntion to your functions.php file

function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_push($words, '…');
$the_excerpt = implode(' ', $words);
$the_excerpt = '<p>' . $the_excerpt . '</p>';
return $the_excerpt;

Step 2: Call the function in your theme file

$my_excerpt = get_excerpt_by_id($post_id); //$post_id is the post id of the desired post

17 Responses to WordPress: Get the Excerpt Automatically Using the Post ID Outside of the Loop

  1. vedete August 24, 2012 at 9:32 pm #

    Very informative, helpful article, Thank you so much for the research and effort that went into preparing this.

  2. Zac January 24, 2013 at 10:59 pm #

    You rule! Thank you so much for posting this!!!! Nice work!!!!

  3. Adamo February 18, 2013 at 4:53 pm #

    This works fine except for one thing, it leaves a lot of white lines (those removed for the images and the other content) if you check the source code, how could that be fixed? Thanks!

  4. Zaf April 11, 2013 at 9:25 am #

    perfect, this help me, thanx 🙂

  5. Arun May 17, 2013 at 10:38 am #

    Thanks alot. I wasted 1 hr searching for wordpress inbuilt function finally reached your site and found this. It worked. Cheers.!!!

    • Nizam Kazi July 15, 2013 at 8:53 am #

      Yah! we are sharing a same story 🙂

    • Jeremias July 18, 2013 at 5:35 pm #

      Same here!! this was of great help for me, very easy to use and quick, I just one doubt, is there any way to pass a value to trim the length of the excerpt, something similar as how you pass the ID of the post


      • Withers Davis July 18, 2013 at 9:12 pm #

        So in line four of the code you will see this

        $excerpt_length = 35; //Sets excerpt length by word count

        That sets the length of the excerpt. If you wanted to set the excerpt length on a per-post basis you could theoretically pass a value from the post to the function. You’d need a custom field in the post for the excerpt length. Hope this helps.

  6. Nizam Kazi July 15, 2013 at 8:52 am #

    Small, Simple, Easy to understand and right to the point. No more no less…

    Your tutorial helped me to get out of a big issue, thanks a lot..!

  7. Andrew Condon July 21, 2013 at 9:54 am #

    Thanks a lot!

  8. Brian Gaeddert December 18, 2013 at 7:48 pm #

    Thanks works great!


    • withers December 18, 2013 at 9:14 pm #

      Thanks Brian.

  9. Parul December 19, 2013 at 8:36 am #

    thanx . helpful code

  10. Yazmin January 18, 2014 at 3:14 pm #

    Thanks! So much nicer to find a starting code base than to write snippets like these from scratch.

  11. Dario May 6, 2014 at 4:56 am #

    Hi is it normal that i can’t see the “See more” link? with the normal Excerpt i can. Thank you!

  12. Jesus July 9, 2014 at 9:40 pm #


  13. Vinoth Eswaramoorthy October 17, 2014 at 4:52 am #

    It works great dude!!!

Leave a Reply

Back to top