Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Getting Into The Loop

The magic behind WordPress is how it processes queries and gives you the results via “The Loop.” The Loop can be hijacked for a variety of custom uses and these techniques can quickly expand the capacity of WordPress to present information in different ways. We’ll cover this, as well as PHP tips which help introspect WordPress during lower-level development.

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

Getting Into The Loop

  1. 1. Getting Into The Loop mitcho (Michael Erlewine) http://mitcho.com January 23, 2010 WordCamp Boston
  2. 2. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  3. 3. Today NOTE: • Will link to slides and code later on http://mitcho.com/blog/ • Please rate later on SpeakerRate (find link on the blog)
  4. 4. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  5. 5. Introduction
  6. 6. Introduction • Hi, I’m mitcho.
  7. 7. Introduction • Hi, I’m mitcho. • Linguist, coder, teacher.
  8. 8. Introduction • Hi, I’m mitcho. • Linguist, coder, teacher. • At MIT. Previously at Mozilla. Now working with Automattic.
  9. 9. Introduction • Hi, I’m mitcho. • Linguist, coder, teacher. • At MIT. Previously at Mozilla. Now working with Automattic. • Programming PHP/MySQL since 2002?
  10. 10. Introduction • Hi, I’m mitcho. • Linguist, coder, teacher. • At MIT. Previously at Mozilla. Now working with Automattic. • Programming PHP/MySQL since 2002? • Blogging (off and on) since 2007.
  11. 11. Introduction • Hi, I’m mitcho. • Linguist, coder, teacher. • At MIT. Previously at Mozilla. Now working with Automattic. • Programming PHP/MySQL since 2002? • Blogging (off and on) since 2007. • http://mitcho.com; @mitchoyoshitaka
  12. 12. Introduction
  13. 13. Introduction • Yet Another Related Posts Plugin
  14. 14. Introduction • Yet Another Related Posts Plugin • ...aka YARPP
  15. 15. Introduction • Yet Another Related Posts Plugin • ...aka YARPP • As seen on Laughing Squid, ma.tt...
  16. 16. Introduction • Yet Another Related Posts Plugin • ...aka YARPP • As seen on Laughing Squid, ma.tt... • Over 350k downloads
  17. 17. Introduction • Yet Another Related Posts Plugin • ...aka YARPP • As seen on Laughing Squid, ma.tt... • Over 350k downloads • http://mitcho.com/code/yarpp or search for “YARPP”; @yarpp
  18. 18. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  19. 19. The Loop
  20. 20. The Loop • “The Loop” is the mechanism by which posts are called from the database and then displayed.
  21. 21. The Loop • “The Loop” is the mechanism by which posts are called from the database and then displayed. • On many pages—like the index or archives—it “loops” through each post.
  22. 22. The simplest Loop get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer();
  23. 23. The simplest Loop get_header(); if (have_posts()) : { while (have_posts()) : the_post(); “The Loop” the_content(); endwhile; endif; get_sidebar(); get_footer();
  24. 24. The simplest Loop get_header(); if (have_posts()) : { while (have_posts()) : “The Loop” the_post(); Sets up the post the_content(); endwhile; endif; get_sidebar(); get_footer();
  25. 25. The simplest Loop get_header(); if (have_posts()) : { while (have_posts()) : “The Loop” the_post(); Sets up the post the_content(); endwhile; endif; get_sidebar(); get_footer(); Every theme’s PHP files are built on this basic structure.
  26. 26. The Loop if there are posts while there are posts get the post do stuff with it end while end if
  27. 27. The Loop if there are posts { while there are posts get the post “The Loop” do stuff with it end while end if
  28. 28. The Loop
  29. 29. The Loop • The Loop is where you can use Template Tags.
  30. 30. The Loop • The Loop is where you can use Template Tags. • codex.wordpress.org/ Template_Tags
  31. 31. The Loop • The Loop is where you can use Template Tags. • codex.wordpress.org/ Template_Tags • It’s the the_post() call that makes that possible.
  32. 32. The Loop if there are posts while there are posts get the post do stuff with it end while end if CC BY flickr.com/photos/myklroventine/ 1430113497/
  33. 33. The Loop But mommy, where do posts come from? if there are posts while there are posts get the post do stuff with it end while end if CC BY flickr.com/photos/myklroventine/ 1430113497/
  34. 34. The Loop But mommy, where do posts come from? if there are posts while there are posts get the post do stuff with it end while end if CC BY flickr.com/photos/myklroventine/ 1430113497/
  35. 35. Every Loop has a query
  36. 36. Every Loop has a query • Regularly, WordPress chooses the right template file and query based on your URL.
  37. 37. Every Loop has a query • Regularly, WordPress chooses the right template file and query based on your URL. • codex.wordpress.org/ Template_Hierarchy
  38. 38. Every Loop has a query • Regularly, WordPress chooses the right template file and query based on your URL. • codex.wordpress.org/ Template_Hierarchy • /archives/123 → single post
  39. 39. Every Loop has a query • Regularly, WordPress chooses the right template file and query based on your URL. • codex.wordpress.org/ Template_Hierarchy • /archives/123 → single post • /archives→ archives
  40. 40. Every Loop has a query • Regularly, WordPress chooses the right template file and query based on your URL. • codex.wordpress.org/ Template_Hierarchy • /archives/123 → single post • /archives→ archives • /tags/chicken→ all chicken articles
  41. 41. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  42. 42. Customizing The Loop
  43. 43. Customizing The Loop • Themes control how information is presented...
  44. 44. Customizing The Loop • Themes control how information is presented... • The query controls what information is presented.
  45. 45. Custom queries
  46. 46. Custom queries Possible applications:
  47. 47. Custom queries Possible applications: • Create custom feeds/displays
  48. 48. Custom queries Possible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/
  49. 49. Custom queries Possible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/ • Pull information on other posts from within the theme’s Loop
  50. 50. Custom queries Possible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/ • Pull information on other posts from within the theme’s Loop • Customize what information is displayed globally
  51. 51. Custom queries Possible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/ • Pull information on other posts Today’s from within the theme’s Loop examples • Customize what information is displayed globally
  52. 52. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  53. 53. Roll your own query
  54. 54. Roll your own query EX:
  55. 55. Roll your own query EX: • Pull information on other posts from within the theme’s Loop
  56. 56. Roll your own query EX: • Pull information on other posts from within the theme’s Loop • Display other posts with a specific criteria, like a tag.
  57. 57. Roll your own query EX: • Pull information on other posts from within the theme’s Loop • Display other posts with a specific criteria, like a tag. • Wrap it up in a shortcode [others]
  58. 58. Roll your own query
  59. 59. Roll your own query The idea:
  60. 60. Roll your own query The idea: • Create a new WP_Query object.
  61. 61. Roll your own query The idea: • Create a new WP_Query object. • Given a $tag...
  62. 62. Roll your own query The idea: • Create a new WP_Query object. • Given a $tag... • new WP_Query(array('tag'=> $tag))
  63. 63. Roll your own query The idea: • Create a new WP_Query object. • Given a $tag... • new WP_Query(array('tag'=> $tag)) • Call it $my_query
  64. 64. Roll your own query The idea: • Create a new WP_Query object. • Given a $tag... • new WP_Query(array('tag'=> $tag)) • Call it $my_query • Create a Loop for $my_query
  65. 65. Roll your own query The idea: • Create a new WP_Query object. • Given a $tag... • new WP_Query(array('tag'=> $tag)) • Call it $my_query • Create a Loop for $my_query • Do stuff in it
  66. 66. Make your own Loop Make sure the Loop controllers are using $my_query, not the default ($wp_query) if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); the_content(); endwhile; endif;
  67. 67. The result:
  68. 68. DEMO:
  69. 69. More about WP_Query
  70. 70. More about WP_Query Learn more from the Codex:
  71. 71. More about WP_Query Learn more from the Codex: • More information on the parameters:
  72. 72. More about WP_Query Learn more from the Codex: • More information on the parameters: • codex.wordpress.org/ Template_Tags/query_posts
  73. 73. More about WP_Query Learn more from the Codex: • More information on the parameters: • codex.wordpress.org/ Template_Tags/query_posts • Tips and examples:
  74. 74. More about WP_Query Learn more from the Codex: • More information on the parameters: • codex.wordpress.org/ Template_Tags/query_posts • Tips and examples: • codex.wordpress.org/The_Loop
  75. 75. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  76. 76. Filter every query
  77. 77. Filter every query EX:
  78. 78. Filter every query EX: • Customize what information is displayed globally.
  79. 79. Filter every query EX: • Customize what information is displayed globally. • Hide all my tweets.
  80. 80. Filter every query
  81. 81. Filter every query The idea:
  82. 82. Filter every query The idea: • Use the request filter to specify that we don’t want results from the “tweet” category (#10)
  83. 83. The result:
  84. 84. DEMO
  85. 85. Aside: the tools Turn up the fire! • Firefox getfirefox.com • Firebug getfirebug.com • FirePHP firephp.org
  86. 86. Aside: the tools Turn up the fire! • Firefox getfirefox.com • Firebug getfirebug.com • FirePHP firephp.org The “Pyrotrinity”
  87. 87. FirePHP Takes a moment to set up:
  88. 88. FirePHP Takes a moment to set up: Use it like this:
  89. 89. Today • Introduction • Loop basics • Custom queries • Method 1: Roll your own query • Method 2: Filter every query • Next steps
  90. 90. Next steps
  91. 91. Next steps Hit the docs:
  92. 92. Next steps Hit the docs: • PHP manual: php.net
  93. 93. Next steps Hit the docs: • PHP manual: php.net • Codex: codex.wordpress.org
  94. 94. Next steps Hit the docs: • PHP manual: php.net • Codex: codex.wordpress.org • The source: core.trac.wordpress.org
  95. 95. Next steps Hit the docs: • PHP manual: php.net • Codex: codex.wordpress.org • The source: core.trac.wordpress.org • grep it!
  96. 96. Next steps
  97. 97. Next steps Learn SQL
  98. 98. Next steps Learn SQL • The language that WordPress’s raw database calls are in.
  99. 99. Next steps Learn SQL • The language that WordPress’s raw database calls are in. • It’s really not that scary.
  100. 100. Next steps Learn SQL • The language that WordPress’s raw database calls are in. • It’s really not that scary. • Lets you write filters directly on different parts of the query.
  101. 101. Next steps Learn SQL • The language that WordPress’s raw database calls are in. • It’s really not that scary. • Lets you write filters directly on different parts of the query. • EX: mitcho.com/blog/how-to/external- orders-in-wordpress-queries/
  102. 102. Thank you! Questions? Slides will be up on mitcho.com/blog/. See you at the Genius Bar! mitcho (Michael Erlewine) mitcho.com; @mitchoyoshitaka

    Be the first to comment

    Login to see the comments

  • gianni27

    May. 20, 2013
  • mikaelz

    May. 28, 2013
  • denywidiyastanto

    Jun. 15, 2013
  • comunitech

    Jun. 23, 2013
  • niceut

    Jul. 22, 2013
  • cesarinou

    Nov. 15, 2013
  • 730n4rd0

    Aug. 19, 2014
  • mrkimbe

    Oct. 30, 2014
  • rch_rodriguez

    Nov. 7, 2014
  • alfaris88

    Dec. 23, 2014
  • rayrizaldy

    Jan. 18, 2015
  • AmintaShare

    Jul. 28, 2015
  • fedegrafo

    Oct. 19, 2015
  • DemetriosKalcassinas

    Oct. 23, 2015
  • AyazAmin1

    Nov. 13, 2015
  • pcurcio988

    Dec. 13, 2015
  • MostafaAminiNasab

    Dec. 2, 2016
  • palmaone

    Jul. 14, 2017
  • bizmate

    Oct. 2, 2018
  • AhmedEid20

    Sep. 25, 2019

The magic behind WordPress is how it processes queries and gives you the results via “The Loop.” The Loop can be hijacked for a variety of custom uses and these techniques can quickly expand the capacity of WordPress to present information in different ways. We’ll cover this, as well as PHP tips which help introspect WordPress during lower-level development.

Views

Total views

50,143

On Slideshare

0

From embeds

0

Number of embeds

263

Actions

Downloads

602

Shares

0

Comments

0

Likes

50

×