183

How can I get WordPress post content by post id?

butlerblog
  • 5,141
  • 3
  • 28
  • 44
m3tsys
  • 2,057
  • 2
  • 15
  • 11

5 Answers5

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;
Rarst
  • 100,126
  • 10
  • 161
  • 298
Bainternet
  • 67,748
  • 8
  • 132
  • 188
158
echo get_post_field('post_content', $post_id);
fuxia
  • 107,219
  • 39
  • 255
  • 462
realmag777
  • 1,714
  • 1
  • 11
  • 9
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.

Peter Mortensen
  • 268
  • 2
  • 10
Ranuka
  • 1,794
  • 3
  • 18
  • 31
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

Jashwant
  • 171
  • 1
  • 4
-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.

Peter Mortensen
  • 268
  • 2
  • 10
DigitalDesignDj
  • 203
  • 1
  • 6