Wordpress Get the Excerpt Outside the Loop

How to get a WordPress post excerpt when not in the loop or you don't have the post variable in scope. This will get the excerpt anywhere.

By Tim Trott | WordPress | January 24, 2009

The WordPress template tag the_excerpt and get_the_excerpt are great for showing a short summary of a post. Unfortunately, you cannot use get_the_excerpt outside "the loop". This is a similar function that can be used anywhere in your theme or template.

This snippet can even be used to display the excerpt for any post anywhere on any page. Just pass in the post ID of the post to show the excerpt for.

php
function get_the_excerpt_here($post_id)
{
  global $wpdb;
  $query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
  $result = $wpdb->get_results($query, ARRAY_A);
  return $result[0]['post_excerpt'];
}
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 11 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. SH

    On Thursday 16th of January 2014, Shahin said

    it does not work in 3.8
    any help plz....

  2. ED

    On Sunday 30th of September 2012, Eli dan said

    can u plz explain where do i have to put this code?

    1. Tim Trott

      On Thursday 29th of November 2012, Tim Trott  Post Author replied

      This code would be used in a template anywhere outside of "the loop", for example the header, footer or sidebars.

  3. MR

    On Saturday 17th of March 2012, Michael Robinson said

    Thanks for posting this. Seems obvious really, one would assume that for something so obviously useful, WP would provide a function for it!

  4. RO

    On Wednesday 30th of November 2011, Rob said

    Brilliant, thank you!

  5. LA

    On Wednesday 17th of August 2011, Laurize said

    How do I pass the post ID? thanks

  6. DA

    On Saturday 6th of August 2011, david said

    Perfect! Just what I was looking for

    Cheers
    D

  7. GL

    On Tuesday 5th of July 2011, Gwyneth Llewelyn said

    Hmm. What if I need an automatically generated excerpt? It won't be stored on the database.

  8. ES

    On Wednesday 11th of August 2010, estevan said

    Is there any way to have this pull an excerpt of the most recent post instead of specifying an ID?

    1. VA

      On Wednesday 2nd of March 2011, vaseem replied

      @estevan, Hello dear, you can easily get the latest post excerpt using the following query

      SELECT post_excerpt FROM $wpdb->posts where `post_status`='publish' && post_excerpt!='' && post_password='' && (post_type!='post' OR post_type!='page') ORDER BY `ID` DESC LIMIT 1

      Have fun and Bye

  9. PD

    On Sunday 18th of October 2009, Phil Dodd said

    Thanks so much for this, I've been searching for a solution to this for some time.