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 trialMathieu Delporte
8,097 PointsThe loop vs query posts
I was watching this video and noticed that I've been using a different approach to displaying content in a portfolio page. I tend to use query_posts instead of the loop. Is this a bad thing?
What I did see in the video is the ability to go through different projects using next and previous arrows, which don't seem to work when I use query_posts.
Also, I query the child pages of my main portfolio page, where in this video he uses custom post types, what is the best approach?
4 Answers
Marin Marusic
3,497 PointsWhy not use query_posts function
It's a very bad thing. query_posts was a brilliant idea, but it was implemented wrong, and it was only about 2 years ago that anybody noticed the mistake. Instead of query_posts for filtering use pre_get_posts. It's a hook, that's called before any query to the database is made, but after the query is already in memory. So perfect time to modify it right? :)
I can't emphasize this enough (well I can) QUERY_POSTS CONSIDERED HARMFUL ( I programmed with Basic, I can quote Djikstra), really, it doesn't recreate the main query object and replaces the main loop with another loop that isn't a main loop any more.
So if you really need query_posts, use WP_Query, syntax is almost identical, and it won't do the mess with the loop query_posts does. WP_Query doesn't rewrite the global query object after all. It instantiates a new one.
So as for your question, I'd go about it like this, I'd use get_posts for getting the main posts you want to get. then, I'd use pre_get_post hook to get me the child pages too, package it in one big object and that's it.
I hope this helps.
P.S. I just found the post where I learned of this, (great read)[http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/] ... Check it out, you can probably get your answer there, if my ramble wasn't enough.
Marin Marusic
3,497 PointsYep, m8, it's ok.
Just remember to add wp_reset_postdata() at the end of the loop. It clears out the query in memory.... So if you need another query, it doesn't get messed up.
Mathieu Delporte
8,097 PointsHi Marin,
Thanks for your reply! I did some research about query_posts when I first started and saw that this is pretty harmful indeed. After watching the video from Zac again I adapted my code to the one below, which seems to work OK on my site now, can you tell me if this is better?
<!--- MAIN LOOP -->
<?php
$args = array(
'showposts' => 20,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'page',
'post_parent' => 9
);
$the_query = new WP_Query( $args );
?>
<!-- the loop -->
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="project portfolio">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="project-hover">
<h4><?php the_title(); ?></h4>
<p><?php the_field('project-ondertitel'); ?></p>
</div>
</a>
</div>
<?php endwhile; endif; ?>
<!-- end of the loop -->
<!--- /MAIN LOOP -->
Mathieu Delporte
8,097 PointsThanks alot, it was a very big help!