getting into the loop

102
Getting Into The Loop mitcho (Michael 芳貴 Erlewine) http://mitcho.com January 23, 2010 WordCamp Boston

Upload: michael-yoshitaka-erlewine

Post on 29-Jan-2018

45.176 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Getting Into The Loop

Getting Into The Loopmitcho (Michael 芳貴 Erlewine)http://mitcho.com

January 23, 2010WordCamp Boston

Page 2: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 3: Getting Into The Loop

NOTE:• Will link to slides and code later

on http://mitcho.com/blog/• Please rate later on SpeakerRate

(find link on the blog)

Today

Page 4: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 5: Getting Into The Loop

Introduction

Page 6: Getting Into The Loop

Introduction• Hi, I’m mitcho.

Page 7: Getting Into The Loop

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.

Page 8: Getting Into The Loop

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.

Page 9: Getting Into The Loop

Introduction• Hi, I’m mitcho.

• Linguist, coder, teacher.• At MIT. Previously at Mozilla. Now

working with Automattic.• Programming PHP/MySQL since

2002?

Page 10: Getting Into The Loop

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.

Page 11: Getting Into The Loop

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

Page 12: Getting Into The Loop

Introduction

Page 13: Getting Into The Loop

• Yet Another Related Posts Plugin

Introduction

Page 14: Getting Into The Loop

• Yet Another Related Posts Plugin• ...aka YARPP

Introduction

Page 15: Getting Into The Loop

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...

Introduction

Page 16: Getting Into The Loop

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...• Over 350k downloads

Introduction

Page 17: Getting Into The Loop

• 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

Introduction

Page 18: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 19: Getting Into The Loop

The Loop

Page 20: Getting Into The Loop

The Loop

• “The Loop” is the mechanism by which posts are called from the database and then displayed.

Page 21: Getting Into The Loop

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.

Page 22: Getting Into The Loop

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

Page 23: Getting Into The Loop

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop”

Page 24: Getting Into The Loop

The simplest Loopget_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop” Sets up the post

Page 25: Getting Into The Loop

The simplest Loop

Every theme’s PHP files are built on this basic structure.

get_header();if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile;endif;get_sidebar();get_footer();

