8. Introduction
• Hi, I’m mitcho.
• Linguist, coder, teacher.
• At MIT. Previously at Mozilla. Now
working with Automattic.
9. Introduction
• Hi, I’m mitcho.
• Linguist, coder, teacher.
• At MIT. Previously at Mozilla. Now
working with Automattic.
• Programming PHP/MySQL since
2002?
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. 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
16. Introduction
• Yet Another Related Posts Plugin
• ...aka YARPP
• As seen on Laughing Squid, ma.tt...
• Over 350k downloads
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. Today
• Introduction
• Loop basics
• Custom queries
• Method 1: Roll your own query
• Method 2: Filter every query
• Next steps
20. The Loop
• “The Loop” is the mechanism by
which posts are called from the
database and then displayed.
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. The simplest Loop
get_header();
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
get_sidebar();
get_footer();
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. 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. 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. The Loop
if there are posts
while there are posts
get the post
do stuff with it
end while
end if
27. The Loop
if there are posts
{
while there are posts
get the post
“The Loop”
do stuff with it
end while
end if
30. The Loop
• The Loop is where you can use
Template Tags.
• codex.wordpress.org/
Template_Tags
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. 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. 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. 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/
36. Every Loop has a query
• Regularly, WordPress chooses the
right template file and query based on
your URL.
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. 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. 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. 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. Today
• Introduction
• Loop basics
• Custom queries
• Method 1: Roll your own query
• Method 2: Filter every query
• Next steps
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. 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. 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. Today
• Introduction
• Loop basics
• Custom queries
• Method 1: Roll your own query
• Method 2: Filter every query
• Next steps
55. Roll your own query
EX:
• Pull information on other posts
from within the theme’s Loop
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. 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]
60. Roll your own query
The idea:
• Create a new WP_Query object.
61. Roll your own query
The idea:
• Create a new WP_Query object.
• Given a $tag...
62. Roll your own query
The idea:
• Create a new WP_Query object.
• Given a $tag...
• new WP_Query(array('tag'=>
$tag))
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. 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. 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. 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;
72. More about WP_Query
Learn more from the Codex:
• More information on the
parameters:
• codex.wordpress.org/
Template_Tags/query_posts
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. 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. Today
• Introduction
• Loop basics
• Custom queries
• Method 1: Roll your own query
• Method 2: Filter every query
• Next steps
99. Next steps
Learn SQL
• The language that WordPress’s raw
database calls are in.
• It’s really not that scary.
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. 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. Thank you!
Questions?
Slides will be up on mitcho.com/blog/.
See you at the Genius Bar!
mitcho (Michael 芳貴 Erlewine)
mitcho.com; @mitchoyoshitaka
Notas do Editor
\n
We have a good deal of content to cover.\nI may defer questions to the end. I can stick around later to answer questions too.\n
\n
We have a good deal of content to cover.\nI may defer questions to the end. I can stick around later to answer questions too.\n
\n
\n
\n
\n
\n
\n
My WP claim to fame\n
My WP claim to fame\n
My WP claim to fame\n
My WP claim to fame\n
My WP claim to fame\n
\n
\n
\n
\n
\n
\n
Knowing just this will help you modify themes intelligently.\n