There are several objects in WordPress that we use all the time, namely $post, $comment and categories/tags. If you’re like me though, you never remember everything that’s in there, or the names of the values you want.
This cheat sheet will be a reference which you (and I) can refer to the next time you’re using one of the WordPress objects.
Quick Primer on Using Objects
If you haven’t used objects before or aren’t familiar with the syntax, there are 3 simple things to know:
1 – Objects store details about a particular “thing”, e.g. the $post object will store details like the date and author of the post.
2 – To access a value, you use the following syntax:
1 | $object->property |
e.g. If we wanted to print out the post’s ID, we’d write:
1 | echo $post->ID; |
3 – If you need to see everything that’s in your object, use the following to get a nicely laid out view of it:
1 | <pre> |
(Add in the extra ‘e’ in </pre>. I’ve just taken it out so my syntax highlighter plugin still works!)
The $post Object
Let’s start with the one you will use the most. In every WordPress loop, a global $post variable is set containing everything about that post/page.
This is a global variable, so you can access it outside of the loop. To do that, add this to your PHP before you use it:
1 | global $post; |
Property | Value | Example |
---|---|---|
ID | Unique post ID. | 443 |
post_author | User ID of the author. | 1 |
post_date | In the format: yyyy-mm-dd hh:mm:ss | 2010-07-29 16:59:54 |
post_date_gmt | Same as above, but in GMT timezone. | 2010-07-29 18:59:54 |
post_content | Similar to HTML view in editor. No paragraphs/linebreaks. | |
post_title | Human-readable post title. | Your Post Title |
post_excerpt | Content of the excerpt, if one has been explicitly set. | |
post_status | published/pending/draft | published |
comment_status | open/closed | open |
ping_status | open/closed | open |
post_password | Plain-text version of the posts’ password. | password1 |
post_name | The post’s slug. | your-post-title |
to_ping | Addresses entered in the “Send Trackbacks to:” box before publishing. | http://problogdesign.com |
pinged | Addresses entered in the “Send Trackbacks to:” box that have been pinged. | http://pliablepress.com |
post_modified | Server time the post was last modified. | 2011-02-01 20:30:08 |
post_modified_gmt | GMT time the post was last modified. | 2011-02-01 21:30:08 |
post_content_filtered | “can be used by plugins for caching expensive post content transformations” (Link) | |
post_parent | ID of the page’s parent (if it has one) | 332 |
guid | Unique link to the post (Not the permalink though!) | http://problogdesign.com/?p=6335 |
menu_order | Value set in the “Menu Order” box on Pages. | 0 |
post_type | post/page/attachment/revision/nav_menu_item/custom-type | attachment |
post_mime_type | MIME type of attachments. | image/jpeg |
comment_count | Number of comments. | 7 |
ancestors | Array of parent/grandparent etc. pages. [0] = parent ID [1] = parent’s parent etc. | Array |
filter | How the post content has been filtered before being returned (Default ‘raw’ = minimal filtering/sanitization). | raw |
For simplicity sake, I’ve referred to certain values as being used for Pages only. Of course, if you’ve defined a custom post type that uses those attributes (e.g. post_parent), then it will be used there too.
Tag and Category Objects
Each individual category or tag can be represented as an object. We’ll start with a quick look at the 3 main functions for getting tag and category objects.
The get_term_by() function is fantastic for finding a single tag or category. You can tell it to search by name, ID, or slug, and whether it’s a tag (post_tag), category or custom taxonomy you’re after, e.g.
1 | $term = get_term_by('slug', 'featured', 'post_tag'); |
To get all the tags on a post, you would use the get_the_tags() function, e.g.
1 | $tags = get_the_tags(); |
And similarly, to get all of the categories on a post, we’ll use get_the_category():
1 | $tags = get_the_category(); |
Property | Value | Example |
---|---|---|
term_id | The category or tag ID. | 42 |
name | The human-readable name. | Breaking News |
slug | Permalink slug for the term. | breaking-news |
term_group | If one term is an alias for another, then both will share the same term group (Default = 0, for all terms) | 4 |
term_taxonomy_id | ID of the taxonomy’s entry in the wp_term_taxonomy table. | 54 |
taxonomy | category/post_tag | post_tag |
description | Text entered to describe this tag/category. | Up-to-the-minute news releases! |
parent | ID of this term’s parent term (e.g. parent category). 0 if it has none. | 0 |
count | Number of posts using this tag/category. | 15 |
object_id | ID of the object that the category/tag is attached to, e.g. the post ID | 226 |
For backwards compatibility, the $category object returned stores many of these values under other names as well, e.g. term_id and cat_ID both return the category ID.
To make things easier on yourself though, forget the category-specific versions and use the ones above instead. That way, you won’t have to remember the difference between the tag and category versions.
The $comment Object
Last of all, we’re going to look at the $comment object. During your comments loop, you can access this object to get specific information about the comment.
Property | Value | Example |
---|---|---|
comment_ID | Unique ID of this comment. | 735 |
comment_post_ID | ID of the post this comment was made on. | 146 |
comment_author | Name of the commenter. | Michael Martin |
comment_author_email | Commenter’s email address. | email@example.com |
comment_author_url | Commenter’s website address (If they left one) | http://problogdesign.com/ |
comment_author_IP | IP address of the commenter. | 192.168.0.1 |
comment_date | (Server) Time the comment was left, in format yyyy-mm-dd hh:mm:ss | 2011-01-15 14:01:48 |
comment_date_gmt | GMT time that the comment was left. | 2011-01-15 16:01:48 |
comment_content | Unfiltered version of the comment’s text (No paragraphs etc.). | |
comment_karma | Unused, and may be removed in future. | 0 |
comment_approved | 1 or 0, depending on whether or not the comment is approved. | 1 |
comment_agent | Long string of the commenter’s user-agent. | Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6… etc. |
comment_type | Blank for regular comments. Alternatively can be pingback or trackback. | |
comment_parent | If threaded commenting is enabled, the ID of the comment this was in reply to (0 if none). | 463 |
user_id | If the comment was made by a logged-in user, then their user ID is stored. | 1 |
And that finishes up our look at the main WordPress objects. There are functions for getting directly at a lot of this information, but at times, it’s far easier to simply take what you need from these objects without worrying about all the different function names.
If you’d like to see examples of how these can be put to use, let me know in the comments and I’ll share some of the ways I’ve used them! And of course, feel free to share your own!