In the first two parts of this series, I introduced the WordPress database and its structure and showed how relationships between different content types are managed.
Here, I'll look at content types in more detail and describe the different types of content in WordPress and how they interact.
When we think of content in WordPress, we normally think of posts and pages. But things aren't quite that simple. WordPress uses a number of content types, and posts and pages are just two examples of one of these.
Understanding the types of content, how they're stored and how they interact with each other will help you get a grip with on WordPress at a more advanced level and write advanced queries for your themes and plugins.
Let's take a look!
The Main Content Types in WordPress
There are four kinds of content in WordPress:
Many of these behave quite similarly, as we'll see.
Posts are the most important content type in WordPress. The terminology around them can be confusing as post is used to describe anything in the
wp_posts table but it is also used to describe a particular post type. Adding the following to a query may seem superfluous but it does make sense!
'post_type' => 'post'
There are five default post types built into WordPress, these are:
- navigation menu item
As I explained in the previous tutorial on this series, covering relationships between data, posts can be related to each other. This is generally used to define which post or page an attachment is attached to, and which page is the parent of another page.
The first three post types will be familiar to WordPress developers as posts, but it's less obvious that the last two are posts. Each revision is saved in the
wp_posts table with the post it relates to as its parent (using the
post_parent field), and navigation menu items are saved as posts and then output via a query when the menu is displayed. This is why navigation menus can suddenly disappear if you use the
pre_get_posts() filter incorrectly!
You can add as many additional post types as you need for your site, by creating custom post types. These are just additional post types which behave in the same way as posts, pages or attachments, depending on how you set them up when registering the post type.
The diagram below show the
wp_posts table and the tables it is related to:
Comments are stored in their own database table,
wp_comments. They work in a similar way to posts in that each can have metadata attached to it via the
wp_commentmeta table, but have different content and so different fields are required for their table.
Comments will be linked to posts via a one-to-many relationship and they are also linked to each other using the
comment_parent field - this is used to identify comments which are replies to other comments. They can also optionally be linked to the
wp_users table if they are made by users who are signed-in.
The diagram shows the
wp_comments table and its relationships:
Users have their own table,
wp_users, and also have metadata stored in the
wp_usermeta table. At first glance users may seem to be a very different content type to posts, but they are more similar than you think - they can be queried and output in an author archive, and they have content in the form of biographical data about the users. This content is structured quite differently from posts, links and comments which is why users need their own table.
The diagram shows the
wp_users table and its relationships:
You'll learn more about user data and how it relates to other tables in a later part of this series.
The final content type is the link. These have been all but phased out by WordPress, with the blogroll no longer being available by default since version 3.5.
Links work in a similar way to posts in that they have content and can have taxonomy terms assigned to them. However they are different from posts in that they have quite different fields, used to store data about the link target, its description and so forth. They aren't linked to users so can't be assigned an author, and they also don't have a field to identify a parent so can't be attached to a post - but you can use taxonomies to include them in archive pages.
wp_links table and its relationships to other tables are shown below:
A Note on Meta Data
Of the four content types covered above, three can have meta data assigned to them:
I'll look at meta data in more detail in the later in this series and show you how it's stored.
As we've explored, WordPress uses a variety of content types to store data and the relationships between it. The types of content stored in the database is not limited to posts and pages, but also includes your own custom post types plus other content types such as attachments, revisions and navigation menu items.
Understanding what each of these is and how it works, as well as the similarities and differences between them, will help you to develop more powerful WordPress themes and plugins.
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post