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:
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.
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!):
postis a content item stored in the
wp_poststable. Each has a
post_typeassigned to it, which could be
pageor something else.
- Post is a post type, used for blog posts or news items depending on the type of site you’re developing.
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:
- 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
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 Type||Used For||Notes|
|Post||Blog posts or similar||Core post type – listed on main blog page|
|Page||Static pages||Designed to be displayed singly – not generally used in archives (although they can be).|
Pages can have parents, which are defined using the
Each page can have multiple children but only one parent.
|Attachment||Documents 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 |