Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialanthonyvigliano
1,667 PointsDifference between $query and $the_query
What's the difference between the 2?
3 Answers
Kevin Korte
28,149 Pointsthe arguments passed in.
wordpress doesn't really care what the variable name is. The variable name just holds an array of arguments, and than you can pass that variable into the WP_Query class, and it will use those arguments when it builds the loop.
You as the developer can choose your variable name that stores the array of arguments.
Patrick O'Dacre
15,471 PointsThere is no difference. Both are variables and the names you give those variables are a matter of personal style.
That being said, in the WordPress dev community, those are the typical names given to the variables used to hold a new WP_Query object.
Personally, I usually name the variable according to the query I'm performing. For example, if I'm querying a custom post type called 'services', then I will name my variable $services_query.
Here's a new WP_Query object being created to hold all posts in the services custom post type:
$services_query = new WP_Query(
array(
'post_type' => 'services'
)
);
Many choose to hold their arguments in an $args variable first:
$args = array(
'post_type' => 'services'
);
$services_query = new WP_Query( $args );
Those two code blocks are identical. Personally, I prefer the first. I first it more readable.
Once you have your new object, you can use it to create a custom loop to actually print out the data associated with each post you've grabbed with your new WP_Query object.
All of this is found in the codex here - https://codex.wordpress.org/Class_Reference/WP_Query
You'll see in their examples, a new WP_Query object is held in variables like $the_query, $query1, etc. It doesn't matter what you name your variable, to the extent that it's readable and clearly conveys what is being held in that variable.
I should note that WP_Query is class used to create objects. This is Object Oriented Programming in php and an understanding of OOP will help you understand WP_Query a lot better.
I hope that helps.
anthonyvigliano
1,667 PointsThank you very much for your response as well. It does help & I do understand your preference as well. I had completed the Bootstrap to WordPress course & to be honest probably should've spent more time learning PHP, before coming here.
Patrick O'Dacre
15,471 PointsIn my experience it's hard to find the ideal starting point. I found myself going back and forth between other php frameworks, WordPress and more. Each time I get a deeper understanding. You may find the same thing.
You don't have to understand WP_Query deeply, or other aspects WordPress for that matter, before you can use it effectively. Don't wait to get a so-called 'expert understanding' before diving in and using this stuff; just make sure your understanding deepens over time.
So I disagree that you should wait ;) In my humble opinion, the path to expertise isn't linear.
anthonyvigliano
1,667 Pointsanthonyvigliano
1,667 PointsThank you so much for the quick response! I thought it was more about a naming convention used by WP, since I tried both & they worked. I guess it's more of the developers preference then to keep everything standard.