Gettingintotheloop 100123225021-phpapp01

1,734 views

Published on

my personal

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Gettingintotheloop 100123225021-phpapp01

  1. 1. Getting Into The Loopmitcho (Michael 芳貴 Erlewine)http://mitcho.comJanuary 23, 2010WordCamp Boston
  2. 2. Today• Introduction• Loop basics• Custom queries • Method 1: Roll your own query • Method 2: Filter every query• Next steps
  3. 3. TodayNOTE: • 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 thisbasic 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 ifCC 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 ifCC 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 ifCC 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 TheLoop
  43. 43. Customizing TheLoop• Themes control how information is presented...
  44. 44. Customizing TheLoop• Themes control how information is presented...• The query controls what information is presented.
  45. 45. Custom queries
  46. 46. Custom queriesPossible applications:
  47. 47. Custom queriesPossible applications: • Create custom feeds/displays
  48. 48. Custom queriesPossible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/
  49. 49. Custom queriesPossible applications: • Create custom feeds/displays • ephramzerb.com/projects/feed- wrangler/ • Pull information on other posts from within the theme’s Loop
  50. 50. Custom queriesPossible 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 Loopexamples • 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 queryEX:
  55. 55. Roll your own queryEX: • Pull information on other posts from within the theme’s Loop
  56. 56. Roll your own queryEX: • 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 queryEX: • 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 queryThe idea:
  60. 60. Roll your own queryThe idea: • Create a new WP_Query object.
  61. 61. Roll your own queryThe idea: • Create a new WP_Query object. • Given a $tag...
  62. 62. Roll your own queryThe idea: • Create a new WP_Query object. • Given a $tag... • new WP_Query(array(tag=> $tag))
  63. 63. Roll your own queryThe 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 queryThe 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 queryThe 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 LoopMake sure the Loop controllers areusing $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_QueryLearn more from the Codex:
  71. 71. More about WP_QueryLearn more from the Codex: • More information on the parameters:
  72. 72. More about WP_QueryLearn more from the Codex: • More information on the parameters: • codex.wordpress.org/ Template_Tags/query_posts
  73. 73. More about WP_QueryLearn more from the Codex: • More information on the parameters: • codex.wordpress.org/ Template_Tags/query_posts • Tips and examples:
  74. 74. More about WP_QueryLearn 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 queryEX:
  78. 78. Filter every queryEX: • Customize what information is displayed globally.
  79. 79. Filter every queryEX: • Customize what information is displayed globally. • Hide all my tweets.
  80. 80. Filter every query
  81. 81. Filter every queryThe idea:
  82. 82. Filter every queryThe 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 toolsTurn up the fire! • Firefox getfirefox.com • Firebug getfirebug.com • FirePHP firephp.org
  86. 86. Aside: the toolsTurn up the fire! • Firefox getfirefox.com • Firebug getfirebug.com • FirePHP firephp.org The “Pyrotrinity”
  87. 87. FirePHPTakes a moment to set up:
  88. 88. FirePHPTakes 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 stepsHit the docs:
  92. 92. Next stepsHit the docs: • PHP manual: php.net
  93. 93. Next stepsHit the docs: • PHP manual: php.net • Codex: codex.wordpress.org
  94. 94. Next stepsHit the docs: • PHP manual: php.net • Codex: codex.wordpress.org • The source: core.trac.wordpress.org
  95. 95. Next stepsHit 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 stepsLearn SQL
  98. 98. Next stepsLearn SQL • The language that WordPress’s raw database calls are in.
  99. 99. Next stepsLearn SQL • The language that WordPress’s raw database calls are in. • It’s really not that scary.
  100. 100. Next stepsLearn 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 stepsLearn 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

×