Get WordPress Post Content by Post  ID

The simplest way to get WordPress post content by post ID.

If you’re wondering how to get WordPress post content by post ID then look no further. The following simple approaches will explain the best approaches to use, and when to choose between unfiltered or filtered content.

Raw HTML

This will get the content without any of the built-in WordPress filters that format the output. This means that the $content value will not auto-embed videos, expand shortcodes and add additional formatting like converting line breaks to <p> tags and formatting curly quotes with wptexturize().

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

Where $post_id is the ID of the post you want to retrieve post_content from.

Formatted HTML

If you want to get the content in the exact same way as WordPress’s built-in the_content() function then the following approach is best to use.

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

Similar to the above, $post_id is the ID for a post you want to get post_content for.

Built-in Content Filters

Wondering what filters are applied to the_content function by default? Here’s a good summary that should help you decide whether or not to filter your content or work with the raw HTML. When you use the `apply_filters( ‘the_content’,

capital_P_dangit

This filter will capitalize the “P” in WordPress, to forever eliminate “Wordpress” from the planet (or at least the little bit we can influence).

do_blocks

Parses dynamic blocks out of post_content and re-renders them. This supports content created with the new Gutenberg editor.

wptexturize

Replaces common plain text characters with formatted entities and returns given text with transformations of quotes into smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol. Code within certain HTML blocks are skipped.

wpautop

Replaces double line breaks with paragraph elements using a group of regex rules. This is used to identify text formatted with newlines and replace double line breaks with HTML paragraph tags. The remaining line breaks after conversion become < > tags, unless $br is set to 0 or false.

shortcode_unautop

This corrects issues with wpautop when applied to shortcodes.

prepend_attachment

Wrap attachment in paragraph tag before content.

wp_filter_content_tags

Modifies HTML tags in post content to include new browser and HTML technologies that may not have existed at the time of post creation. These modifications currently include adding srcset, sizes, and loading attributes to img HTML tags. Future similar optimizations should be added/expected here.

do_shortcode

Search content for shortcodes and filter shortcodes through their hooks. If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.

convert_smilies

Convert text equivalent of smilies to images if the option use_smilies is true and the global used in the function isn’t empty.

Filtered vs. Unfiltered

When you should apply the default content filters? It depends and is situational. Typically I don’t apply filters when I simply want a direct output of a post that doesn’t use shortcodes, Gutenberg blocks, and needs to be simple and straight forward. It can help provide precise control over the output of the content when it’s needed.

On the other hand, it’s typically better and more common to filter the content using the built-in hook methods that WordPress provides. This will ensure that your content appears in the same way it would within it’s original page context, so if you’re not sure which one to go with I’d recommend choosing the Formatted HTML function example above.

Conclusion

You should now have a good understanding of how to get post content by ID in WordPress, and whether or not to filter the result in the same way that the_content() is with built-in filters.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.