Get WordPress post content by post id

  • How can I get WordPress post content by post id?

  • Bainternet

    Bainternet Correct answer

    10 years ago

    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;

    Shorthand for specific field: `$content = get_post_field('post_content', $my_postid);`

    @Bainternet I'm just curious here... what is the part `$content = str_replace(']]>', ']]>', $content);` do? what's the purpose of it there?

    @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.

    maybe you also need do_shortcode() like `$content = do_shortcode(get_post_field('post_content', $my_postid));`

    Is there anyway to preserve the "more_link"?

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM