WordPress Queries           -the right way                   Anthony Hortin#wpmelb          @maddisondesigns
How you’re probably queryingquery_posts()get_posts()new WP_Query()
You’re doing it wrong!
The Loopif ( have_posts() ) : while ( have_posts() ) :   the_post(); endwhile;endif;
The Loopif ( have_posts() ) // Determines if there’s anything to iterate while ( have_posts() ) :   the_post(); endwhile;e...
The Loopif ( have_posts() ) while ( have_posts() ) :   // Sets up globals & continues iteration   the_post(); endwhile;end...
Anatomy of a WordPress PageThe Main Query
wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.req...
wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.req...
wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.req...
wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.req...
What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
So, what does this mean?
It means...Before the theme is even loaded,WordPress already has your Posts!
Mind == Blown!
In the bootstrap$wp_the_query = new WP_Query();$wp_query =& $wp_the_query;
In the bootstrap// Holds the real main query.// It should never be modified$wp_the_query// A live reference to the main qu...
In the bootstrap// Holds the real main query.// It should never be modified$wp_the_query// A live reference to the main qu...
Back to our queries...
query_posts()get_posts()new WP_Query()All three create new WP_Query objects
query_posts()goes one step further though
query_posts()function &query_posts($query) {    unset($GLOBALS[wp_query]);    $GLOBALS[wp_query] = new WP_Query();    retu...
query_posts()function &query_posts($query) {  // Destroys the Global $wp_query variable!  unset($GLOBALS[wp_query]);    $G...
query_posts()function &query_posts($query) {    unset($GLOBALS[wp_query]);    // Creates new $wp_query variable    $GLOBAL...
I’m sure you’ve all done this...get_header();query_posts( cat=-1,-2,-3 );while( have_posts() ) :  the_post();endwhile;wp_r...
That’s running 2* queries!
That’s running 2* queries!It’s running the query WordPressthinks you want.It’s then running your new queryyou actually want.
* In actual fact, WP_Query doesn’t just run one query. It runs four! So, that means your template is actually running eigh...
Don’t forget to reset// Restores the $wp_query reference to $wp_the_query// Resets the globalswp_reset_query();// Resets t...
There’s a better way
Say hello to pre_get_posts[From the Codex]The pre_get_posts action gives developersaccess to the $query object by referenc...
Say hello to pre_get_postsWhat this means is we can change ourmain query before it’s run
pre_get_postspre_get_posts fires for every post query:— get_posts()— new WP_Query()— Sidebar widgets— Admin screen queries...
How do we use it?
In functions.php[example 1]function my_pre_get_posts( $query ) {    // Check if the main query and home and not admin    i...
In functions.php[example 1]function my_pre_get_posts( $query ) {    // Check if the main query and home and not admin    i...
In functions.php[example 1]function my_pre_get_posts( $query ) {    // Check if the main query and home and not admin    i...
In functions.php[example 1]function my_pre_get_posts( $query ) {    // Check if the main query and home and not admin    i...
In functions.php[example 1]function my_pre_get_posts( $query ) {    // Check if the main query and home and not admin    i...
In functions.php[example 2]function my_pre_get_posts( $query ) {    // Check if the main query and movie CPT archive and n...
In functions.php[example 2]function my_pre_get_posts( $query ) {    // Check if the main query and movie CPT archive and n...
In functions.php[example 2]function my_pre_get_posts( $query ) {    // Check if the main query and movie CPT archive and n...
In functions.php[example 2]function my_pre_get_posts( $query ) {    // Check if the main query and movie CPT archive and n...
Remember...Do:— Use pre_get_posts— Check if it’s the main query by using is_main_query()— Check it’s not an admin query by...
References// You Don’t Know Query - Andrew Nacinhttp://wordpress.tv/2012/06/15/andrew-nacin-wp_queryhttp://www.slideshare....
That’s all folks!☺Thanks! Questions?
Upcoming SlideShare
Loading in...5
×

WordPress Queries - the right way

3,672

Published on

This is a presentation for the November WordPress Melbourne meetup. It shows how to modify your main WordPress query the right way, using the pre_get_posts hook, rather than using query_posts() or even WP_Query().

Published in: Art & Photos
1 Comment
10 Likes
Statistics
Notes
  • Is there a video available somewhere. I couldn't find one via Google. Thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,672
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
84
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • wp-blog-header() -> wp-load.php -> wp-config.php -> wp-settings.php\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • WordPress Queries - the right way

    1. 1. WordPress Queries -the right way Anthony Hortin#wpmelb @maddisondesigns
    2. 2. How you’re probably queryingquery_posts()get_posts()new WP_Query()
    3. 3. You’re doing it wrong!
    4. 4. The Loopif ( have_posts() ) : while ( have_posts() ) : the_post(); endwhile;endif;
    5. 5. The Loopif ( have_posts() ) // Determines if there’s anything to iterate while ( have_posts() ) : the_post(); endwhile;endif;
    6. 6. The Loopif ( have_posts() ) while ( have_posts() ) : // Sets up globals & continues iteration the_post(); endwhile;endif;
    7. 7. Anatomy of a WordPress PageThe Main Query
    8. 8. wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.require_once( dirname(__FILE__) . /wp-load.php );wp();// Decide which template files to load// ie. archive.php, index.php etcrequire_once( ABSPATH . WPINC . /template-loader.php );
    9. 9. wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.require_once( dirname(__FILE__) . /wp-load.php );wp();// Decide which template files to load// ie. archive.php, index.php etcrequire_once( ABSPATH . WPINC . /template-loader.php );
    10. 10. wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.require_once( dirname(__FILE__) . /wp-load.php );wp();// Decide which template files to load// ie. archive.php, index.php etcrequire_once( ABSPATH . WPINC . /template-loader.php );
    11. 11. wp-blog-header.php// Loads the WordPress bootstrap// ie. Sets the ABSPATH constant.// Loads the wp-config.php file etc.require_once( dirname(__FILE__) . /wp-load.php );// Does ALL THE THINGS!wp();// Decide which template files to load// ie. archive.php, index.php etcrequire_once( ABSPATH . WPINC . /template-loader.php );
    12. 12. What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
    13. 13. What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
    14. 14. What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
    15. 15. What is wp()?wp();- Parses the URL & runs it through WP_Rewrite- Sets up query variables for WP_Query- Runs the query
    16. 16. So, what does this mean?
    17. 17. It means...Before the theme is even loaded,WordPress already has your Posts!
    18. 18. Mind == Blown!
    19. 19. In the bootstrap$wp_the_query = new WP_Query();$wp_query =& $wp_the_query;
    20. 20. In the bootstrap// Holds the real main query.// It should never be modified$wp_the_query// A live reference to the main query$wp_query
    21. 21. In the bootstrap// Holds the real main query.// It should never be modified$wp_the_query// A live reference to the main query$wp_query
    22. 22. Back to our queries...
    23. 23. query_posts()get_posts()new WP_Query()All three create new WP_Query objects
    24. 24. query_posts()goes one step further though
    25. 25. query_posts()function &query_posts($query) { unset($GLOBALS[wp_query]); $GLOBALS[wp_query] = new WP_Query(); return $GLOBALS[wp_query]->query($query);}
    26. 26. query_posts()function &query_posts($query) { // Destroys the Global $wp_query variable! unset($GLOBALS[wp_query]); $GLOBALS[wp_query] = new WP_Query(); return $GLOBALS[wp_query]->query($query);}
    27. 27. query_posts()function &query_posts($query) { unset($GLOBALS[wp_query]); // Creates new $wp_query variable $GLOBALS[wp_query] = new WP_Query(); return $GLOBALS[wp_query]->query($query);}
    28. 28. I’m sure you’ve all done this...get_header();query_posts( cat=-1,-2,-3 );while( have_posts() ) : the_post();endwhile;wp_reset_query();get_footer();
    29. 29. That’s running 2* queries!
    30. 30. That’s running 2* queries!It’s running the query WordPressthinks you want.It’s then running your new queryyou actually want.
    31. 31. * In actual fact, WP_Query doesn’t just run one query. It runs four! So, that means your template is actually running eight queries!
    32. 32. Don’t forget to reset// Restores the $wp_query reference to $wp_the_query// Resets the globalswp_reset_query();// Resets the globalswp_reset_postdata();
    33. 33. There’s a better way
    34. 34. Say hello to pre_get_posts[From the Codex]The pre_get_posts action gives developersaccess to the $query object by reference.(any changes you make to $query are madedirectly to the original object)
    35. 35. Say hello to pre_get_postsWhat this means is we can change ourmain query before it’s run
    36. 36. pre_get_postspre_get_posts fires for every post query:— get_posts()— new WP_Query()— Sidebar widgets— Admin screen queries— Everything!
    37. 37. How do we use it?
    38. 38. In functions.php[example 1]function my_pre_get_posts( $query ) { // Check if the main query and home and not admin if ( $query->is_main_query() && is_home() && !is_admin() ) { // Display only posts that belong to a certain Category $query->set( category_name, fatuity ); // Display only 3 posts per page $query->set( posts_per_page, 3 ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    39. 39. In functions.php[example 1]function my_pre_get_posts( $query ) { // Check if the main query and home and not admin if ( $query->is_main_query() && is_home() && !is_admin() ) { // Display only posts that belong to a certain Category $query->set( category_name, fatuity ); // Display only 3 posts per page $query->set( posts_per_page, 3 ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    40. 40. In functions.php[example 1]function my_pre_get_posts( $query ) { // Check if the main query and home and not admin if ( $query->is_main_query() && is_home() && !is_admin() ) { // Display only posts that belong to a certain Category $query->set( category_name, fatuity ); // Display only 3 posts per page $query->set( posts_per_page, 3 ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    41. 41. In functions.php[example 1]function my_pre_get_posts( $query ) { // Check if the main query and home and not admin if ( $query->is_main_query() && is_home() && !is_admin() ) { // Display only posts that belong to a certain Category $query->set( category_name, fatuity ); // Display only 3 posts per page $query->set( posts_per_page, 3 ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    42. 42. In functions.php[example 1]function my_pre_get_posts( $query ) { // Check if the main query and home and not admin if ( $query->is_main_query() && is_home() && !is_admin() ) { // Display only posts that belong to a certain Category $query->set( category_name, fatuity ); // Display only 3 posts per page $query->set( posts_per_page, 3 ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    43. 43. In functions.php[example 2]function my_pre_get_posts( $query ) { // Check if the main query and movie CPT archive and not admin if($query->is_main_query() && is_post_type_archive(movie) && !is_admin()){ // Display only posts from a certain taxonomies $query->set( tax_query, array( array( taxonomy => genre, field => slug, terms => array ( fantasy, sci-fi ) ) ) ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    44. 44. In functions.php[example 2]function my_pre_get_posts( $query ) { // Check if the main query and movie CPT archive and not admin if($query->is_main_query() && is_post_type_archive(movie) && !is_admin()){ // Display only posts from a certain taxonomies $query->set( tax_query, array( array( taxonomy => genre, field => slug, terms => array ( fantasy, sci-fi ) ) ) ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    45. 45. In functions.php[example 2]function my_pre_get_posts( $query ) { // Check if the main query and movie CPT archive and not admin if($query->is_main_query() && is_post_type_archive(movie) && !is_admin()){ // Display only posts from a certain taxonomies $query->set( tax_query, array( array( taxonomy => genre, field => slug, terms => array ( fantasy, sci-fi ) ) ) ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    46. 46. In functions.php[example 2]function my_pre_get_posts( $query ) { // Check if the main query and movie CPT archive and not admin if($query->is_main_query() && is_post_type_archive(movie) && !is_admin()){ // Display only posts from a certain taxonomies $query->set( tax_query, array( array( taxonomy => genre, field => slug, terms => array ( fantasy, sci-fi ) ) ) ); return; }}// Add our function to the pre_get_posts hookadd_action( pre_get_posts, my_pre_get_posts );
    47. 47. Remember...Do:— Use pre_get_posts— Check if it’s the main query by using is_main_query()— Check it’s not an admin query by using is_admin()— Check for specific templates using is_home(), etc..— Set up your query using same parameters as WP_Query()Don’t:— Use query_posts()unless you have a very good reason AND you use wp_reset_query()( if you really need a secondary query, use new WP_Query() )
    48. 48. References// You Don’t Know Query - Andrew Nacinhttp://wordpress.tv/2012/06/15/andrew-nacin-wp_queryhttp://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-portland-2011// Make sense of WP Query functionshttp://bit.ly/wpsequery// Querying Posts Without query_postshttp://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts// pre_get_posts on the WordPress Codexhttp://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts// Example code on Githubhttps://github.com/maddisondesigns/wpmelb-nov
    49. 49. That’s all folks!☺Thanks! Questions?
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×