Tuesday, July 21, 2020

Where are Pages Stored? The WordPress Database

Where are Pages Stored? The WordPress Database

Just where does WordPress store your pages and posts, and how does it access them? 

Pages and posts are stored in the WordPress database. This is an essential part of how your WordPress site works. Without it, none of your pages or posts would be saved and they wouldn’t be displayed when someone visits them.

In this post, I’ll show you how the database in WordPress works and how WordPress builds a page from the database when a visitor comes to your site.

How WordPress Works—Database and Files

WordPress is a content management system (or CMS). This means that instead of creating a static HTML file for each page in your site, it uses a database to store all the content of those pages, and then uses code to access that content each time a page is loaded.

As well as the database, WordPress consists of two more elements:

  • The core files that run WordPress itself, which you install when you set up your site.
  • Any files you upload to the site, including theme and plugin files as well as media attachments.

WordPress uses all these to build the pages in your site without you having to write any code.

So let’s take more of a look at where your pages and posts are stored and how the database is structured.

Where Posts and Pages are Stored

All of the content in your site is stored in the database. This includes:

  • Your pages, posts and any posts of a custom post type (e.g. products if you’re running a store).
  • Data about attachments.
  • Any metadata such as post categories and tags (and custom taxonomies if you have them in your site), as well as custom fields and any metadata added by plugins.
  • Information on the attachments you’ve uploaded to your site, including the name of the file, and metadata such as alt text and a description, and information on which pages or posts art might be attached to.
  • Your settings, including overall site settings such as the site title and description, and more specific settings for your plugins and theme.
  • User data and settings.

There are twelve tables in the WordPress database, which you can see in this image from the WordPress Codex.

the WordPress database

Most of the tables are linked to each other, with wp_posts being the most important one:

  • wp_posts is where the content of your posts and pages is stored. Information on attachments is also stored in this table, as well as navigation menus and revisions.
  • wp_postmeta (linked to wp_posts) stores metadata about posts and pages.
  • wp_comments (linked to wp_users) stores all of the comments on your posts including who they were posted by and what point they were added to.
  • wp_commentmeta (linked to wp_comments) stores metadata about comments.
  • wp_terms (linked to wp_term_taxonomy) stores all the taxonomy terms in your site, including your categories and tags.
  • wp_term_taxonomy (linked to wp_terms and wp_term_relationships) stores the taxonomy for each term in the wp_terms table.
  • wp_term_relationships (linked to wp_terms_taxonomy and wp_posts) links your posts to the taxonomy terms that have been assigned to them.
  • wp_termmeta (linked to wp_terms) stores metadata on your taxonomy terms.
  • wp_users (linked to wp_posts and wp_comments) stores a list of all the users on your site and their username, password and other data.
  • wp_usermeta (linked to wp_users) stores metadata on your users.

The relationships between the tables link all of the data and ensures that for a given post, WordPress knows what taxonomy terms it has, who wrote it, what comments it has and more.

Two tables aren’t linked to the others and don’t have a relationship to your posts:

  • wp_options stores global options and settings for the whole site (not for a post, which would be stored in wp_postmeta).
  • wp_links is a hangover to the early days of blogging and stores the contents of the blogroll, a list of links. It’s rarely used these days.

So your posts and pages are stored in wp_posts but metadata about them is stored in other tables which are linked to them.

But how does WordPress translate all this data into pages on your site?

How WordPress Fetches Content for Your Pages

WordPress uses a specific piece of code called The Loop to fetch database content and output it in the pages of your site. This applies for single pages, archive pages, the home page and search results.

Watch out this short video to get a complete introduction to the loop. 

 

The loop starts with this code:

This checks if there are any posts to be output on this page then while there are posts, uses the_post() template tag so that the posts’ content can be output.

Note that here, post doesn’t mean a blog post. A post in this context can mean any content type including a post, page, attachment or post of a custom post type. It can get confusing!

It ends with this code:

This ends the while loop from the beginning, meaning WordPress stops looping through posts when it’s found everything it needs for this page in the site. It then closes the if check and stops checking if there are any posts.

