SlideShare a Scribd company logo
1 of 94
Download to read offline
Who’s afraid of the
BIG BAD WOLF?
Chris Reynolds@jazzs3quence
LOOP
Who’s afraid of the
BIG BAD WOLF?
Chris Reynolds@jazzs3quence
LOOP
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
“The Loop is PHP code used by WordPress to display
posts. Using The Loop, WordPress processes each post
to be displayed on the current page, and formats it
according to how it matches specified criteria within
The Loop tags. Any HTML or PHP code in the Loop will
be processed on each post.”
— WordPress Codex
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
lolwut
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
lolwut
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
lolwut
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
lolwut
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
DA LOOP? WHO DIS?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
“Knock knock”
Who’s afraid of the big bad Loop?
“Knock knock”
Who’s afraid of the big bad Loop?
“…uh, who’s there?”
Who’s afraid of the big bad Loop?
“…uh, who’s there?”
Who’s afraid of the big bad Loop?
“Got any posts?”
Who’s afraid of the big bad Loop?
“Got any posts?”
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Checks if there are posts.
Who’s afraid of the big bad Loop?
Checks if there are posts.
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Keep going until you run out of posts.
Who’s afraid of the big bad Loop?
Keep going until you run out of posts.
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
THE MAGIC
Who’s afraid of the big bad Loop?
Title of the Proposal
?
reset_postdata
wp_reset_postdata
wp_reset_query
?
wp_reset_query
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
wp_reset_query() restores the $wp_query and global post
data to the original main query. This function should be called
after query_posts(), if you must use that function. As noted in
the examples below, it's heavily encouraged to use the
pre_get_posts filter to alter query parameters before the
query is made.
Calling wp_reset_query is not necessary after using
WP_Query or get_posts as these don't modify the main query
object. Instead use wp_reset_postdata
wp_reset_postdata
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
Use this function to restore the context of the template tags
from a secondary query loop back to the main query loop.
A secondary query loop using $sec_query = new WP_Query()
and $sec_query->the_post() affects the global $post
variable. The global $post variable is used by template tags
by default. wp_reset_postdata() restores the global $post
variable to the current post in the main query (contained in
the global $wp_query variable as opposed to the $sec_query
variable), so that the template tags refer to the main query
loop by default again.
reset_postdata
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
Title of the Proposal
reset_postdata
wp_reset_postdata
wp_reset_query
Title of the Proposal
reset_postdata
wp_reset_postdata
wp_reset_query
Title of the Proposal
reset_postdata
wp_reset_postdata
wp_reset_query
Title of the Proposal
reset_postdata
wp_reset_postdata
wp_reset_query
s3q.us/wcden2018-search
?s=patrick&post_type%5B%5D=post&
post_type%5B%5D=song&post_type%5B%5D=artist
s3q.us/wcden2018-search
s3q.us/wcden2018-
searchform
get_posts
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
Title of the Proposal
get_posts
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
WP_Query
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
$wp_query->request
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Title of the Proposal
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
$wp_query->posts

get_posts()
query_posts
Who’s afraid of the big bad Loop?
query_posts
Who’s afraid of the big bad Loop?
Who’s afraid of the big bad Loop?
query_posts
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
query_posts
https://www.dropbox.com/s/g3qv4965tb3bhzt/
Screenshot%202018-06-25%2009.21.54.png?dl=0
Who’s afraid of the big bad Loop?
Note: This function will completely override the
main query and isn’t intended for use by plugins
or themes. Its overly-simplistic approach to
modifying the main query can be problematic
and should be avoided wherever possible. In
most cases, there are better, more performant
options for modifying the main query such as via
the 'pre_get_posts' action within WP_Query.
The Loop
How WordPress fetches
posts.
get_posts
Returns the array of posts
from any query (including
the main query).
The Magic
All WordPress queries are
based around URL query
variables that are secretly
hidden.
WP_Query
Runs a WordPress query
for posts.
Resetting Post Data
Always use
wp_reset_postdata after
secondary queries. Period.
query_posts
Don’t use. Ever.
REVIEW!
QUESTIONS?
CHRIS REYNOLDS
Senior WordPress Engineer
@ Human Made
@jazzs3quence
https://github.com/jazzsequence
https://chrisreynolds.io
https://jazzsequence.com
http://s3q.us/wcden2018-loop
Chris Reynolds
me@chrisreynolds.io
@jazzs3quence
http://s3q.us/wcden2018-loop
THANK YOU.

More Related Content

Similar to Who's afraid of the big bad loop?

Similar to Who's afraid of the big bad loop? (6)

Challenges with Data Quality, Sharing, and Versioning in Next-Generation Sequ...
Challenges with Data Quality, Sharing, and Versioning in Next-Generation Sequ...Challenges with Data Quality, Sharing, and Versioning in Next-Generation Sequ...
Challenges with Data Quality, Sharing, and Versioning in Next-Generation Sequ...
 
Drupal 8.3.0: the features are ready, are you?
Drupal 8.3.0: the features are ready, are you?Drupal 8.3.0: the features are ready, are you?
Drupal 8.3.0: the features are ready, are you?
 
JoomlaDay Conference_September 2023 PDF.pdf
JoomlaDay Conference_September 2023 PDF.pdfJoomlaDay Conference_September 2023 PDF.pdf
JoomlaDay Conference_September 2023 PDF.pdf
 
The hero's journey
The hero's journeyThe hero's journey
The hero's journey
 
20151020 Metis
20151020 Metis20151020 Metis
20151020 Metis
 
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-SubramanyaErlang - Because s**t Happens by Mahesh Paolini-Subramanya
Erlang - Because s**t Happens by Mahesh Paolini-Subramanya
 

More from Chris Reynolds

9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC
Chris Reynolds
 

More from Chris Reynolds (10)

Developing an SDK for Personalization at the Edge
Developing an SDK for Personalization at the EdgeDeveloping an SDK for Personalization at the Edge
Developing an SDK for Personalization at the Edge
 
Outcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes EverythingOutcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
Outcomes vs Outputs: How Outcome Driven Development Planning Changes Everything
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...
 
How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...How the WordPress Block Editor Changes the Conversation for Content Editors a...
How the WordPress Block Editor Changes the Conversation for Content Editors a...
 
Being a better ally
Being a better allyBeing a better ally
Being a better ally
 
Drop Kick Imposter Syndrome
Drop Kick Imposter SyndromeDrop Kick Imposter Syndrome
Drop Kick Imposter Syndrome
 
Awesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and TeamsAwesome Git Workflow for Agencies and Teams
Awesome Git Workflow for Agencies and Teams
 
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary ThingWhy Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary Thing
 
9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC9 Things You Didn't Know You Could Do with Your Blog WPSLC
9 Things You Didn't Know You Could Do with Your Blog WPSLC
 
9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blog9 things you didn't know you could do with your blog
9 things you didn't know you could do with your blog
 

Recently uploaded

Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
sexy call girls service in goa
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Sheetaleventcompany
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 

Recently uploaded (20)

𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Pratap Nagar Delhi 💯Call Us 🔝8264348440🔝
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 

Who's afraid of the big bad loop?