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 trial

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a Portfolio Landing Page

Jan BM
Jan BM
3,795 Points

what's the difference of "$the_query->have_posts()" and "have_posts()" ? this is without "$the_query->"

Sorry im not that good in php

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

It's okay to be confused by this, it is confusing. They both inherently do the same thing.

Let's start with the plain old has_posts(). This is part of the default loop. The default loop basically makes some assumptions for you. Those assumptions are that you are looping for the default post type that comes with wordpress, with all of wordpress's default parameters for this loop.

In many cases, this is fine to use. You can read more about the default loop here: http://codex.wordpress.org/The_Loop

Now, the $the_query->have_posts() is doing basically the same thing. It's getting posts. However, this method of getting posts is actually using a class, that is part of wordpress's core. It's using the WP_Query class. Basically this gives us a ton of options we can filter our loop by. If you wanted to loop for a custom post type, this is the way you'd do it. There are probably over 100 different parameters that can be passed into this loop.

The $the_query part is actually a variable we set before we loop. And that variable is equal to a new WP_Query class with arguments passed into it. A real life example, I was using a calendar plugin, and that plugin created a custom post type of calendar to store each calendar event. I looked at the database to see how it was storing data, and I wanted to be able to show all of the upcoming events for the calendar on the home page. I wrote a custom WP_Query loop that returned only posts with the post type of calendar, it looked something like"

<?php
$calendar_args = array(
'post_type' => 'calendar'
);
$calendar = new WP_Query( $calendar_args );

if( $calendar->have_posts() ) : while( $calendar->have_posts() ) : $calendar->the_posts();

//now my loop begins

?>

<?php endwhile; else : ?>

<?php endif; ?>

And something like that would return only my calendar events. Here is all of the options for the WP Query class: http://codex.wordpress.org/Class_Reference/WP_Query

Jan BM
Jan BM
3,795 Points

thanks, but from the video it looks like this: if(have_posts() ) : while( $calendar->have_posts() ) : $calendar->the_posts();

there was no $calendar initiated from the first have_posts().

does it make any difference by doing this instead? if( $calendar->have_posts() ) : while( $calendar->have_posts() ) : $calendar->the_posts();

Mohsen Qaddoura
Mohsen Qaddoura
22,237 Points

Thank you very much Kevin Korte, your answers are always spot on , thorough, with links and examples. Quite helpful. Thank you once more!