Customization

The WordPress $Post Object and You

September 15, 2011   ·   By   ·   1 Comment   ·   Posted in Customization, Functions, The Loop, Wordpress

Ah, fair $post object. Lets talk about how great you are.

The What What?

Post Object. WordPress stores all post data (think content, author, tags, etc) in a single object, accessible to themes and plugins on each page load. Basically, this is the place you’re going to go to if you want to get some extra information about a post for whatever clever scheme you’ve got cooked up. Need to know how many “n”s are in this particular post? Talk to the post object. Not sure when this post was originally published? Post object. Looking for lawyers who ride? Now we’re talking – but you want the Law Tigers, not the $post object. Watch their commercials. You won’t regret it.

How do I get at this mystical post object?

Single Posts

The post object is available nearly everywhere you’re likely to be looking at a post. If you’re working with a single post page, or an actual page, you’ve got access to that post’s (or page’s) post object anywhere within your theme file. It’s all held in a variable named $post.

Multiple Posts

If you’re on a page with multiple posts, things are a little trickier but not much. If you’re inside the loop, you’ve got access to a $post object (named $post) referring to the current post in the loop.

Elsewhere

What if you want to get at a post that isn’t the one you’re looking at on a particular page – or what if you want to get at one from a plugin, or somewhere where there isn’t a current post explicitly defined? There’s a function for that:

$id = 41;
$mypost = get_post($id);
print_r($mypost);

get_post() to the rescue! There are, however, a couple of gotchas with get_post.

  1. You have to pass it a variable – not an integer. See how in the example above, I set $id to 41, an then used $id as the argument to get_post(), instead of just saying get_post(41)? That’s important. If you try to pass an integer to get_post(), it will blow up. No Joke.
  2. You probably shouldnt use the variable $post. Why not? Because WordPress uses $post all the time, and chances are you’ll either overwrite WordPress’ version of $post, or they’ll overwrite yours – either way, somebody is going to be mad. Choose something more clever than $post (like $mypost!). I know you can do it.

What if I don’t like getting the data back as an object? I’m an associative array kind of guy.

get_post() gets you. So do I. There’s a second, optional argument to get_post that determines what it spits out at you. It defaults to OBJECT, but you can also pass in ARRAY_A, or ARRAY_N to get an associative array or a numeric array version of the post data.

Ok, I’ve got a post object – what kind of data is in there?

Find out for yourself! The snippet above uses the function print_r() – it will show you the structure AND data of a particular post object – very useful stuff. Surround it in a <pre> tag, and you’ll be in even better shape – it will display nicely in your browser.

I’m too lazy for that. Didn’t I come here for you to teach me about this?

Well played. Here’s a quick rundown on the data the post object gives you:

Property Example Explanation
$post->post_title The WordPress $Post Object and You Title of post.
$post->post_excerpt Learn about the WordPress $Post object! Manually created post excerpt. If you didn’t purposefully create an excerpt on the add post page, you’re not getting anything here.
$post->post_status
  • publish
  • pending
  • draft
  • auto-draft
  • future
  • private
  • inherit
  • trash
Current status of the post.
$post->comment_status
  • open
  • closed
  • registered_only
Comment status for this particular post.
$post->ping_status
  • open
  • closed
Does the current post accept pingbacks and trackbacks?
$post->post_password 123456 The plaintext password for this post, if there is one. Empty otherwise.
$post->post_name the-wordpress-post-object-and-you A normalized, sanitized version of the post title, used to generate pretty permalinks.
$post->to_ping http://technorati.com http://someothersitetoping.com Space separated list of sites to ping which have not been pinged yet. Modified by “Send Trackbacks” field on add post page.
$post->pinged http://technorati.com http://someothersitetoping.com Space separated list of sites already pinged for this post.
$post->post_modified 2011-09-15 21:21:59 Time this post was last modified, based on local server time. MySQL timestamp format.
$post->post_modified_gmt 2011-09-15 21:21:59 Time this post was last modified, based on GMT timezone. MySQL timestamp format.
$post->post_content_filtered Post Content This field is designed to hold a version of the post for caching, in situations where filters are being run on the post that are “expensive” (slow), and undesireable to run every time. Not used in WP core, may be used by plugins.
$post->post_parent 0 ID of the parent post. Posts that have parents are generally revisions, or attachments. If the post_parent is 0, this is a bona-fide, original post.
$post->guid

http://codegarage.com/blog/?p=41

Global Unique Identifier for the post. According to the documentation page on wordpress.org, this can’t be relied on to actually work as a link to the post, but I’m not totally sure why. Maybe in cases where the site url has changed?
$post->menu_order 0 Integer determining the order in a list of posts. Generally used for pages in menus, but can be used by plugins for special ordering.
$post->post_type
  • post
  • page
  • attachment
The particular type of post this is. Attachments are generally images, pdfs, etc. Posts and pages are self explanatory.
$post->mime_type image/png Mime type for attachments.
$post->comment_count 14 Number of comments on this post currently.
$post->post_ancestors array Array of parent posts for this post.

Whew!

I think that about covers it. One last question you might have:
How do I get WordPress to spit out properly formatted post content?

Good luck!

One Comment

Submit a Comment