• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
SnippetPress

SnippetPress

The Home of WordPress Snippets

Vultr Web Hosting
  • Cheat Sheets
  • Reviews
  • Snippets
  • Tutorials

WordPress Post Object Reference Cheat Sheet

May 12, 2020 by SnippetPress Leave a Comment

As somebody who works on WordPress sites a lot, I’m often having to access the same properties for the WordPress $post object day in day out. The problem is, my memory is like a sieve, so I’m constantly having to search for how to access these properties. That’s what this quick reference cheat sheet is designed for. It lists the different properties available for a WordPress $post object. Simples!

Accessing the $post object

If you don’t already have access to the post object (for instance, it hasn’t been passed to you as part of the filter you’re using), then you need to get this first before you can access the properties.

To get the object for the current post in the loop, you can use the WordPress global $post:

global $post;

In order to get a different post object than the one in the loop, you’ll need to use the WordPress function get_post(). This allows you to retrieve a $post object by its ID:

get_post( 82 ); // Replace 82 with the ID of the post you want to retrieve

Accessing $post object properties

Once we’ve got access to the $post object, we can access its properties:

// Get the post ID
$post->ID;

// Get the post author's user ID
$post->post_author;

// Get the date the post was published (in the sites local time)
$post->post_date;

// Get the date the post was published (in GMT/UTC)
$post->post_date_gmt;

// Get the date the post was last modified (in the sites local time)
$post->post_modified;

// Get the date the post was last modified (in GMT/UTC)
$post->post_modified_gmt;

// Get the post type
$post->post_type;

// Get the post name - this will be the same as the post slug
$post->post_name;

// Get the post title
$post->post_title;

// Get the post content
$post->post_content;

// Get an array of categories the post is in
$post->post_category;

// Get the post excerpt
$post->post_excerpt;

// Get the post status (published, draft etc)
$post->post_status;

// Get the comment status of the post (open or closed)
$post->comment_status;

// Get the number of comments on the post
$post->comment_count;

// Get the ping status of the post (open or closed)
$post->ping_status;

// Get the password required to view the post (will be empty if no password is set)
$post->post_password;

// Get an array of URLs to ping when the post is published
$post->to_ping;

// Get an array of URLs that were pinged when the post was published
$post->pinged;

// Get the cached version of the post (used by some plugins)
$post->post_content_filtered;

// Get the posts parent ID (if the post has no parent this will be 0)
$post->post_parent;

// Get the posts GUID
$post->guid;

// Get the mime type of the post (only works for media attachments)
$post->post_mime_type;

Filed Under: Cheat Sheets, Cheat Sheets, WordPress Core

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

E-mail Newsletter

More to See

WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

July 15, 2020 By SnippetPress

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 By SnippetPress

Footer

Recent

  • Change The WordPress Page Title Separator Without A Plugin
  • WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart
  • WooCommerce – Display an “Add $X To Get Free Shipping” Notice
  • WooCommerce – Integrate Google Merchant Center Customer Reviews
  • WooCommerce – Disable Out of Stock Variations

Search

Copyright © 2023 ·SnippetPress