Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Last active May 10, 2018 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarySwift/5bca400e32b9f2b9c7c2446a6903ff5e to your computer and use it in GitHub Desktop.
Save GarySwift/5bca400e32b9f2b9c7c2446a6903ff5e to your computer and use it in GitHub Desktop.
Get WordPress content - various ways

WordPress post_content notes

Display the post content. (Standard)

This will process shortcodes, parse tags etc.

(https://developer.wordpress.org/reference/functions/the_content/)

while ( have_posts() ) : the_post();
	the_content();		
endwhile; 

Get the post content.

An important difference from the_content() is that get_the_content() does not pass the content through the 'the_content' filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.

(https://codex.wordpress.org/Function_Reference/get_the_content)

ech get_the_content();

Oustside the loop

The will get the content and apply filters

echo apply_filters('the_content', get_post_field('post_content', $post_id));

The will just get the content, similar to get_the_content().

get_post_field('post_content', $post_id); 

If you want to invoke the post object, you can also do it this way.

$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;

(https://wordpress.stackexchange.com/questions/9667/get-wordpress-post-content-by-post-id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment