10. $wp_query object
The WP Class gives the $wp_query object
information that defines the current
request.
11. class WP_Query
$wp_query object
The WP Class gives the $wp_query object
information that defines the current
request.
$wp_query determines the query based on
that information and fetches the
requested posts
12. class WP_Query
class WP {
function main() {
$this->init();
$this->parse_request();
$this->query_posts();
$this->handle_404();
$this->register_globals()
$wp_query object
16. class WP_Query
Interacting with WP_Query
Before the loop
Altering the query, filters
17. class WP_Query
Interacting with WP_Query
During the loop
Conditionals and template tags,
secondary queries, get_sidebar(), more
secondary queries, get_footer().
19. How do we Query?
query_posts() alters the main query
20. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
21. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
WP_Query - returns an array of post objects
22. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
WP_Query - returns an array of post objects
get_posts() use for secondary queries
23. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
WP_Query - returns an array of post objects
get_posts() use for secondary queries
get_posts() - wrapper for a separate instance of WP_Query
returns an array of post objects
24. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
WP_Query - returns an array of post objects
get_posts() use for secondary queries
get_posts() - wrapper for a separate instance of WP_Query
returns an array of post objects
$wpdb->roll your own
25. How do we Query?
query_posts() alters the main query
new WP_Query() use for secondary queries
WP_Query - returns an array of post objects
get_posts() use for secondary queries
get_posts() - wrapper for a separate instance of WP_Query
returns an array of post objects
$wpdb->roll your own
$wpdb uses the wpdb class to execute any SQL query on the
WordPress database
30. Secondary Loops
get_posts()
$post = get_posts( array(
‘posts_per_page’ => 7
! ! ‘cat’ => -3,
));
/**
*
* To make post template tags ie: the_content()
* available when using get_posts() call the
* internal function setup_postdata() with the
* the $post array as its argument
*
*/
32. Secondary Loops
get_posts()
$post = get_posts( array(
‘posts_per_page’ => 7
! ! ‘cat’ => -3,
));
**/
*
* After looping through a separate query,
* wp_reset_postdata() restores the $post global
* to the current post in the main query
*
*/
37. query_posts()
query_posts( array(
‘posts_per_page’ => 7
! ! ‘cat’ => -3,
));
**/
* If you use query_posts() you HAVE to use
* wp_reset_query() to restore the $post globals
*
*/
39. query_posts()
query_posts() alters destroys the
global $wp_query variables
/**
*
* This will cause WordPress to run the query it
* thought you wanted and the one you’re actually going
* to use.
*
* breaks widgets
* pagination breaks
* conditional tags will not work
* and more...
*
*/
47. pre_get_posts()
pre_get_posts fires every time WordPress
fetches posts from the database.
nav menus, new wp_query, query_posts,
get_posts, that slider plugin your client
installed, everything.
52. What happens when we
run a query?
WordPress actually runs 4 SQL queries
every time a query is run
53. $my_query = new WP_Query();
while( $my_query->have_posts() ):
$my_query->the_post();
54. $my_query = new WP_Query();
while( $my_query->have_posts() ):
$my_query->the_post();
1)Gets the posts:
SELECT SQL_CALC_FOUND_ROWS...
FROM wp_post LIMIT 0, 10
55. $my_query = new WP_Query();
while( $my_query->have_posts() ):
$my_query->the_post();
1)Gets the posts:
SELECT SQL_CALC_FOUND_ROWS...
FROM wp_post LIMIT 0, 10
2)Counts the posts for pagination:
SELECT FOUND_ROWS()
56. $my_query = new WP_Query();
while( $my_query->have_posts() ):
$my_query->the_post();
1)Gets the posts:
SELECT SQL_CALC_FOUND_ROWS...
FROM wp_post LIMIT 0, 10
2)Counts the posts for pagination:
SELECT FOUND_ROWS()
3)Gets all the metadata:
SELECT post_id, meta_key, meta_value FROM
wp_postmeta WHERE post_id IN...
57. $my_query = new WP_Query();
while( $my_query->have_posts() ):
$my_query->the_post();
1)Gets the posts:
SELECT SQL_CALC_FOUND_ROWS...
FROM wp_post LIMIT 0, 10
2)Counts the posts for pagination:
SELECT FOUND_ROWS()
3)Gets all the metadata:
SELECT post_id, meta_key, meta_value FROM
wp_postmeta WHERE post_id IN...
4)Gets all the terms
SELECT… AS… INNER JOIN wp_term_taxonomy
AS... INNER JOIN wp_term_relationships
AS... ON... IN...
58. What if I don’t need
pagination, meta, or
terms?
59. What if I don’t need
pagination, meta, or
terms?
You can selectively turn these off
60. What if I don’t need
pagination, meta, or
terms?
You can selectively turn these off
$my_query = new WP_Query( array(
‘no_found_rows’ = true,
‘update_post_meta_cache’ => false,
‘update_post_term_cache’ => false,
) );
61. <review>
$wp_query is always the main query
if you use query_posts(), $wp_query now holds the query
you just created.
62. <review>
$wp_query is always the main query
if you use query_posts(), $wp_query now holds the query
you just created.
Use pre_get_posts() when you need to alter the main query.
It’s cleaner the “right” way to do it and your clients and
fellow developers will love you.
63. <review>
$wp_query is always the main query
if you use query_posts(), $wp_query now holds the query
you just created.
Use pre_get_posts() when you need to alter the main query.
It’s cleaner the “right” way to do it and your clients and
fellow developers will love you.
For secondary queries - custom sliders, sidebars, etc...
use get_posts() or create a new WP_Query
64. <review>
$wp_query is always the main query
if you use query_posts(), $wp_query now holds the query
you just created.
Use pre_get_posts() when you need to alter the main query.
It’s cleaner the “right” way to do it and your clients and
fellow developers will love you.
For secondary queries - custom sliders, sidebars, etc...
use get_posts() or create a new WP_Query
If you call the_post(), or set_up_postdata() use
wp_reset_postdata(). If you have to use query_posts() use
wp_reset_query().
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. WP class in gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
\n
WP::parse_request()\n- Parses the URL using WP_Rewrite\n- Sets up query variables for WP_Query\n
\n
\n
\n
\n
\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n
WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date.\n