In between those two lines will be the code that outputs content. I’ll show you different versions of this for different content types shortly.

WordPress knows what content is relevant for the current page based on the kind of page that’s being visited. So for an archive page it will know what kind of archive page it is (a category archive, for example), and for a single post or page it will know which post or page is being output. It will use the correct template file from the theme (using the theme hierarchy) to output the relevant loop for the current content type.

Let’s take a look at this in action.

Single Posts and Pages

The loop for single posts and pages will only be run once, because only the content for one post or page needs to be shown. WordPress knows this because it knows what kind of page it’s showing in your site and will only fetch the current post or page.

The loop for a single post will look something like this:

This is a simplified version of the loop. In most themes it will also include more metadata, a list of categories, and comments. But it shows you how the core content of the post or page is output.

Everything is enclosed in an article element with an ID of the post ID and classes that are set using the post_class() template tag. There’s then a h1 element with the post title and a section for the content using the the_content() template tag.

All this is done automatically by accessing the database, meaning there’s only one file for all your single posts—the template file.

A loop for a page will look very similar and is less likely to have metadata and comments, but that will vary according to your theme.

Archive Pages

Archive pages also use the loop but instead of outputting just one post, they will fetch multiple posts and display them all. So for example a category archive will output all posts in that category.

Again, WordPress knows to do this based on the page that’s being displayed. This is where the while() part of the loop is more obvious because WordPress will keep going through the loop again and again while there are posts to display.

Here’s an example loop for a category archive:

There are a few key differences here compared to the single post or page:

  • The title is inside an h3 tag, not h2. This is because the title of the archive page will be in a h2 tag.
  • The title is inside a link to the post using the_permalink(). This means that a visitor can click through to the individual post from the archive page.
  • The featured image is being output as part of the archive page design. This is optional but will encourage more clicks through to the post. It’s also enclosed in a link to the post.
  • An excerpt of the post is output instead of the entire content. In some blogs, the entire content is output on archive pages but if you just use an excerpt, it will mean less scrolling for people to see all your posts in that archive.

There are differences, but as you can see, archives are using the loop just like single pages and posts—and they’re using it to access the database, fetch the content saved in the wp_posts table and output that.

The Home Page

The way the home page is displayed will depend on how you’ve set it up. Your home page can either be a list of your most recent posts, or it can be a static page. You can configure this in the Settings menu in the WordPress admin.

If your home page is a static page, then it will normally use the same template file as the other static pages in your site and the same version of the loop. This is unless your theme has a specific template file for the home page. Sometimes a template file like this will be used to fetch he contents of the page and output extra content such as a slider which is only on the home page. Either way, the loop will be like the loop we saw above for single pages.

If your home page is an archive of your latest posts, it won’t use the archive.php template file (annoyingly), but instead will use a template file specifically for the home page or, if there isn’t one, it will use the index.php file, which is the catch-all template file for any pages that don’t have a more specific template file.

It’ll work through the loop again and again, fetching and outputting probably the most recent posts in your site and showing either the excerpt or the full content. This depends on the code in your loop.

So whichever way your home page is set up, it’ll still be using the loop to access the database and fetch either the single stain page or the list of recent posts.

Summary

WordPress’s power comes from the fact that it’s a content management system. This means that to create a website, you don’t have to write any code. Instead, you create posts and pages in the WordPress admin screens and WordPress saves those to the database so they can be output on the relevant pages of your site so people can visit them.

This makes it much easier for you to create and manage your site and for the site to grow to include as much content as you need. Be sure to check out our learning guide if you want to learn more WordPress development.

The Best WordPress Themes and Plugins on Envato Market

Explore thousands of the best WordPress themes ever created on ThemeForest and leading WordPress plugins on CodeCanyon. Purchase these high-quality WordPress themes and plugins and improve your website experience for you and your visitors. 

Here are a few of the best-selling and up-and-coming WordPress themes and plugins available for 2020.


No comments:

Post a Comment