WordPress $post Variable: A Simple Reference

If you are a WordPress developer you might have found the need to access information that is not readily accessible via the template tags. So before you go and create a complicated function, take a look at the list below. This list of WordPress $post variables shows the information that you can retrieve while in the Loop. Using these variables will enable greater flexibility in your WordPress themes, functions, and plugins.

WordPress $post Variable Keys

Below are the methods of retrieving the information that you are most likely to need.

  • $post>ID – ID of the current post.
  • $post–>post_category - Retrieves the ID of the post category.
  • $post–>post_parent - ID of the page parent. Useful for creating custom navigational elements.
  • $post–>post_title – Post Title
  • $post–>post_excerpt - Post excerpt
  • $post>post_content – Retrieves all of the post content, along with any markup.
  • $post–>post_name – Retrieving the slug of a post.
  • $post–>guid – Post Url
  • $post–>post_author – ID of post author post_parent
  • $post–>post_type - Returns the type, page or post.
  • $post–>menu_order - Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
  • $post–>post_date - Retrieves the integer timestamp for when the post was published. The output can be customized. See the php.net date manual.
  • $post–>post_modified - Retrieves the integer timestamp for when the post was last modified.
  • $post–>post_status - Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
  • $post–>comment_count - Returns the number of comments, pings, and trackbacks for a given post.

How To Use This List

The $post variable contains an array of post content. As shown above, we are able to retrieve specific content by using a key. If you want to retrieve this content in your loop, take a look at the sample code below where the post content is retrieved and stored in $yourvariable.

<?php if (have_posts()) : while (have_posts()) : the_post();
//somewhere in the loop
$yourvariable = $post->post_content;
//do something
endwhile; endif; ?>

This is a simplified version, but it will work in any case, as well as with custom loops using get_posts() or query_posts(). For these loops, however, it will probably be necessary for you to ensure that all post data can be accessed. This is done by using a special WordPress function called setup_postdata(). And it should be called before any $post data is accessed. See its usage below.


Is Something Missing?

We have omitted a few variables, namely those relating to pinging and commenting. Let us know if we should add anything else to our list.

Tags: ,

4 Responses to “WordPress $post Variable: A Simple Reference”


Trackback URL:

Leave a Reply

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com

Go Daddy Deal of the Week: Cheap .COM Domains! Offer expires soon!