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 *

0 Comments

Submit a Comment

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

How to add Google reCAPTCHA in Contact Form 7 in easy steps

How to add Google reCAPTCHA in Contact Form 7 in easy steps

It’s a must that a contact form should have Google reCAPTCHA or any other security options enabled on it. Well, reCAPTCHA is most recommended. It protects your contact forms from getting filled out by bots or spams. And if you want to add reCAPTCHA on your contact...

Divi Content Toggle to display key information in compact manner

Divi Content Toggle to display key information in compact manner

Having long pages with detailed information can delight Google bots, however, for users, it can be a bulky experience. They might find themselves overwhelmed or either confused when looking over pages overtaken by content. Then there comes the Divi Content Toggle...

Create Divi structured data pages easily for Google rich snippets

Create Divi structured data pages easily for Google rich snippets

Having Divi structured data markup pages can help you and your website in many ways. It not only features your web pages as Google rich snippets. But it brings a valuable amount of organic traffic. And it can be done for any website of any CMS, such as WordPress,...

Divi 4 header footer pack

Fill up your details to download the file

Please check your email for the download link.

One Plugin, Many Possibilities

You have Successfully Subscribed!