Upload
topher1kenobe
View
650
Download
0
Embed Size (px)
Citation preview
Getting What You Need With WP_Query
Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe
Developer and Documenter from
Getting What You Need With WP_QueryTopher DeRosia@topher1kenobe
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
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
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
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
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
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
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
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
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
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
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
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
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
THANKS FOR
LISTENING
Getting What You Need With WP_QueryTopher DeRosia
@topher1kenobe
http://topher1kenobe.comhttp://heropress.com
Follow me @topher1kenobe