unlocking the magical powers of wp_query

17
Unlocking the Magical Powers of WP_Query Dustin Filippini @dustyf dustyf.com

Upload: dustin-filippini

Post on 11-Jan-2017

2.039 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Unlocking the Magical Powers of WP_Query

Unlocking the Magical Powers of WP_Query

Dustin Filippini @dustyf

dustyf.com

Page 2: Unlocking the Magical Powers of WP_Query

The Loop

Page 3: Unlocking the Magical Powers of WP_Query
Page 4: Unlocking the Magical Powers of WP_Query
Page 5: Unlocking the Magical Powers of WP_Query

How to Get Posts?query_posts()

Not a good way do it because it:

• Creates a new WP_Query object with whatever parameters you set.

• Replaces the existing main query loop with a new one (that is no longer the main query)

https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

Page 6: Unlocking the Magical Powers of WP_Query

How to Get Posts?get_posts()

• Returns an array of posts • You can’t use the helper functions the_title(),

the_content, etc. • Uncached function, so results wouldn’t be

cached in some areas like WordPress.com VIP

Page 7: Unlocking the Magical Powers of WP_Query

How to Get Posts?new WP_Query()

• Sets up a new query object • You can use the_content(), the_title(), etc. • Actually powers both query_posts() and

get_posts() behind the scenes as well as the main query

• Flexible to use for all kinds of post queries.

Page 8: Unlocking the Magical Powers of WP_Query
Page 9: Unlocking the Magical Powers of WP_Query
Page 10: Unlocking the Magical Powers of WP_Query

What is an object? A class?

Page 11: Unlocking the Magical Powers of WP_Query

Using the WP_Query Class

Page 12: Unlocking the Magical Powers of WP_Query

Some of the parameters you can pass to WP_Query

• author • category

• cat (id) • category_name • cat__in • cat__not_in

• tax_query • post_type • posts_per_page • post_parent • post_status

• order • orderby • date_query • year • month_num • meta_key • meta_value • meta_query • has_password • post__in • name • s

Page 13: Unlocking the Magical Powers of WP_Query

Properties of WP_Query

• $query • $query_vars • $queried_object • $queried_object_id • $posts • $post_count • $found_posts • $max_num_pages • $current_post • $post

• $is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search,$is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, and more…

Page 14: Unlocking the Magical Powers of WP_Query

Methods of WP_Query• the_post() • have_posts() • rewind_posts() • get_queried_object() • get_queried_object_id() • get( $query_var ) • set( $query_var, $value )

Page 15: Unlocking the Magical Powers of WP_Query
Page 16: Unlocking the Magical Powers of WP_Query
Page 17: Unlocking the Magical Powers of WP_Query

Dustin Filippini @dustyf

dustyf.com