WordPress $post, $term and $comment Object Cheat Sheet
56There 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 2 3 | <pre> <?php print_r($object); ?> </pr> |
(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 ( [0] => 61 [1] => 57 ) |
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 2 3 4 | $tags = get_the_tags(); foreach($tags as $term) { echo "$term->name | "; } |
And similarly, to get all of the categories on a post, we’ll use get_the_category():
1 2 3 4 | $tags = get_the_category(); foreach($tags as $term) { echo "$term->name | "; } |
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!
Enjoy this post? You should follow me on Twitter!
Wow what a list! Def useful for reference. Something like this could make a cool infographic.
Ah, never thought of that Devin! Nice idea, will remember that for the next time I do a post like this!
Nice, this will save me a lot of time. I always forget these and then I have to search for them. Now I have them all in one place.
An infographic is a great idea, then you could just print it out and hang it on the wall when you are working on your blog.
my co-worker’s mother-in-law makes $87 every hour on the laptop. She has been out of a job for 6 months but last month her check was $7747 just working on the laptop for a few hours. Read more on this site… http://www.MakeCash16.com
These commands are really useful and now i can save my time using these simple and easy information. Thanks to you Michael.
Nice going easier then wordpress.org:s list! thx
Thanks!
I was just looking for this Michael.
But I have one doubt: using the call of “post title”, would it be possible to return a list of articles from a dynamic tag? For example I want to list articles with the tag which is equal to the current post title.
Page bookmarked!
Yep, that’s definitely possible. That said, you’d be better of using the post_name field (i.e. the slug of the post). That way, you’ll get the hyphenated, lowercase version of the tag (Which is what the query will want when you search for the tag)
e.g. something like this would do it:
global $post;
$tag = $post->post_name;
$query = ‘posts_per_page=5&tag=’. $tag;
$tagsloop = new WP_Query($query);
if($tagsloop->have_posts() ) :
while($tagsloop->have_posts() ) : $tagsloop->the_post();
(Loop as normal now)
You have to use the WP_Query method (Rather than a normal query_posts() ) incase you use this inside your real post loop.
Michael, thanks for the tip. But how could I use this on a template? Should I just add the <?php code around? Thanks
Nice cheat sheet. I generally end up using print_r() whenever I need to look up the data an object or array holds, mainly out of habit. (I’ve worked with XML and JSON parsing enough that I’ve gotten used to not having a handy documentation page.)
Also, I thought I might point out a relevant post of mine that shows how to create your own objects on the fly.
Definitely a very handy function that Matt! Would love to see the link to your post, definitely share it here!
Strange, I thought I posted the link. Here: http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/
Ah, nice tutorial Matt! Looks very handy!
Whau. Great tips there. Very usefull backgrund informaion.
Thanks for sharing.
It’s too nice. But i like it
I’ve read about an object (I can’t remember the name of it) that basically is like a “meta” title, i.e. shows one title to the search engine and another title to a human. I can’t imagine this would be a good idea, but I was awe struck when I heard about it. Is anyone familiar with this?
That sounds like some form of cloaking I think? I can imagine a few ways of doing it (Maybe showing your search engine title in the HTML and using JavaScript to then convert it for real users), but really couldn’t recommend it. If Google caught on, could be a heavy penalty on the site :(
I think sticking with regular meta titles and the HTML
Thanks for the cheat sheet. I use it all the time and It’s saved me much effort trying to find the right paramenter in a number of instances. Thanks for sharing
Hi Michael
Just stopped by to say hello – you know this PHP business is beyond the likes of me.
How goes the university course?
And… how goes Pliable Press?
If all goes well, you could be the first guy to retire on the same day that you graduate. LOL
Hi Keith,
Great to hear from you again, hope you’ve been keeping well!
I’m out of uni this year (Back next year though). It’s a placement year for my course, so I’ve spent it working full time here (And absolutely loved it! Dreading next September when it ends! :( )
PliablePress is going well. A nice big chunk of time ahead of me set aside for it, so definitely new themes and new features in the next few weeks!
What about you? Life been treating you well I hope? :D
Michael
(PS – Retiring? Now, where would the fun be in doing that yet? Having far too much fun as it is! :D )
I really appreciate you for posting this reference. It could be handy for wordpress developers.
Thank you for the cheat sheet. It was of great help. Thanks again.
Thanks for this well-written and extremely helpful post. Nice to have this info distilled down from the Codex. Best.
Great list – thanks! I haven’t seen one as in-depth as this before – it’ll save me time even if only for the $post object, which is a particular bugbear of mine!
I notice the post is from the start of the month, did you ever get round to creating an infographic?
This is my first time on the blog, but it’s great to see a post like this which can save me time without cutting corners, as so many articles can lead you to do. Thanks again!
I’m going to give it a try on my blogs
very useful post thanks a lot.
Wow. Amazing list. Thank you.
Nice little docu…
Hopefully, I can use them when I made changes to my site http://www.codingwhiz.com.
Thanks again. Nice cheat sheet.These commands are really useful
I need to learn more about wordpress :)
Excellent cheatsheet. I have to learn more wordpress. thanks for this excellent post.
Really informative. I will try and apply some of this to my blog. Thanx
A very nice list, i did not know these values existed.
Thanks Michael ,
Nice article
Regards
Ned
thats so perfect,
I love this blog, broadening my knowledge…
thanks again..
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.
Hey, its just amazing work Michael. Its great article.
very informative and helpful, will try these in my blog
This blog is very useful for me. Thank for sharing.
This cheat sheet is freaking awesome! Glad I stumbled upon this.
Thanks for the detailed properties…
Definitely a good quick reference…
Great blog, it’s nice to help others by telling them what is harmful and what preferred.
Hi, this cheatsheet image format have you?
The comment object really works, it saves my time . thanks
Really nice blog, thanks for the detailed tutorial.
hey my dear blogger…this code :-
Very informative thank you..
The information is really useful for me and excellent cheat sheet. I have to learn more about word press.
I like your work. The cheat sheet is a handy tool I am sure to use.
my buddy’s mom makes $85 an hour on the laptop. She has been laid off for 6 months but last month her pay was $7422 just working on the laptop for a few hours. Read more on this web site… MakeCash16.com
my best friend’s sister makes $83 every hour on the internet. She has been out of work for 7 months but last month her paycheck was $7475 just working on the internet for a few hours. Here’s the site to read more… http://www.MakeCash16.com
I want to use this cheat sheet. Awesome!
Tout n’est que la lèvre, Nike Air Max Shoes la sagesse qui veut l’expérience. Expericence est un professeur dur parce qu’elle donne le premier test,Nike Air Max Shoes la le?on afterwards.Law Vernon, writer.Experience britannique n’est pas intéressante jusqu’à ce qu’il commence à se répéter, en fait, jusqu’à il le fait , il n’est guère d’expérience louis vuitton Site Officiel.
Thank you for this awesome compilation of wordpress objects. Even at wordpress is not easy to find all this info quick and handy.
Thanks a lot!
Thanks for the list it’s easier then the one on wordpress.org
thx for the list
The New Executive Office Building is a nondescript modern high-rise building a block north on 17th Street; good north face outlets warm enough the original EOB was known as the Old EOB when the additional building was put into use; very few long-time Washington residents are aware of the “Eisenhower” designation.