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 WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Single Page

Why are we not using the WP_Query object

I think I'm missing something crucial here, so let me ask what is probably a dumb question:

In the portfolio-single page we are using 'have-posts()' to retrieve the post details, and it obviously seems to work. Why do we not need to use '$query->have-posts();' in this instance?

Anthony Attard
Anthony Attard
43,915 Points

Great question. Was wondering about this as well.

1 Answer

Since you are already in the portfolio-single.php template, there's no need to create an new instance of the WP_Query class - the main WordPress loop suffices. Single portfolio posts are already the objects that are being queried.

Most use cases for creating a new WP_Query class is for a secondary loop.

Hope this helps!

Brandon Mulholand
Brandon Mulholand
2,115 Points

To any onlookers: This answer is correct. However, there's a dirtier way of thinking about it. If you can go into the CMS side of WordPress and edit the page's content, then that content is going to be displayed within the normal WordPress loop. If you need data or content that wouldn't be available by simply going to the page and changing the page's content via the CMS, then you'll probably need a custom WP_Query.