Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Busting The Loop Lies

Busting The Loop Lies

“The Loop” lies to us all, don’t let it trick you. An intervention with the loop is past due. Lets walk through mastering its evil ways together. What is the best way to loop? Should you use get_posts, WP_Query or query_posts? Why does pagination break? How do you loop at all and what is the loop? How can you optimize the loop and decrease load time? Get the most out of the loop! In this talk we will master the backbone of WordPress. Together and answer these important and overlooked questions in turn.

Schedule:
http://2013.birmingham.wordcamp.org/session/busting-the-loop-lies/

Links:
http://wordpress.tv/2013/03/15/andrew-nacin-wp_query-wordpress-in-depth/
http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts
http://codex.wordpress.org/Function_Reference/WP_Query
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Kevin Dees

August 24, 2013
Tweet

More Decks by Kevin Dees

Other Decks in Programming

Transcript

  1. <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();

    ?> <h1><?php the_title(); ?></h1> <?php the_content();?> <?php endwhile; ?> <?php endif; ?> Sunday, August 25, 13
  2. <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();

    ?> <h1><?php the_title(); ?></h1> <?php the_content();?> <?php endwhile; ?> <?php endif; ?> Sunday, August 25, 13
  3. The Main Loop // wp-settings.php around line 220 $wp_the_query =

    new WP_Query(); $wp_query = $wp_the_query; Sunday, August 25, 13
  4. Everything else // includes folder post.php around line 1627 function

    get_posts($args = null) { // lots of code $get_posts = new WP_Query; return $get_posts->query($r); } Sunday, August 25, 13
  5. <?php query_posts( array( 'posts_per_page' => 5 ); while ( have_posts()

    ) : the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; wp_reset_query(); ?> Sunday, August 25, 13
  6. <?php $theposts = get_posts( $args ); foreach($theposts as $post) :

    setup_postdata($post); ?> <h2> <?php the_title(); ?> </h2> <?php endforeach; wp_reset_postdata(); ?> Sunday, August 25, 13
  7. function get_last_five() { $last_five = new WP_Query( array( 'post_type' =>

    'any', 'posts_per_page' => 5 )); if ( $last_five->have_posts() ) { while ( $last_five->have_posts() ) { $last_five->the_post(); $title = get_the_title(); echo "<p>{$title}</p>"; } } wp_reset_postdata(); } Sunday, August 25, 13
  8. <?php add_action( 'pre_get_posts', 'demo_main_loop' ); function demo_main_loop($query) { if (

    $query->is_main_query() && !is_admin() ) : // do stuff endif; return; } ?> Sunday, August 25, 13
  9. // do stuff if ( is_home() ) { $query->set('post_type', 'any');

    $query->set('posts_per_page', 3); } Sunday, August 25, 13
  10. // do stuff $meta_args = array( array( 'key' => 'demo_on_home_page',

    'value' => 1 ) ); if ( is_home() ) { $query->set('post_type', 'any'); $query->set('meta_query', $meta_args); $query->set('posts_per_page', 3); } Sunday, August 25, 13
  11. 1 : the posts 2 : number of posts 3

    : meta data 4 : taxonomy data Sunday, August 25, 13
  12. <?php $numq = 0; add_filter('query', 'numQ'); function numQ($q) { global

    $numq; $numq++; return $q; } echo $numq; ?> Sunday, August 25, 13
  13. global $numq; $last_five = new WP_Query( array( 'post_type' => 'any',

    'posts_per_page' => 2, 'no_found_rows' => true, // rows 'update_post_meta_cache' => false, // meta 'update_post_term_cache' => false // tax )); // the loop echo “<h1>Total Queries: {$numq}</h1>”; Sunday, August 25, 13
  14. if ( $last_five->have_posts() ) { echo "<h3>Last Posted Items</h3><ul>"; while

    ( $last_five->have_posts() ) { $last_five->the_post(); $title = get_the_title(); $link = get_permalink(); echo "<li><a href='{$link}'>{$title}</a></li>"; } echo "</ul>"; } Sunday, August 25, 13