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.

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.

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

85 Stunning Divi WooCommerce Templates for Your Store

85 Stunning Divi WooCommerce Templates for Your Store

What type of Divi WooCommerce templates are you looking for? A Divi WooCommerce shop page layout, product page layout, cart layout, or everything? Whether you're behind one or all, you can always find layouts to fulfill the need to enhance a Divi WooCommerce store....

Introducing the Marvelous Gravity Forms Styler for Divi

Introducing the Marvelous Gravity Forms Styler for Divi

To style and insert Gravity Forms in Divi isn't a simple task. It's a long, step-driven, and complex procedure. You have to deal with Gravity Forms functions, then Divi's, and finally use codes to make Gravity Forms look good. For a non-techie user, it's a nightmare....

How to Add the Instagram Feed to Your Divi Website

How to Add the Instagram Feed to Your Divi Website

Does your client want to add an Instagram Feed to Divi? Or is it only you who wants to improve the website’s engagement by setting up a live Instagram Feed on the website? In either case, it’s a smart move to leverage Instagram’s influence on your customers or...

Popular on Divi Extended

Divi Plus WooCommerce Extended Divi Child Theme Divi Header Layouts