google.com, pub-6998054861372143, DIRECT, f08c47fec0942fa0 WordPress $post, $term and $comment Object Cheat Sheet | Free Premium Drupal Theme

WordPress $post, $term and $comment Object Cheat Sheet

· Posted in

WordPress Object Cheat Sheets


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
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;







































































































































PropertyValueExample
IDUnique post ID.443
post_authorUser ID of the author.1
post_dateIn the format: yyyy-mm-dd hh:mm:ss2010-07-29 16:59:54
post_date_gmtSame as above, but in GMT timezone.2010-07-29 18:59:54
post_contentSimilar to HTML view in editor. No paragraphs/linebreaks.
post_titleHuman-readable post title.Your Post Title
post_excerptContent of the excerpt, if one has been explicitly set.
post_statuspublished/pending/draftpublished
comment_statusopen/closedopen
ping_statusopen/closedopen
post_passwordPlain-text version of the posts’ password.password1
post_nameThe post’s slug.your-post-title
to_pingAddresses entered in the “Send Trackbacks to:” box before publishing.http://problogdesign.com
pingedAddresses entered in the “Send Trackbacks to:” box that have been pinged.http://pliablepress.com
post_modifiedServer time the post was last modified.2011-02-01 20:30:08
post_modified_gmtGMT 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_parentID of the page’s parent (if it has one)332
guidUnique link to the post (Not the permalink though!)http://problogdesign.com/?p=6335
menu_orderValue set in the “Menu Order” box on Pages.0
post_typepost/page/attachment/revision/nav_menu_item/custom-typeattachment
post_mime_typeMIME type of attachments.image/jpeg
comment_countNumber of comments.7
ancestorsArray of parent/grandparent etc. pages. [0] = parent ID [1] = parent’s parent etc.
Array
(
[0] => 61
[1] => 57
)

filterHow 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 | ";
}




























































PropertyValueExample
term_idThe category or tag ID.42
nameThe human-readable name.Breaking News
slugPermalink slug for the term.breaking-news
term_groupIf one term is an alias for another, then both will share the same term group (Default = 0, for all terms)4
term_taxonomy_idID of the taxonomy’s entry in the wp_term_taxonomy table.54
taxonomycategory/post_tagpost_tag
descriptionText entered to describe this tag/category.Up-to-the-minute news releases!
parentID of this term’s parent term (e.g. parent category). 0 if it has none.0
countNumber of posts using this tag/category.15
object_idID of the object that the category/tag is attached to, e.g. the post ID226

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.





















































































PropertyValueExample
comment_IDUnique ID of this comment.735
comment_post_IDID of the post this comment was made on.146
comment_authorName of the commenter.Michael Martin
comment_author_emailCommenter’s email address.email@example.com
comment_author_urlCommenter’s website address (If they left one)http://problogdesign.com/
comment_author_IPIP address of the commenter.192.168.0.1
comment_date(Server) Time the comment was left, in format yyyy-mm-dd hh:mm:ss2011-01-15 14:01:48
comment_date_gmtGMT time that the comment was left.2011-01-15 16:01:48
comment_contentUnfiltered version of the comment’s text (No paragraphs etc.).
comment_karmaUnused, and may be removed in future.0
comment_approved1 or 0, depending on whether or not the comment is approved.1
comment_agentLong string of the commenter’s user-agent.Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6… etc.
comment_typeBlank for regular comments. Alternatively can be pingback or trackback.
comment_parentIf threaded commenting is enabled, the ID of the comment this was in reply to (0 if none).463
user_idIf 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!


Powered by Blogger.