Become Wordpress Professional - Your Ultimate Wordpress Guide

Friday, 3 January 2014

How to Get Post Content By Post ID in Wordpress

get post content in wordpress
Getting Wordpress post content for a specific post is easy using core functions. Wordpress is very well known for it's flexibility. Therefore One can easily get or retrieve any data required such as post content, post category, post tags and any other similar data associated. In this short tutorial, you will learn how to to do it.

While performing custom programming, developers often want to access the content of a particular post. It is very easy to get content of a particular post. The only requirement is to have a valid Post ID. Each post is stored in the database with it's unique id that is needed to be passed in a core function of wordpress and retrieve it's content accordingly.

Below is the code that you can use to get content of a particular post:

// Setting the Post ID in a variable for which the content is required
$the_postid = 20; 

$the_post_content = get_post($the_postid);
$content = $the_post_content->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);

// Print Content data or further use it as required
echo $content;

View code on Github in PHP

Please note that the content will be in Raw HTML format as it is stored within a database. Now that you have the post content stored within a variable, it can easily be rendered or styled or further functions or actions can be performed as per developer's requirement.


Post a Comment

Please Avoid Spamming. Comments will be moderated before they are published.