Get a WordPress Post’s Content by ID


Categories
Tags

On occasion you might want to get the content from a specific post and display it on one of your pages. Here’s how it can be done using some of WordPress’ inbuilt functions and PHP.

Before you get started you’ll need to know the ID of the post you want to use.

The example below will get and display the body content for a post with an ID of 12. You could use this on a page or post template in your theme files:

$my_postid = 12; //define 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;

Comments

We welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the authors and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).