Nov 20, 2019 | WordPress

Difference between wp_query & wp_the_query

Let’s start with the difference between global and $GLOBALS to understand the difference between $wp_query & $wp_the_query.

Global is a keyword which is used to creates global variable. Global variables can be imported into local scope for local scope.

$GLOBALS is a superglobal array that is accessible everywhere without the global keyword, but the significant difference is that it only grants access to that variable we can’t modify it.

In the context of WordPress(reference – wp-settings.php):
$GLOBALS[‘wp_the_query’] = new WP_Query();
$GLOBALS[‘wp_query’] = $GLOBALS[‘wp_the_query’];

$wp_query is the variable that contains the copy of GLOBALS[‘wp_the_query’]. Plugins and themes could modify $wp_query.

$wp_the_query is the variable that contains an original copy of GLOBALS[‘wp_the_query’]. This is used to reset the $wp_query object when we call wp_reset_query() function.

To be conclusive, If you would like to write competent & reliable code, which rarely fails, you should be using $GLOBALS[‘wp_the_query’]. On the other hand, if you believe other plugins and themes are always practicing the correct methodology of programming, you could use $GLOBALS[‘wp_query’].

Posted By:
Mohd Arif
Dreamer, Schemer, Music & Technology enthusiast. He enjoys building things that speak HTTP. Arif is Development Head at Elicus and has over 8 years experience in the WordPress development industry.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Build Divi Shop Amazingly With Divi Cart Pro in 2021 & Beyond

Build Divi Shop Amazingly With Divi Cart Pro in 2021 & Beyond

Building a Divi shop is amazing when you’ve got the right tool. And you don’t have to put extra work to make everything right for your customers as well as you —  the owner. You can use the Divi and WooCommerce to lay out the structure for your Divi store, but...

Fix WordPress Site Stuck in Maintenance Mode (3 Easy Ways)

Fix WordPress Site Stuck in Maintenance Mode (3 Easy Ways)

Is your WordPress site stuck in maintenance mode, and you aren’t able to find any way out? Then, in this tutorial, we’ll look into the issue and help you unstuck your website from maintenance mode. There will be no heavy lifting or anything hardcode related that might...

7 Tips & a Complete Guide to Design Perfect Website Header

7 Tips & a Complete Guide to Design Perfect Website Header

From recognizing a brand to navigate the website with ease, a visitor’s journey starts from the website header. And it continues up to an indefinite time whenever he visits the site -- to either explore, get the services, or read the blog and whatnot. The header of a...

Popular on Divi Extended

Divi Plus Divi Child Theme Divi Header Layouts