SlideShare a Scribd company logo
Unlocking the Magical
Powers of WP_Query
Dustin Filippini
@dustyf
dustyf.com
The Loop
How to Get Posts?
query_posts()
Not a good way do it because it:
• Creates a new WP_Query object with
whatever parameters you set.
• Replaces the existing main query loop with a
new one (that is no longer the main query)
https://developer.wordpress.com/2012/05/14/
querying-posts-without-query_posts/
How to Get Posts?
get_posts()
• Returns an array of posts
• You can’t use the helper functions the_title(),
the_content, etc.
• Uncached function, so results wouldn’t be
cached in some areas like WordPress.com VIP
How to Get Posts?
new WP_Query()
• Sets up a new query object
• You can use the_content(), the_title(), etc.
• Actually powers both query_posts() and
get_posts() behind the scenes as well as the
main query
• Flexible to use for all kinds of post queries.
What is an object? A class?
Using the WP_Query Class
Some of the parameters you
can pass to WP_Query
• author
• category
• cat (id)
• category_name
• cat__in
• cat__not_in
• tax_query
• post_type
• posts_per_page
• post_parent
• post_status
• order
• orderby
• date_query
• year
• month_num
• meta_key
• meta_value
• meta_query
• has_password
• post__in
• name
• s
Properties of WP_Query
• $query
• $query_vars
• $queried_object
• $queried_object_id
• $posts
• $post_count
• $found_posts
• $max_num_pages
• $current_post
• $post
• $is_single, $is_page,
$is_archive,
$is_preview, $is_date,
$is_year, $is_month,
$is_time, $is_author,
$is_category, $is_tag,
$is_tax, $is_search,
$is_feed,
$is_comment_feed,
$is_trackback,
$is_home, $is_404,
and more…
Methods of WP_Query
• the_post()
• have_posts()
• rewind_posts()
• get_queried_object()
• get_queried_object_id()
• get( $query_var )
• set( $query_var, $value )
Dustin Filippini
@dustyf
dustyf.com

More Related Content

What's hot

Unleash the power of HTTP with ASP.NET Web API
Unleash the power of HTTP with ASP.NET Web APIUnleash the power of HTTP with ASP.NET Web API
Unleash the power of HTTP with ASP.NET Web API
Filip W
 

What's hot (20)

Building RESTful APIs
Building RESTful APIsBuilding RESTful APIs
Building RESTful APIs
 
Saving Time By Testing With Jest
Saving Time By Testing With JestSaving Time By Testing With Jest
Saving Time By Testing With Jest
 
Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricks
 
WordPress: Getting Under the Hood
WordPress: Getting Under the HoodWordPress: Getting Under the Hood
WordPress: Getting Under the Hood
 
eMusic: WordPress in the Enterprise
eMusic: WordPress in the EnterpriseeMusic: WordPress in the Enterprise
eMusic: WordPress in the Enterprise
 
Unleash the power of HTTP with ASP.NET Web API
Unleash the power of HTTP with ASP.NET Web APIUnleash the power of HTTP with ASP.NET Web API
Unleash the power of HTTP with ASP.NET Web API
 
PowerShell for SharePoint Developers
PowerShell for SharePoint DevelopersPowerShell for SharePoint Developers
PowerShell for SharePoint Developers
 
API Design & Security in django
API Design & Security in djangoAPI Design & Security in django
API Design & Security in django
 
A Day of REST
A Day of RESTA Day of REST
A Day of REST
 
Watir Presentation Sumanth Krishna. A
Watir Presentation   Sumanth Krishna. AWatir Presentation   Sumanth Krishna. A
Watir Presentation Sumanth Krishna. A
 
Live Coverage at The New York Times
Live Coverage at The New York TimesLive Coverage at The New York Times
Live Coverage at The New York Times
 
Day 7 - Make it Fast
Day 7 - Make it FastDay 7 - Make it Fast
Day 7 - Make it Fast
 
Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)Introduction To Ruby Watir (Web Application Testing In Ruby)
Introduction To Ruby Watir (Web Application Testing In Ruby)
 
How to build a rest api
How to build a rest apiHow to build a rest api
How to build a rest api
 
Keyword Driven Framework using WATIR
Keyword Driven Framework using WATIRKeyword Driven Framework using WATIR
Keyword Driven Framework using WATIR
 
Day 2 - Intro to Rails
Day 2 - Intro to RailsDay 2 - Intro to Rails
Day 2 - Intro to Rails
 
