183

How can I get WordPress post content by post id?

0

5 Answers 5

216

Simple as it gets

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
5
  • 102
    Shorthand for specific field: $content = get_post_field('post_content', $my_postid);
    – Rarst
    Commented Feb 17, 2011 at 21:39
  • 5
    @Bainternet I'm just curious here... what is the part $content = str_replace(']]>', ']]>', $content); do? what's the purpose of it there? Commented Nov 4, 2013 at 11:02
  • 2
    @AverageJoe its basic search and replace. When using the_content() the content is filtered. Since in the above example the content was directly retrieved, the author has used the search and replace to make it safe. Commented Mar 18, 2014 at 9:21
  • 5
    maybe you also need do_shortcode() like $content = do_shortcode(get_post_field('post_content', $my_postid));
    – cyptus
    Commented Mar 9, 2018 at 13:47
  • Is there anyway to preserve the "more_link"? Commented Jul 5, 2018 at 1:46
157
echo get_post_field('post_content', $post_id);
4
  • 73
    better to do it like echo apply_filters('the_content', get_post_field('post_content', $post_id));. For example when using qTranslate, your solution would not be enough.
    – Karel Attl
    Commented Jan 17, 2013 at 7:10
  • 6
    This is the best answer if the scope is to get the post content as it is in the WordPress edit page.
    – mcont
    Commented Aug 8, 2014 at 13:21
  • 1
    Without the code from @KarelAttl line breaks where missing. With the apply_filters code it worked perfectly. Commented Sep 23, 2015 at 12:23
  • 2
    apply_filters is a good option, but wasn't right for my current purpose. It's good to have both options.
    – KnightHawk
    Commented Nov 5, 2015 at 18:12
49

Another way to get a WordPress post content by post id is:

$content = apply_filters('the_content', get_post_field('post_content', $my_postid));

To complete this answer I have also added method 01 and method 02 to this answer.

Method 01 (credit goes to bainternet):

$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);

Method 02 (credit goes to realmag777):

$content = get_post_field('post_content', $my_postid);

Method 03:

$content = apply_filters('the_content', get_post_field('post_content', $my_postid));

Read the What is the best / efficient way to get WordPress content by post id and why? question to get an idea about which one you should use from the above three.

6

Starting from wordpress 5.2.0,

We can now use post object or post id get_the_content function.

We can also pass $more_link_text and $strip_teaser

get_the_content( string $more_link_text = null, bool $strip_teaser = false, WP_Post|object|int $post = null )

We can use it along with apply_filters like below to get all filters of the_content

apply_filters( 'the_content', get_the_content(null, false, $post_id)),

Reference

-5

If you need more than one post, use get_posts(). It leaves the main query alone and returns an array of posts that's easy to loop over.

1
  • 1
    This answer misunderstands the question
    – wickywills
    Commented Apr 21, 2022 at 6:56

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