Most of my plugins including Contextual Related Posts and Top 10 run SQL queries in order to fetch information from the WordPress database. e.g. Contextual Related Posts pulls the related posts via an SQL query and Top 10 pulls the popular posts.

I usually tell many users who request support in the forums to install Query Monitor and check what the SQL generated by the plugin in. It also helps to run the queries in phpMyAdmin or a similar software to see what the outputs are.

In this post, I’ll explain how you can use Query Monitor to find the SQL that is generated by Contextual Related Posts.

Step 1: Install Query Monitor

If you don’t have Query Monitor installed, you can navigate to Plugins > Add New and search for Query Monitor. It should be the first plugin in the list. Hit Install and then Activate or Network Activate (if you’re using Multisite).

Once you install Query Monitor, you will notice a new menu item added to the admin bar at the top of your page. This item has several menu items as you can see in the top right portion of the above screenshot.

Step 2: Locate the Query

Navigate the post that you have noticed the problem on and then hit the new menu item and then select Queries. This will split your screen horizontally (or vertically) and you’ll see a set of menus to the left along with a complete set of queries.

Next select Queries by Component. This screen gives you the list of queries based on the “component” that generated them. These will be plugins, the theme and the WordPress Core.

Note: Turn off the in-built cache in the settings page of the respective plugin before you proceed. These queries are not generated if the cache is on.

Contextual Related Posts query

In the above screen, you can select Plugin: contextual-related-posts to find the queries generated by Contextual Related Posts. In order to find the main query, select WP_Query->get_posts in the Caller dropdown as you can see in the below screenshot.

Better Search query

The query for Better Search will change depending on if you have Seamless Mode on or off. If you have Seamless Mode on, then the plugin directly taps into the WordPress Core to find the search results. In that case, you will first need to select the Core Component. Note that we need to look at the Main Query as

If you have Seamless mode off, the query will be under the Better Search and the query will be as per the below screenshot. You will also have a Main Query which continues to be generated by WordPress Core.

Top 10 query

Similar to Contextual Related Posts, the query for Top 10 can be found under the Component as per the screenshot below.