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.
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
$content = get_post_field( 'post_content', $post_id );
$post_id is the ID of the post you want to retrieve
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
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’,
This filter will capitalize the “P” in WordPress, to forever eliminate “Wordpress” from the planet (or at least the little bit we can influence).
Parses dynamic blocks out of
post_content and re-renders them. This supports content created with the new Gutenberg editor.
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.
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
This corrects issues with
wpautop when applied to shortcodes.
Wrap attachment in paragraph tag before content.
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.
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 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.
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.