Home

Get WordPress Post Content Outside the Loop

Some WordPress functions only work when you're inside The Loop. But you can still get to the post content when you're outside The Loop.

When developing for WordPress, you have to be cognizant as to whether you're inside or outside of The Loop when performing tasks. Several WordPress functions will work only within The Loop. I've had issues getting content and other post data when working outside The Loop.

Here's how you would echo the post content, grabbing the post object using its ID.

<?php

// you need to know the post ID
$id = 1234;
// retrieves post object
$post = get_post($id);
// do something with the post, e.g. echo its content
echo $post->post_content;

?>

Thought this may seem obvious, the gotcha is that you need to know the ID of the post you want to retrieve.


References

Let's Connect

Keep Reading

Get WordPress Post Tags as Links

How to extract post tags and make them into links while in The Loop.

Jul 02, 2013

Is React the New WordPress?

I heard this comment from Henri Helvetica during the JamDev conference that made me stop. How is a React developer like a WordPress developer?

Apr 11, 2023

Using jQuery and CoffeeScript in WordPress

The standard ready function doesn't always work in WordPress plugins. Here's a workaround.

May 01, 2013