Alfresco Mvc - a seamless integration with Spring Mvc
Alfresco Mvc - a seamless integration with Spring MvcAlfresco Mvc - a seamless integration with Spring Mvc
Alfresco Mvc - a seamless integration with Spring Mvc
 
2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote2015 WordCamp Maine Keynote
2015 WordCamp Maine Keynote
 
40+ tips to use Postman more efficiently
40+ tips to use Postman more efficiently40+ tips to use Postman more efficiently
40+ tips to use Postman more efficiently
 
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
Write an API for Almost Anything: The Amazing Power and Flexibility of Django...
 

Viewers also liked

Collins powel liderazgo
Collins powel liderazgoCollins powel liderazgo
Collins powel liderazgo
Camilo Acosta
 
Intro to WordPress Child Themes
Intro to WordPress Child ThemesIntro to WordPress Child Themes
Intro to WordPress Child Themes
vegasgeek
 
WordPress per giornalisti freelance
WordPress per giornalisti freelance  WordPress per giornalisti freelance
WordPress per giornalisti freelance
GGDBologna
 

Viewers also liked (20)

Never Launch without Training
Never Launch without TrainingNever Launch without Training
Never Launch without Training
 
Becoming a better blogger condensed word camp
Becoming a better blogger condensed word campBecoming a better blogger condensed word camp
Becoming a better blogger condensed word camp
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
 
Dev to Delivery with Puppet, Vagrant and AWS
Dev to Delivery with Puppet, Vagrant and AWSDev to Delivery with Puppet, Vagrant and AWS
Dev to Delivery with Puppet, Vagrant and AWS
 
Collins powel liderazgo
Collins powel liderazgoCollins powel liderazgo
Collins powel liderazgo
 
Passwords, Attakcks, and Security, oh my!
Passwords, Attakcks, and Security, oh my!Passwords, Attakcks, and Security, oh my!
Passwords, Attakcks, and Security, oh my!
 
BuddyPress @ WordCamp
BuddyPress @ WordCampBuddyPress @ WordCamp
BuddyPress @ WordCamp
 
Wordpress para seus Clientes
Wordpress para seus ClientesWordpress para seus Clientes
Wordpress para seus Clientes
 
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress CodingWordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
WordCamp Milwaukee 2012 - Aaron Saray - Secure Wordpress Coding
 
Stop Creating Data For Sake of Creating Data
Stop Creating Data For Sake of Creating DataStop Creating Data For Sake of Creating Data
Stop Creating Data For Sake of Creating Data
 
Make Cash. Using Open Source. And WordPress.
Make Cash. Using Open Source. And WordPress.Make Cash. Using Open Source. And WordPress.
Make Cash. Using Open Source. And WordPress.
 
Breaking up (your code) is hard to do
Breaking up (your code) is hard to doBreaking up (your code) is hard to do
Breaking up (your code) is hard to do
 
Wordcamp Reno11 - keynote
Wordcamp Reno11 - keynoteWordcamp Reno11 - keynote
Wordcamp Reno11 - keynote
 
Reno-Tahoe WordCamp 2011 - WordPress End User Security - Dre Armeda
Reno-Tahoe WordCamp 2011 - WordPress End User Security - Dre ArmedaReno-Tahoe WordCamp 2011 - WordPress End User Security - Dre Armeda
Reno-Tahoe WordCamp 2011 - WordPress End User Security - Dre Armeda
 
Intro to WordPress Child Themes
Intro to WordPress Child ThemesIntro to WordPress Child Themes
Intro to WordPress Child Themes
 
Take the next step with git
Take the next step with gitTake the next step with git
Take the next step with git
 
BuddyPress Presentation - WCPhilly
BuddyPress Presentation - WCPhillyBuddyPress Presentation - WCPhilly
BuddyPress Presentation - WCPhilly
 
WordPress per giornalisti freelance
WordPress per giornalisti freelance  WordPress per giornalisti freelance
WordPress per giornalisti freelance
 
Converting Website Visitors Into Business Leads
Converting Website Visitors Into Business LeadsConverting Website Visitors Into Business Leads
Converting Website Visitors Into Business Leads
 
Object Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin DevelopmentObject Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin Development
 

Similar to Unlocking the Magical Powers of WP_Query

The WordPress University
The WordPress UniversityThe WordPress University
The WordPress University
Stephanie Leary
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
Hal Stern
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
Hal Stern
 
Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3
Hal Stern
 
Workshop: Creating your first WordPress plugin
Workshop: Creating your first WordPress pluginWorkshop: Creating your first WordPress plugin
Workshop: Creating your first WordPress plugin
ylefebvre
 
