32

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:

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

3
  • ok, got it using my_excerpt($post->post_content, get_the_excerpt()) and using the my_excerpt() function from wordpress.stackexchange.com/questions/6961/…
    – ariel
    Aug 24, 2011 at 0:44
  • 3
    Please add solution you came up with as an answer, so this doesn't haunt site as unanswered question. :)
    – Rarst
    Sep 11, 2011 at 21:56
  • Just use the_post() (it works on single post template too) function before you call get_the_excerpt() it will setup necessary data for you.
    – Sisir
    Sep 18, 2014 at 16:01

9 Answers 9

32

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));

6
  • 1
    This should be accepted as answer as it's the recommended way to pull data outside the loop. Also doesn't require any custom function or overriding of the $post global variable.
    – MacK
    Jun 16, 2015 at 16:43
  • 4
    it return empty string.
    – Kyaw Tun
    Jan 20, 2016 at 7:45
  • 1
    @KyawTun - it works, so long as $post_id is set (what is the value of $post_id? AND $post_id is a valid, legitimate post ID. Jan 20, 2016 at 15:03
  • 2
    @cale_b Thanks. I use get_posts query and get ID from the resulting array. The post object does have post_title, post_content, ID, etc. But not working.
    – Kyaw Tun
    Jan 21, 2016 at 12:30
  • If you need JUST the TEXT and not <p> tag which is included with the_excerpt filter, then use "get_the_excerpt" filter, so that above filter becomes: $text = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post_id)); this will give you just the RAW text you can insert anywhere in your own markup. May 20, 2016 at 18:42
22

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.

2
  • 1
    This works but: "You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly." global $post;$post = $post_object;setup_postdata( $post );$excerpt = get_the_excerpt();
    – deach
    Jan 19, 2017 at 23:35
  • setup_postdata($post); FTW!!!! Apr 30, 2017 at 17:48
18

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.

2
  • 1
    Excellent find my friend. I've never understood why WordPress would have deprecated such a critical function. This is effectively rebuilding it from scratch, but it works. Given how often we use the excerpt outside the loop with features like social sharing plugins, it probably should have remained a part of the core. May 15, 2014 at 2:56
  • 1
    EAMann's answer is a much better approach to this problem, and the should be considered the best practice. This approach is basically duplicating Core's internals instead of using the API.
    – Ian Dunn
    Mar 24, 2015 at 1:18
9

Now you can simply use the get_the_excerpt( $postID ) function. Since: WordPress 4.5.0 introduced the $post parameter.

3
  • 1
    This should be new accepted answer since we are in WP 4.5 + era. Jun 13, 2016 at 12:18
  • 19
    This won't work if the excerpt is empty as the wp_trim_excerpt filter will return the excerpt for the current post.
    – Dylan
    Aug 16, 2016 at 22:24
  • 9
    See core.trac.wordpress.org/ticket/36934 for details on what @Dylan said
    – kraftner
    Sep 14, 2016 at 7:18
5

got it using my_excerpt($post->post_content, get_the_excerpt()) and using the my_excerpt() function from Using wp_trim_excerpt to get the_excerpt() outside the loop

2
  • 2
    Link-only answers are no good. Copy the relevant code here. When that link is broken, that site is down / gone, then this answer has no value. Jun 18, 2014 at 15:23
  • It worked perfectly for me!
    – Saikat
    Jul 24, 2017 at 12:19
1

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;
}
2
  • But the asker has a post object as stated in the question.
    – fuxia
    Nov 25, 2012 at 19:51
  • 3
    Correct me if I'm wrong, this method will return the manual excerpt but won't generate one if needed
    – Bill
    Nov 7, 2014 at 12:46
1

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;
}
1
  • This is the most direct way to do it.. Not sure it's great performance-wise though. You still get my +1
    – Bill
    Nov 7, 2014 at 12:47
1

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);
-1
$trimexcerpt = get_the_content();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 18, $more = '… ' ); 
echo $shortexcerpt;
1
  • Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Mar 14, 2018 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.