3. 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_Query
Topher DeRosia
@topher1kenobe
4. When NOT to use WP_Query
When it’s already being run and already getting
what you want.
Examples:
Templates
When 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_Query
Topher DeRosia
@topher1kenobe
5. When to use WP_Query
Any time you want content from a WordPress
table that isn’t already being gotten.
Examples:
Widget output
Shortcode output
Custom template tag output
Getting What You Need With WP_Query
Topher DeRosia
@topher1kenobe
6. 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_Query
Topher DeRosia
@topher1kenobe
7. 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_Query
Topher DeRosia
@topher1kenobe
8. Example Code
// Check the query object to see if we have posts
if ( $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_Query
Topher DeRosia
@topher1kenobe
9. 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_Query
Topher DeRosia
@topher1kenobe
10. Example Code
This gets the default posts loop, like on your blog.
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $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_Query
Topher DeRosia
@topher1kenobe
11. Customize WP_Query
It’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_Query
Topher DeRosia
@topher1kenobe
12. WP_Query Options
Getting What You Need With WP_Query
Topher 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
13. Key Point
WP_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_Query
Topher DeRosia
@topher1kenobe
14. Key Point Application
Make a function to hold your query.
Make that function cache the results in a
transient
https://codex.wordpress.org/Transients_API
Write other functions to render the data
however you wish.
Getting What You Need With WP_Query
Topher DeRosia
@topher1kenobe
15. Tips and Tricks
Speed 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_Query
Topher DeRosia
@topher1kenobe
16. THANKS FOR
LISTENING
Getting What You Need With WP_Query
Topher DeRosia
@topher1kenobe
http://topher1kenobe.com
http://heropress.com
Follow me @topher1kenobe