• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
WordPress Queries - the right way
 

WordPress Queries - the right way

on

  • 3,187 views

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 ...

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().

Statistics

Views

Total Views
3,187
Views on SlideShare
3,158
Embed Views
29

Actions

Likes
8
Downloads
67
Comments
1

5 Embeds 29

http://www.wpaustralia.org 20
https://twitter.com 4
http://dev.tjara.com 3
http://pinterest.com 1
http://dev.quark.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Is there a video available somewhere. I couldn't find one via Google. Thanks
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \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 WordPress Queries - the right way Presentation Transcript

  • 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;endif;
  • The Loopif ( have_posts() ) while ( have_posts() ) : // Sets up globals & continues iteration the_post(); endwhile;endif;
  • 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.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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 query$wp_query
  • 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
  • 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(); return $GLOBALS[wp_query]->query($query);}
  • 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);}
  • 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);}
  • 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();
  • 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 eight queries!
  • Don’t forget to reset// Restores the $wp_query reference to $wp_the_query// Resets the globalswp_reset_query();// Resets the globalswp_reset_postdata();
  • 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 reference.(any changes you make to $query are madedirectly to the original object)
  • 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— Everything!
  • 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 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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 );
  • 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() )
  • 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
  • That’s all folks!☺Thanks! Questions?