WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can i get wordpress posts content by post id?

share|improve this question
up vote 73 down vote accepted

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;
share|improve this answer
51  
Shorthand for specific field: $content = get_post_field('post_content', $my_postid); – Rarst Feb 17 '11 at 21:39
3  
@Bainternet I'm just curious here... what is the part $content = str_replace(']]>', ']]>', $content); do? what's the purpose of it there? – Average Joe Nov 4 '13 at 11:02
    
@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. – Harish Chouhan Mar 18 '14 at 9:21
echo get_post_field('post_content', $post_id);
share|improve this answer
29  
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 Jan 17 '13 at 7:10
1  
This is the best answer if the scope is to get the post content as it is in the WordPress edit page. – Matteo Aug 8 '14 at 13:21
    
Without the code from @KarelAttl line breaks where missing. With the apply_filters code it worked perfectly. – Alexander Taubenkorb Sep 23 '15 at 12:23
    
apply_filters is a good option, but wasn't right for my current purpose. It's good to have both options. – KnightHawk Nov 5 '15 at 18:12

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 loop over.

share|improve this answer

protected by Community Nov 27 '12 at 19:56

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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