Get excerpt using get_the_excerpt outside a loop

I have a code that call get_the_title() and it works, but get_the_excerpt() return empty. How can i make it work?

This code is inside a plugin called “WP Facebook Open Graph protocol”. Here’s the part i want to change:

Read More
if (is_singular('post')) {
  if (has_excerpt($post->ID)) {
    echo "t<meta property='og:description' content='".esc_attr(strip_tags(get_the_excerpt($post->ID)))."' />n";
  }else{
    echo "t<meta property='og:description' content='". [?] ."' />n";
  }
}else{
  echo "t<meta property='og:description' content='".get_bloginfo('description')."' />n";
}

Here, has_excerpt always fail, and get_the_excerpt($post->ID) don’t work anymore (deprecated).

So, how can i display the excerpt there?

ps: I’m using “Advanced Excerpt” plugin as well

Related posts

Leave a Reply

9 comments

  1. I found this question when looking how to do this without the post object.

    My additional research turned up this slick technique:

    $text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

  2. Since it seems you already have the post object you need the excerpt for, you can just force things to work:

    setup_postdata( $post );
    $excerpt = get_the_excerpt();
    

    The setup_postdata() function will globalize the $post object and make it available for regular old loop function. When you’re inside the loop, you call the_post() and it sets things up for you … outside of the loop you need to force it manually.

  3. Try this:

    Create a new function in functions.php and then call it from wherever.

    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_pop($words);
            array_push($words, '…');
            $the_excerpt = implode(' ', $words);
        endif;
    
        $the_excerpt = '<p>' . $the_excerpt . '</p>';
    
        return $the_excerpt;
    }
    

    Here’s a post describing the code.

  4. In case you don’t have the post object, here’s a short function like the one from Withers.

    function get_excerpt_by_id($post_id){
        $the_post = get_post($post_id);
        $the_excerpt = $the_post->post_excerpt; 
        return $the_excerpt;
    }
    
  5. This is for when you want to use get_the_excerpt() outside the loop:

    function custom_get_excerpt($post_id) {
        $temp = $post;
        $post = get_post($post_id);
        setup_postdata($post);
    
        $excerpt = get_the_excerpt();
    
        wp_reset_postdata();
        $post = $temp;
    
        return $excerpt;
    }
    
  6. If you’d like to generate the excerpt automatically from the content in one line – you can use wp_trim_words function like this:

    // 30 is the number of words ehere
    $excerpt = wp_trim_words(get_post_field('post_content', $post_id), 30);