Android & Wordpress Help center

How to get proper HTML formatting with get_post method.

I was coding a custom excerpt function in functions.php and for that i had to use the get_post() method which fetches every details associated with a particular post. I used it to get content of the post id being passed as a param to my custom post excerpt function.

I noticed a very subtle behaviour of get_post() method while using the object that get_post() method returned.  When i used the object to get the post_content, the html formatting that it was providing me was not similar to the one i was getting (for the same post) when i used the_content() method. I was really astonished as to why this was happening !!!

Googling a bit i found that it is some editor filters that the two methods follow to throw the html, which was causing a mismatch in the HTML (post_content) that they were throwing.


Now to get around this situation & to get the same html post_content as the one provided by the_content() method you can do something like this.

In your code instead of doing this :

// ID of your post 

$post_id = 100;

// Get every information about the post

$post_details = get_post($post_id);

// Get the post_content to use it somewhere 

$post_content = $post_details->post_content;

Change the last line where you are fetching the post_content to something like this :

// Get the post_content to use it somewhere 

$post_content = apply_filters(‘the_content’, $post_details->post_content);

Applying the_content() filter will help you get post_content’s HTML formatted the way you would have got when using the_content() method.


Hope it helps some one!



Single Post Navigation

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: