Let’s run the slower approach and test what happens. We want to see how many times the database is hit as well as what SQL queries are processed. In this episode, you’ll explore how the terms, taxonomy, and posts are related by looking at the database tables and their schema. Then you’ll discover the SQL queries when running get_terms(). This episode is Part 1 of a two-part testing exercise. There’s much to cover and learn as you deep dive into database tables, relational information, SQL queries, get_terms(), and WP_Query.
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
Slow Approach Overview
In this episode, you’ll walk through the slow approach’s code, which can be found here. Let’s make sure you understand what’s happening at a macro level before we run the tests on it.
This hands-on lab is more advanced. It will teach you about more advanced SQL commands, such as JOIN and GROUP BY. You’ll also experiment with $wpdb, which is WordPress’ MySQL Communication wrapper. In this episode, let’s get you ready to start this lab. You’ll learn about the objectives and what you’ll be doing, as well as getting your sandbox ready to go. To get started, you are going to want to install this lab’s companion plugin into your Sandbox project. This plugin is freely available on GitLab. Just follow the instructions in the video (or the README.md) to install. Next, […]
SQL to Update Custom Field
Up until now, you’ve been getting data out of the database. It’s time to update data in the database. You’ll learn about how to update custom fields in the wp_postmeta database table both by writing the native SQL and using update function from WordPress Core. First, let’s write the SQL to change the custom field’s value in the database. You’ll learn about the UPDATE keyword in SQL. Then let’s update the subtitle using update_post_meta(). When you run the following code, the subtitle changes and then renders out to the browser. You’ll look at the SQL queries WordPress generates via the […]
SQL to Get a Custom Field
Let’s talk about post metadata. WordPress stores metadata for post’s content into its wp_postmeta database table. When you need a piece of metadata that does not already exists, then you create a new custom field. Therefore, a custom field is nothing more than a post metadata that you specify. That’s it. Hint: You’ll learn how WordPress optimizes SQL queries to speed up your webpage loading. In this episode, you’ll look at the sequence of the SQL queries that WordPress does when loading a page: grabs the post, then terms, and then all of the post metadata. The metadata is then […]
Exploring WordPress Queries with Query Monitor
Let’s explore different WordPress queries using the Query Monitor plugin. This plugin lets you see the actual SQL queries. You’ll see the query for fetching the post, thumbnail, user, and more.
In the last episode, you created a new custom database table. In this episode, you will see where WordPress Core defines the schema for all of its predefined database tables. You will find the schema in wp-admin/includes/schema.php. Let’s take a look at it. Let’s talk about a use case for you. Let’s say you are creating a plugin that needs a custom database table. When do you load the schema?
Create a New Database Table
Let’s create a new database table by writing a native SQL query in phpMyAdmin or Sequel Pro. The intent of this episode is (1) to give you an appreciation of the schema that defines each database table and (2) to prepare you the next episode about how WordPress creates each of the database tables. You are actively writing the schema that will define a new custom database table. Don’t worry. Tonya walks you through each step and explains it. She also gives you insights and her thought processes. You’ll learn about specifying default values, primary keys, and foreign keys.
Defining Table Structure (Schema)
Let’s look at the database table structure in WordPress. The technical term is schema. You’ll learn about SQL data types, lengths, options, and more. Programming languages let you specify the different types of data. Many languages let you specify different types of the same data. For example, you can specify a number as integer, short integer, long integer, floating, decimal and more. Why? Why would you want to take the time to specify a specific category of the number data type? Memory. A short integer requires less memory than a long integer. You get specific to be efficient with memory […]
Table Alias with AS
In SQL, you can specify an alias for database tables using the keyword AS. In this episode, you will use this keyword and see why you want to use it.