Understanding and Working with Posts in WordPress

By Rachel McCollin

wp_posts tree

Working with Posts in WordPress

WordPress is an excellent content management system (CMS), and working with posts in WordPress can be a simple task. Like so many examples of WordPress terminology, the term ‘posts’ can be confusing, as it refers to a post type as well as a content type stored in a specific database table.

Here, I’ll clarify the difference between the two and look at post types in detail.

In the previous part of this series, I examined the different content types in WordPress, which are stored in a number of different database tables. These are:

  • posts
  • users
  • comments
  • links

In this tutorial I’ll examine posts in more detail and outline the post types, the relationships between them and how understanding all this can help you work with them.

Defining Posts

First, I’ll start by defining posts and how they differ from Posts (yes, it’s confusing and adding a capital letter doesn’t make things all that much clearer!):

  • A post is a content item stored in the wp_posts table. Each has a post_type assigned to it, which could be post, page or something else.
  • Post is a post type, used for blog posts or news items depending on the type of site you’re developing.

The wp_posts table is shown in the image below.

Make sense? If not, read on, all will become clear!

Post Types in WordPress

WordPress comes with five post types built in:

  • Post
  • Page
  • Attachment
  • Revision
  • Navigation Menu Item

In addition to this you can add your own custom post types using the register_post_type() function, meaning that your WordPress installation can have as many post types as you like. Each post type has the same status as the inbuilt post types – occasionally I have heard of people thinking that ‘custom post type’ is a post type itself. It isn’t!

I’ll illustrate that with an example. When writing a custom query for posts, you would add the following to your query’s arguments:

'post_type' => 'post'

But if you’ve created a custom post type called my_post_type, you would query it with the following:

'post_type' => 'my_post_type'

You can use this post_type argument to query any post type, and indeed you can query multiple post types if you wish. I won’t go into detail on that as it’s outside the scope of this tutorial, but you can read more on the WordPress Codex page for WP_Query.

Understanding the Default Post Types

Let’s have a quick look at each of the built-in post types, how they’re similar to each other and how they differ. Note that in the table below, I use the term post in the generic sense to describe content of all post types.

Post TypeUsed ForNotes
PostBlog posts or similarCore post type – listed on main blog page
PageStatic pagesDesigned to be displayed singly – not generally used in archives (although they can be).
Pages can have parents, which are defined using the post_parent field in the record for the child page.
Each page can have multiple children but only one parent.
AttachmentDocuments and images (which may or may not be attached to a post)Media uploaded to a post will have the ID of that post in its post_parent field. Images uploaded via the Media screen in the dashboard will have that field …read more    


Posted in WordPress and tagged , .

Leave a Reply

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