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 trialbeatleboy
539 PointsWhy 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?
1 Answer
Jacob Arriola
12,805 PointsSince 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
2,115 PointsTo 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.
Anthony Attard
43,915 PointsAnthony Attard
43,915 PointsGreat question. Was wondering about this as well.