scriptygoddess

12 Oct, 2011

WordPress Objects

Posted by: Jennifer In: WordPress|WordPress: Lessons Learned

For some custom templates, I need to get information from various WordPress objects, either relating to the current page, or from elsewhere in the site. The best way to do that is to get the object in a variable then do a print_r($objectvar) to see all the information included with that object. Even though I use these objects almost on a daily basis, I forget what's in them, and running print_r to remind myself what the object elements are is a pain. So I'm storing them here for future reference.

get_queried_object

Scenario: On a custom taxonomy page. (In this case the custom taxonomy is called "Product Category" – the specific category/term page we're on is "Accessories". To get specific details on the term – I used get_queried_object (which is a pretty useless codex page – there's barely any information there)

$term = $wp_query->get_queried_object();

This is what $term looks like:

stdClass Object
(
[term_id] => 5
[name] => Accessories
[slug] => accessories
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => product-category
[description] =>
[parent] => 0
[count] => 7
)

get_queried_object on a page looks something like this:

[ID] => 6
[post_author] => 1
[post_date] => 2010-03-29 05:38:46
[post_date_gmt] => 2010-03-29 05:38:46
[post_content] => This is my page content. Woo hoo! Hi.
[post_title] => My Totally Awesome Page
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => my-totally-awesome-page
[to_ping] =>
[pinged] =>
[post_modified] => 2010-10-04 16:43:41
[post_modified_gmt] => 2010-10-04 22:43:41
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://www.mydomain.com/?page_id=6
[menu_order] => 3
[post_type] => page
[post_mime_type] =>
[comment_count] => 1
[ancestors] => Array
(
)
[filter] => page

On a category archive page – this is what the object looks like:

[term_id] => 3
[name] => Press Releases
[slug] => press-releases
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 21
[cat_ID] => 3
[category_count] => 21
[category_description] =>
[cat_name] => Press Releases
[category_nicename] => press-releases
[category_parent] => 0

get_categories

Scenario: We want to get a list of terms in a custom taxonomy. Here are our $args:

'parent' => (A PRENT CAT ID),
'hide_empty' => 0,
'taxonomy' => 'location'

Then we pull in the list via get_categories

$mycats = get_categories($args)

This is one of the objects in $mycats (Can get one via a for each loop…)

[term_id] => 1393
[name] => Central Florida
[slug] => central-florida
[term_group] => 0
[term_taxonomy_id] => 1401
[taxonomy] => location
[description] =>
[parent] => 27
[count] => 3
[cat_ID] => 1393
[category_count] => 3
[category_description] =>
[cat_name] => Central Florida
[category_nicename] => central-florida
[category_parent] => 27

get_term_by

Scenario: get information on a specific term using get_term_by

The example below was done using get_term_by('id'… etc. Here is what the object looks like that I got back:

[term_id] => 24
[name] => Northern California
[slug] => northern-california
[term_group] => 0
[term_taxonomy_id] => 24
[taxonomy] => location
[description] =>
[parent] => 23
[count] => 24

I'll continue to add to this post over time. I've had it sitting in draft forever – but figured it was time to hit the publish button already. :) I think it's interesting to note that in the above objects – we're looking at a particular term in a taxonomy – but depending on how we obtained it – will also depend on what elements are in that object. ie. the presence of "cat_ID" in one "category_count" (not sure how this is different than "count", also not really sure what "term_taxonomy_id" is but I notice that it doesn't always equal term_id – so these two are not one and the same…

get_children

I use get_children very often to get all attached images to a post. When I do, the code usually looks like this:

$images = get_children(array(
'post_type' => 'attachment',
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'));

$images will have multiple objects in it (if there are multiple images attached) here's an example of one of them:

[ID] => 1171
[post_author] => 2
[post_date] => 2011-10-02 21:11:39
[post_date_gmt] => 2011-10-03 03:11:39
[post_content] => (FYI - this is the image description)
[post_title] => IMG_2168.jpg
[post_excerpt] => (FYI - This is the image caption)
[post_status] => inherit
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => img_2168-jpg
[to_ping] =>
[pinged] =>
[post_modified] => 2011-10-02 21:11:39
[post_modified_gmt] => 2011-10-03 03:11:39
[post_content_filtered] =>
[post_parent] => 1173
[guid] => http://www.domain.com/wp-content/uploads/2011/10/IMG_2168.jpg
[menu_order] => 0
[post_type] => attachment
[post_mime_type] => image/jpeg
[comment_count] => 0
[filter] => raw

No Responses to "WordPress Objects"

Comments are closed.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements