Lab Introduction
Lab: Advanced SQL – Get Posts Grouped by Terms
Video Runtime: 14:27
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, you want to put this snippet of code into the get_results
method. You’ll use it to record the number of times the database is hit (meaning a request is made to it).
<?php | |
/** | |
* Add the following testing code into the `wp-includes/wp-db.php` file | |
* This snippet of code will trigger our plugin's recorder. | |
*/ | |
public function get_results( $query = null, $output = OBJECT ) { | |
// TESTING - TONYA | |
if ( function_exists( 'UpTechLabs\KnowTheCode\GroupbyLab\record_database_hit' ) ) { | |
\UpTechLabs\KnowTheCode\GroupbyLab\record_database_hit( $query ); | |
} | |
// TESTING-END - TONYA |
Code Challenge
Want to challenge yourself before starting this lab? See if you can write a native SQL query that grabs the posts grouped by each category. Load into your MySQL database interface, such as Sequel Pro or phpmyAdmin.
Don’t repeat yourself. Don’t repeat yourself. Don’t repeat yourself.
Episodes
Total Lab Runtime: 02:46:48
- 1 Lab Introductionfree 14:27
- 2 Slow Approach Overviewpro 12:11
- 3 Test Slow Approach - Part 1pro 17:27
- 4 Test Slow Approach - Part 2pro 21:07
- 5 Relational Typespro 23:20
- 6 Types of Joinspro 09:30
- 7 Joining Metadatapro 12:37
- 8 Joining Posts to Termspro 20:32
- 9 Grouping Datasetspro 08:32
- 10 WordPress SQL Handler $wpdbpro 11:01
- 11 Deeper Dive into $wpdb->get_resultspro 05:54
- 12 Run Fast Approach & Compare Resultspro 10:10