Parsing strange v2
Parsing strange v2Parsing strange v2
Parsing strange v2
Hal Stern
 

Similar to Unlocking the Magical Powers of WP_Query (20)

Writing a WordPress Theme - HighEdWeb 2013 #WRK2
Writing a WordPress Theme - HighEdWeb 2013 #WRK2Writing a WordPress Theme - HighEdWeb 2013 #WRK2
Writing a WordPress Theme - HighEdWeb 2013 #WRK2
 
Caching, Scaling, and What I've Learned from WordPress.com VIP
Caching, Scaling, and What I've Learned from WordPress.com VIPCaching, Scaling, and What I've Learned from WordPress.com VIP
Caching, Scaling, and What I've Learned from WordPress.com VIP
 
WordPress Themes 101 - PSUWeb13 Workshop
WordPress Themes 101 - PSUWeb13 WorkshopWordPress Themes 101 - PSUWeb13 Workshop
WordPress Themes 101 - PSUWeb13 Workshop
 
WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2WordPress Theme Design and Development Workshop - Day 2
WordPress Theme Design and Development Workshop - Day 2
 
WordPress Themes 101 - HighEdWeb New England 2013
WordPress Themes 101 - HighEdWeb New England 2013WordPress Themes 101 - HighEdWeb New England 2013
WordPress Themes 101 - HighEdWeb New England 2013
 
What you can do In WatiR
What you can do In WatiRWhat you can do In WatiR
What you can do In WatiR
 
WordPress Themes 101 - dotEduGuru Summit 2013
WordPress Themes 101 - dotEduGuru Summit 2013WordPress Themes 101 - dotEduGuru Summit 2013
WordPress Themes 101 - dotEduGuru Summit 2013
 
Streamlining Your Template Structures When Building Themes
Streamlining Your Template Structures When Building ThemesStreamlining Your Template Structures When Building Themes
Streamlining Your Template Structures When Building Themes
 
The WordPress University
The WordPress UniversityThe WordPress University
The WordPress University
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
 
Responsive themeworkshop wcneo2016
Responsive themeworkshop wcneo2016Responsive themeworkshop wcneo2016
Responsive themeworkshop wcneo2016
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
 
What are child themes, and why use them
What are child themes, and why use themWhat are child themes, and why use them
What are child themes, and why use them
 
Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3
 
Workshop: Creating your first WordPress plugin
Workshop: Creating your first WordPress pluginWorkshop: Creating your first WordPress plugin
Workshop: Creating your first WordPress plugin
 
Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?Theming in WordPress - Where do I Start?
Theming in WordPress - Where do I Start?
 
Parsing strange v2
Parsing strange v2Parsing strange v2
Parsing strange v2
 
Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)
 
Cain & Obenland — Episode 4
Cain & Obenland — Episode 4Cain & Obenland — Episode 4
Cain & Obenland — Episode 4
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Unlocking the Magical Powers of WP_Query

  • 1. Unlocking the Magical Powers of WP_Query Dustin Filippini @dustyf dustyf.com
  • 3.
  • 4.
  • 5. How to Get Posts? query_posts() Not a good way do it because it: • Creates a new WP_Query object with whatever parameters you set. • Replaces the existing main query loop with a new one (that is no longer the main query) https://developer.wordpress.com/2012/05/14/ querying-posts-without-query_posts/
  • 6. How to Get Posts? get_posts() • Returns an array of posts • You can’t use the helper functions the_title(), the_content, etc. • Uncached function, so results wouldn’t be cached in some areas like WordPress.com VIP
  • 7. How to Get Posts? new WP_Query() • Sets up a new query object • You can use the_content(), the_title(), etc. • Actually powers both query_posts() and get_posts() behind the scenes as well as the main query • Flexible to use for all kinds of post queries.
  • 8.
  • 9.
  • 10. What is an object? A class?
  • 12. Some of the parameters you can pass to WP_Query • author • category • cat (id) • category_name • cat__in • cat__not_in • tax_query • post_type • posts_per_page • post_parent • post_status • order • orderby • date_query • year • month_num • meta_key • meta_value • meta_query • has_password • post__in • name • s
  • 13. Properties of WP_Query • $query • $query_vars • $queried_object • $queried_object_id • $posts • $post_count • $found_posts • $max_num_pages • $current_post • $post • $is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, and more…
  • 14. Methods of WP_Query • the_post() • have_posts() • rewind_posts() • get_queried_object() • get_queried_object_id() • get( $query_var ) • set( $query_var, $value )
  • 15.
  • 16.