16
Getting What You Need With WP_Query Getting What You Need With WP_Query Topher DeRosia @topher1kenobe

Working with WP_Query in WordPress

Embed Size (px)

Citation preview

Page 1: Working with WP_Query in WordPress

Getting What You Need With WP_Query

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 2: Working with WP_Query in WordPress

Developer and Documenter from

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 3: Working with WP_Query in WordPress

What is WP_Query?

A database abstraction layer, allowing you to make consistent, error-free (mostly), safe database queries.

https://codex.wordpress.org/Class_Reference/WP_Query

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 4: Working with WP_Query in WordPress

When NOT to use WP_Query

When it’s already being run and already getting what you want.

Examples:TemplatesWhen an existing specialized function works as well

get_term() get_term_by() get_term_children() get_term_link() etc.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 5: Working with WP_Query in WordPress

When to use WP_Query

Any time you want content from a WordPress table that isn’t already being gotten.

Examples:Widget outputShortcode outputCustom template tag output

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 6: Working with WP_Query in WordPress

Example Code// The Query$the_query = new WP_Query( $args );

This simply creates a new instance of WP_Query.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 7: Working with WP_Query in WordPress

Example Code// Check the query object to see if we have posts

if ( $the_query->have_posts() ) { echo '<ul>';

echo '</ul>';} else { // no posts found}

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 8: Working with WP_Query in WordPress

Example Code// Check the query object to see if we have postsif ( $the_query->have_posts() ) { echo '<ul>';

// While we have posts, prepare each post and print the title while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul>';} else { // no posts found}

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 9: Working with WP_Query in WordPress

Example Code

/* Restore original Post Data */wp_reset_postdata();

Important! If you don’t reset then your query is going to taint other queries around it.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 10: Working with WP_Query in WordPress

Example CodeThis gets the default posts loop, like on your blog.

// The Query$the_query = new WP_Query( $args );

// The Loopif ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul>';} else { // no posts found}/* Restore original Post Data */wp_reset_postdata();

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 11: Working with WP_Query in WordPress

Customize WP_QueryIt’s all in the $args:

$args = array('post_type' => 'download','post_status' => 'publish',

);

Gets normal data, but for ‘download’ Custom Content Type.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 12: Working with WP_Query in WordPress

WP_Query Options

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

5.1 Author Parameters

5.2 Category Parameters

5.3 Tag Parameters

5.4 Taxonomy Parameters

5.5 Search Parameter

5.6 Post & Page Parameters

5.7 Password Parameters

5.8 Type Parameters

5.9 Status Parameters

5.10 Pagination Parameters

5.11 Order & Orderby Parameters

5.12 Date Parameters

5.13 Custom Field Parameters

5.14 Permission Parameters

5.15 Caching Parameters

5.16 Return Fields Parameter

Page 13: Working with WP_Query in WordPress

Key PointWP_Query returns data in a consistent way, regardless of your query.

What you do with that data is irrelevant to the query.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 14: Working with WP_Query in WordPress

Key Point ApplicationMake a function to hold your query.

Make that function cache the results in a transienthttps://codex.wordpress.org/Transients_API

Write other functions to render the data however you wish.

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 15: Working with WP_Query in WordPress

Tips and TricksSpeed things up with 'no_found_rows' => true

Querying by meta key is SLOW. Avoid if possible, cache if you must.

Almost always store results in a transient.https://youtu.be/UU7TdtLzPrA?t=8s

Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe

Page 16: Working with WP_Query in WordPress

THANKS FOR

LISTENING

Getting What You Need With WP_QueryTopher DeRosia

@topher1kenobe

http://topher1kenobe.comhttp://heropress.com

Follow me @topher1kenobe