This is the 13th day of my participation in the More text Challenge. For details, see more text Challenge

Understanding and Working with Content Types in WordPress

Understand and use the content types in WordPress

The first two parts of this series introduced the WordPress database and its structure and showed the relationship between the different content types before.

Here’s a look at content types in more detail, describing the different content types in WordPress and how to interact with them.

When we think about content in WordPress, we usually think of articles and pages, but it’s not that simple. WordPress uses many content types, and articles and pages are just two examples of these.

Understanding content types, how they are stored and how they interact with each other will help you gain a deeper understanding of WordPress and write advanced queries in themes and plug-ins.

Let’s have a look! — Let ‘s take a look!

The main content types in WordPress

There are four types of content in WordPress:

  1. posts
  2. comments
  3. users
  4. links

As we shall see, many of these behaviors are very similar.

Posts

Posts is the most important content type in WordPress.

The terminology around it can be confusing because POST is used to describe anything in the WP_posts table, but it is also used to describe a specific post type — “post.” Adding the following query may seem redundant, but it makes sense:

'post_type'= >'post'
Copy the code

To distinguish, translate post as an article only in the case of a certain type of “article”, and leave the original word unchanged in all other cases by default.

There are five default post types in WordPress:

  1. The article – post
  2. Page – the page
  3. Attachment – attachment
  4. Revision
  5. Navigation menu item (i.e., each item in the navigation menu for a location, and the navigation menu for a location corresponds to term)

As explained in the previous tutorial in this series [Introduction to Relationships between Data], Posts can be associated with each other. This is usually used to define which post or page an attachment is attached to and which is the parent of another page.

The first three types of post are familiar to WordPress developers, but the last two are less obvious.

Each revision saves its associated POST as a parent document in the WP_Posts table (using the POST_parent field), the navigation menu item is saved as POST, and the output is queried when the menu is displayed. That’s why the navigation menu suddenly disappears if the pre_get_posts() filter is incorrectly used!

By creating a custom POST type, you can add as many additional post types as you need. Additional POST types can behave the same as articles, pages, or attachments, depending on how they are set up when the post type is registered.

The following diagram shows the relationship between the WP_posts table and this table:

comments

Comments are stored in its own database table, WP_comments. They work in a similar way to post; each comment can have additional metadata via the Wp_Commentmeta table, but the content is different from that of post, and therefore the fields required for the table are different.

Comments are linked to post via a one-to-many relationship, and they themselves are linked to each other via the Comment_parent field, which identifies which comment the current comment is responding to. If the comments were added by a logged in user, they are selectively linked to the WP_Users table.

The following figure shows the WP_comments table and its relationships:

The user

Users have their own table, Wp_Users, and metadata is stored in the Wp_UserMeta table.

At first glance, users’ content types may seem entirely different from posts, but they are much more similar than you might think — you can query them and output them to an author archive, and they have content in the form of user resume data.

The structure of user content is completely different from posts, links, and comments, which is why users need their own tables.

The following figure shows the WP_USERS table and its relationships

Later in this series, you’ll learn more about user data and how to associate other tables.

link

The final content type is Link. These have been phased out by WordPress and as of version 3.5 blogroll is no longer available by default.

Links work like posts in that they have content and can assign taxonomy entries. Unlike posts, however, linked fields store data about the link target, description, and so on. They don’t link to users, so they can’t assign authors; They also have no field to identify the parent and therefore cannot be attached to a POST; But you can use the taxonomy to include them in the archive page.

The wP_Links table and its relationships with other tables look like this:

About Metadata

Of the four content types described above, three can have metadata:

  1. posts
  2. comments
  3. users

Later in this series, you’ll look at metadata in more detail and show how it’s stored.

The profile

As already explored, WordPress uses multiple content types to store data and the relationships between them. The content types stored in the database are not limited to articles and pages, but also include custom POST types as well as other content types such as attachments, revisions, and navigation menu items.

Understanding what these are and how they work, as well as the similarities and differences between them, will help you develop more powerful WordPress themes and plugins.