Groupby Lab Tester Plugin GitLab
Know the Code Hands-on Advanced Lab - demonstrating approaches to get posts grouped by their terms. This is the companion plugin for this hands-on lab. You can find the lab at https://knowthecode.io/labs/get-posts-by-terms
by hellofromTonya · gitlab.com/knowthecode/groupby-lab-tester · website
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://gitlab.com/knowthecode/groupby-lab-tester/-/archive/master/groupby-lab-tester-master.zipReadme
Groupby Lab Tester
This plugin tests two different approaches for getting an array of posts that are grouped by each term. This plugin is a companion to the Know the Code Hands-on Advanced Lab, Get Posts Grouped by Terms. In this lab, you will learn how to use this plugin as well as a deep dive into more advanced SQL Queries and $wpdb (i.e. WordPress' MySQL Wrapper).
Approaches
- Grab the terms first using
get_terms()and then loop through each term and query the database to grab the associated posts. - Build a more advanced SQL Query that performs the task in one database hit.
First Approach
This first approach is found when doing a Google search. It involves fetching all of the terms first. Then iterating through those terms one-by-one. During the loop, this approach uses WP_Query to get the posts that are associated with each term.
This approach works fine; however, it's less performant. Why? It requires additional database hits. Remember, the database is a bottleneck on the server side. You want to optimize your trips to the database.
Second Approach
The second approach uses $wpdb with a custom SQL query. This query joins the multiple tables together and then groups them. It allows us to fetch the records in one database hit....just ONE trip to the database. That's much faster.
We use this approach on Know the Code, as part of our Help Center.
Lab Dependencies
For this lab, you will need to install the following plugins. You should have both of these installed on all of your projects. These two plugins are for development only and not to be deployed to a live production site.
- UpDevTools - A suite of Developer tools including Kint, Whoops, and more.
- [Query Monitor]() - We use Query Monitor to analyze the queries.
You will need a sandbox.dev site spun up on your local machine using DesktopServer, VVV, or your favorite web appliance. Any theme will do.
Installation
Make sure that you have a sandbox site setup. We have a Sandbox repo ready for you on [GitLab](). It has everything you need, including dummy content. Use this sandbox to work along with Tonya.
- In terminal, navigate to
{path to your sandbox project}/wp-content/plugins. - Then type in terminal:
git clone git@gitlab.com:knowthecode/Groupby-Lab-Tester.git - Log into your Sandbox site.
- Go to Plugins and activate the plugin.