WordPress Function to Get Post Content by Post ID

In WordPress we can get the content of the post ID by custom WordPress function. For that add the following code in the template file and function in the theme folder.

1. Add the following code in the template file where the post content to be displayed.

$postID = 12
echo get_post_content_by_id($postID); 

here $postID is the iD of the post, of which the content to be displayed.

2. Add the following function definition in the function.php file in the theme folder.

function get_post_content_by_id($postID) {
	$content_post = get_post($postID);
	$content = $content_post->post_content;
	$content = apply_filters('the_content', $content);
	$content = str_replace(']]>', ']]>', $content);
	return $content;

this function will return the content of the corresponding post id.

Leave a Comment