$30 off During Our Annual Pro Sale. View Details »

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. Busting
    The Loop Lies
    Kevin Dees
    Robojuice
    Sunday, August 25, 13

    View Slide

  2. #looplies
    Sunday, August 25, 13

    View Slide

  3. Why do you
    need to know
    “The Loop” Lies?
    Sunday, August 25, 13

    View Slide

  4. The very basics....
    What is “The Loop”
    Sunday, August 25, 13

    View Slide







  5. Sunday, August 25, 13

    View Slide

  6. The very basics....
    Where “The Loop”
    comes from
    Sunday, August 25, 13

    View Slide

  7. “The Loop”Lives
    in Your
    Template Files
    Sunday, August 25, 13

    View Slide

  8. index.php
    archive.php
    404.php
    home.php
    Sunday, August 25, 13

    View Slide

  9. What are
    “The Loop” Lies?
    Sunday, August 25, 13

    View Slide

  10. “The Loop” is hard!
    LIE
    Sunday, August 25, 13

    View Slide







  11. Sunday, August 25, 13

    View Slide

  12. The Main Loop
    vs
    Everything else
    Sunday, August 25, 13

    View Slide

  13. $wp_query
    Sunday, August 25, 13

    View Slide

  14. $wp_the_query
    Sunday, August 25, 13

    View Slide

  15. NEVER USE IT
    Sunday, August 25, 13

    View Slide

  16. A puppy dies
    every time you use it
    Sunday, August 25, 13

    View Slide

  17. The Main Loop
    // wp-settings.php around line 220
    $wp_the_query = new WP_Query();
    $wp_query = $wp_the_query;
    Sunday, August 25, 13

    View Slide

  18. 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

    View Slide

  19. 4 common ways to
    use “The Loop”
    Sunday, August 25, 13

    View Slide

  20. 4 common lies
    about them
    Sunday, August 25, 13

    View Slide

  21. query_posts is great
    LIE
    Sunday, August 25, 13

    View Slide

  22. It is used to edit the
    main loop
    Sunday, August 25, 13

    View Slide

  23. query_posts( array( 'posts_per_page' => 5 );
    while ( have_posts() ) : the_post();
    echo '';
    the_title();
    echo '';
    endwhile;
    wp_reset_query();
    ?>
    Sunday, August 25, 13

    View Slide

  24. NEVER USE IT
    Sunday, August 25, 13

    View Slide

  25. You run the main
    query twice
    Sunday, August 25, 13

    View Slide

  26. You break
    pagination
    Sunday, August 25, 13

    View Slide

  27. A puppy dies
    every time you use it
    Sunday, August 25, 13

    View Slide

  28. get_posts uses
    foreach so don’t
    touch it
    LIE
    Sunday, August 25, 13

    View Slide

  29. $theposts = get_posts( $args );
    foreach($theposts as $post) :
    setup_postdata($post);
    ?>



    endforeach;
    wp_reset_postdata();
    ?>
    Sunday, August 25, 13

    View Slide

  30. WP_Query is too
    verbose to use
    LIE
    Sunday, August 25, 13

    View Slide

  31. 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 "{$title}";
    }
    }
    wp_reset_postdata();
    }
    Sunday, August 25, 13

    View Slide

  32. pre_get_posts is
    confusing
    LIE
    Sunday, August 25, 13

    View Slide

  33. 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

    View Slide

  34. // do stuff
    if ( is_home() ) {
    $query->set('post_type', 'any');
    $query->set('posts_per_page', 3);
    }
    Sunday, August 25, 13

    View Slide

  35. “The Loop” lacks
    powerful selection
    LIE
    Sunday, August 25, 13

    View Slide

  36. Sunday, August 25, 13

    View Slide

  37. // 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

    View Slide

  38. “The Loop” only runs
    1 query
    LIE
    Sunday, August 25, 13

    View Slide

  39. 4 queries
    are run
    Sunday, August 25, 13

    View Slide

  40. 1 : the posts
    2 : number of posts
    3 : meta data
    4 : taxonomy data
    Sunday, August 25, 13

    View Slide

  41. $numq = 0;
    add_filter('query', 'numQ');
    function numQ($q) {
    global $numq;
    $numq++;
    return $q;
    }
    echo $numq;
    ?>
    Sunday, August 25, 13

    View Slide

  42. Can you have just
    1 query?
    Sunday, August 25, 13

    View Slide

  43. 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 “Total Queries: {$numq}”;
    Sunday, August 25, 13

    View Slide

  44. if ( $last_five->have_posts() ) {
    echo "Last Posted Items";
    while ( $last_five->have_posts() ) {
    $last_five->the_post();
    $title = get_the_title();
    $link = get_permalink();
    echo "{$title}";
    }
    echo "";
    }
    Sunday, August 25, 13

    View Slide

  45. Thank You
    Sunday, August 25, 13

    View Slide