{“The Loop” Sets up the post

Page 26: Getting Into The Loop

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

Page 27: Getting Into The Loop

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

{“The Loop”

Page 28: Getting Into The Loop

The Loop

Page 29: Getting Into The Loop

The Loop

• The Loop is where you can use Template Tags.

Page 30: Getting Into The Loop

The Loop

• The Loop is where you can use Template Tags.• codex.wordpress.org/

Template_Tags

Page 31: Getting Into The Loop

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.

Page 32: Getting Into The Loop

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

CC BY flickr.com/photos/myklroventine/1430113497/

Page 33: Getting Into The Loop

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

But mommy, where doposts come from?

CC BY flickr.com/photos/myklroventine/1430113497/

Page 34: Getting Into The Loop

The Loop

if there are posts while there are posts get the post do stuff with it end whileend if

But mommy, where doposts come from?

CC BY flickr.com/photos/myklroventine/1430113497/

Page 35: Getting Into The Loop

Every Loop has a query

Page 36: Getting Into The Loop

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.

Page 37: Getting Into The Loop

Every Loop has a query

• Regularly, WordPress chooses the right template file and query based on your URL.• codex.wordpress.org/

Template_Hierarchy

Page 38: Getting Into The Loop

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

Page 39: Getting Into The Loop

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

Page 40: Getting Into The Loop

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

Page 41: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 42: Getting Into The Loop

Customizing The Loop

Page 43: Getting Into The Loop

Customizing The Loop

• Themes control how information is presented...

Page 44: Getting Into The Loop

Customizing The Loop

• Themes control how information is presented...

• The query controls what information is presented.

Page 45: Getting Into The Loop

Custom queries

Page 46: Getting Into The Loop

Custom queries

Possible applications:

Page 47: Getting Into The Loop

Custom queries

Possible applications:• Create custom feeds/displays

Page 48: Getting Into The Loop

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

Page 49: Getting Into The Loop

Custom queries

Possible applications:• Create custom feeds/displays

• ephramzerb.com/projects/feed-wrangler/

• Pull information on other posts from within the theme’s Loop

Page 50: Getting Into The Loop

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

Page 51: Getting Into The Loop

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

Today’sexamples

Page 52: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 53: Getting Into The Loop

Roll your own query

Page 54: Getting Into The Loop

Roll your own query

EX:

Page 55: Getting Into The Loop

Roll your own query

EX:• Pull information on other posts

from within the theme’s Loop

Page 56: Getting Into The Loop

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.

Page 57: Getting Into The Loop

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]

Page 58: Getting Into The Loop

Roll your own query

Page 59: Getting Into The Loop

Roll your own query

The idea:

Page 60: Getting Into The Loop

Roll your own query

The idea:• Create a new WP_Query object.

Page 61: Getting Into The Loop

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...

Page 62: Getting Into The Loop

Roll your own query

The idea:• Create a new WP_Query object.

• Given a $tag...• new WP_Query(array('tag'=>

$tag))

Page 63: Getting Into The Loop

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

Page 64: Getting Into The Loop

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

Page 65: Getting Into The Loop

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

Page 66: Getting Into The Loop

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;

Page 67: Getting Into The Loop

The result:

Page 68: Getting Into The Loop

DEMO:

Page 69: Getting Into The Loop

More about WP_Query

Page 70: Getting Into The Loop

More about WP_Query

Learn more from the Codex:

Page 71: Getting Into The Loop

More about WP_Query

Learn more from the Codex:• More information on the

parameters:

Page 72: Getting Into The Loop

More about WP_Query

Learn more from the Codex:• More information on the

parameters:• codex.wordpress.org/

Template_Tags/query_posts

Page 73: Getting Into The Loop

More about WP_Query

Learn more from the Codex:• More information on the

parameters:• codex.wordpress.org/

Template_Tags/query_posts• Tips and examples:

Page 74: Getting Into The Loop

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

Page 75: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 76: Getting Into The Loop

Filter every query

Page 77: Getting Into The Loop

Filter every query

EX:

Page 78: Getting Into The Loop

Filter every query

EX:• Customize what information

is displayed globally.

Page 79: Getting Into The Loop

Filter every query

EX:• Customize what information

is displayed globally.• Hide all my tweets.

Page 80: Getting Into The Loop

Filter every query

Page 81: Getting Into The Loop

The idea:

Filter every query

Page 82: Getting Into The Loop

The idea:• Use the request filter to specify

that we don’t want results from the “tweet” category (#10)

Filter every query

Page 83: Getting Into The Loop

The result:

Page 84: Getting Into The Loop

DEMO

Page 85: Getting Into The Loop

Aside: the tools

Turn up the fire!

• Firefox getfirefox.com

• Firebug getfirebug.com

• FirePHP firephp.org

Page 86: Getting Into The Loop

Aside: the tools

Turn up the fire!

• Firefox getfirefox.com

• Firebug getfirebug.com

• FirePHP firephp.org

The “Pyrotrinity”

Page 87: Getting Into The Loop

FirePHP

Takes a moment to set up:

Page 88: Getting Into The Loop

FirePHP

Takes a moment to set up:

Use it like this:

Page 89: Getting Into The Loop

• Introduction• Loop basics• Custom queries

• Method 1: Roll your own query• Method 2: Filter every query

• Next steps

Today

Page 90: Getting Into The Loop

Next steps

Page 91: Getting Into The Loop

Hit the docs:

Next steps

Page 92: Getting Into The Loop

Hit the docs:• PHP manual: php.net

Next steps

Page 93: Getting Into The Loop

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org

Next steps

Page 94: Getting Into The Loop

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org• The source:

core.trac.wordpress.org

Next steps

Page 95: Getting Into The Loop

Hit the docs:• PHP manual: php.net• Codex: codex.wordpress.org• The source:

core.trac.wordpress.org• grep it!

Next steps

Page 96: Getting Into The Loop

Next steps

Page 97: Getting Into The Loop

Learn SQL

Next steps

Page 98: Getting Into The Loop

Learn SQL• The language that WordPress’s raw

database calls are in.

Next steps

Page 99: Getting Into The Loop

Learn SQL• The language that WordPress’s raw

database calls are in.• It’s really not that scary.

Next steps

Page 100: Getting Into The Loop

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.

Next steps

Page 101: Getting Into The Loop

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/

Next steps

Page 102: Getting Into The Loop

Thank you!Questions?

Slides will be up on mitcho.com/blog/.See you at the Genius Bar!

mitcho (Michael 芳貴 Erlewine)mitcho.com; @mitchoyoshitaka