5 * The query API attempts to get which part of WordPress to the user is on. It
6 * also provides functionality to getting URL query information.
8 * @link http://codex.wordpress.org/The_Loop More information on The Loop.
15 * Retrieve variable in the WP_Query class.
17 * @see WP_Query::get()
21 * @param string $var The variable key to retrieve.
24 function get_query_var($var) {
27 return $wp_query->get($var);
31 * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
33 * @uses WP_Query::get_queried_object
40 function get_queried_object() {
42 return $wp_query->get_queried_object();
46 * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
48 * @uses WP_Query::get_queried_object_id()
55 function get_queried_object_id() {
57 return $wp_query->get_queried_object_id();
63 * @see WP_Query::set()
67 * @param string $var Query variable key.
71 function set_query_var($var, $value) {
74 return $wp_query->set($var, $value);
78 * Set up The Loop with query parameters.
80 * This will override the current WordPress Loop and shouldn't be used more than
81 * once. This must not be used within the WordPress Loop.
86 * @param string $query
87 * @return array List of posts
89 function &query_posts($query) {
90 unset($GLOBALS['wp_query']);
91 $GLOBALS['wp_query'] = new WP_Query();
92 return $GLOBALS['wp_query']->query($query);
96 * Destroy the previous query and set up a new query.
98 * This should be used after {@link query_posts()} and before another {@link
99 * query_posts()}. This will remove obscure bugs that occur when the previous
100 * wp_query object is not destroyed properly before another is set up.
105 function wp_reset_query() {
106 unset($GLOBALS['wp_query']);
107 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
112 * After looping through a separate query, this function restores
113 * the $post global to the current post in the main query
118 function wp_reset_postdata() {
120 if ( !empty($wp_query->post) ) {
121 $GLOBALS['post'] = $wp_query->post;
122 setup_postdata($wp_query->post);
131 * Is the query for an archive page?
133 * Month, Year, Category, Author, Post Type archive...
135 * @see WP_Query::is_archive()
141 function is_archive() {
144 if ( ! isset( $wp_query ) ) {
145 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
149 return $wp_query->is_archive();
153 * Is the query for a post type archive page?
155 * @see WP_Query::is_post_type_archive()
159 * @param mixed $post_types Optional. Post type or array of posts types to check against.
162 function is_post_type_archive( $post_types = '' ) {
165 if ( ! isset( $wp_query ) ) {
166 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
170 return $wp_query->is_post_type_archive( $post_types );
174 * Is the query for an attachment page?
176 * @see WP_Query::is_attachment()
182 function is_attachment() {
185 if ( ! isset( $wp_query ) ) {
186 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
190 return $wp_query->is_attachment();
194 * Is the query for an author archive page?
196 * If the $author parameter is specified, this function will additionally
197 * check if the query is for one of the authors specified.
199 * @see WP_Query::is_author()
203 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
206 function is_author( $author = '' ) {
209 if ( ! isset( $wp_query ) ) {
210 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
214 return $wp_query->is_author( $author );
218 * Is the query for a category archive page?
220 * If the $category parameter is specified, this function will additionally
221 * check if the query is for one of the categories specified.
223 * @see WP_Query::is_category()
227 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
230 function is_category( $category = '' ) {
233 if ( ! isset( $wp_query ) ) {
234 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
238 return $wp_query->is_category( $category );
242 * Is the query for a tag archive page?
244 * If the $tag parameter is specified, this function will additionally
245 * check if the query is for one of the tags specified.
247 * @see WP_Query::is_tag()
251 * @param mixed $slug Optional. Tag slug or array of slugs.
254 function is_tag( $slug = '' ) {
257 if ( ! isset( $wp_query ) ) {
258 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
262 return $wp_query->is_tag( $slug );
266 * Is the query for a taxonomy archive page?
268 * If the $taxonomy parameter is specified, this function will additionally
269 * check if the query is for that specific $taxonomy.
271 * If the $term parameter is specified in addition to the $taxonomy parameter,
272 * this function will additionally check if the query is for one of the terms
275 * @see WP_Query::is_tax()
279 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
280 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
283 function is_tax( $taxonomy = '', $term = '' ) {
286 if ( ! isset( $wp_query ) ) {
287 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
291 return $wp_query->is_tax( $taxonomy, $term );
295 * Whether the current URL is within the comments popup window.
297 * @see WP_Query::is_comments_popup()
303 function is_comments_popup() {
306 if ( ! isset( $wp_query ) ) {
307 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
311 return $wp_query->is_comments_popup();
315 * Is the query for a date archive?
317 * @see WP_Query::is_date()
326 if ( ! isset( $wp_query ) ) {
327 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
331 return $wp_query->is_date();
335 * Is the query for a day archive?
337 * @see WP_Query::is_day()
346 if ( ! isset( $wp_query ) ) {
347 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
351 return $wp_query->is_day();
355 * Is the query for a feed?
357 * @see WP_Query::is_feed()
361 * @param string|array $feeds Optional feed types to check.
364 function is_feed( $feeds = '' ) {
367 if ( ! isset( $wp_query ) ) {
368 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
372 return $wp_query->is_feed( $feeds );
376 * Is the query for a comments feed?
378 * @see WP_Query::is_comments_feed()
384 function is_comment_feed() {
387 if ( ! isset( $wp_query ) ) {
388 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
392 return $wp_query->is_comment_feed();
396 * Is the query for the front page of the site?
398 * This is for what is displayed at your site's main URL.
400 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
402 * If you set a static page for the front page of your site, this function will return
403 * true when viewing that page.
405 * Otherwise the same as @see is_home()
407 * @see WP_Query::is_front_page()
412 * @return bool True, if front of site.
414 function is_front_page() {
417 if ( ! isset( $wp_query ) ) {
418 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
422 return $wp_query->is_front_page();
426 * Is the query for the blog homepage?
428 * This is the page which shows the time based blog content of your site.
430 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
432 * If you set a static page for the front page of your site, this function will return
433 * true only on the page you set as the "Posts page".
435 * @see is_front_page()
437 * @see WP_Query::is_home()
441 * @return bool True if blog view homepage.
446 if ( ! isset( $wp_query ) ) {
447 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
451 return $wp_query->is_home();
455 * Is the query for a month archive?
457 * @see WP_Query::is_month()
463 function is_month() {
466 if ( ! isset( $wp_query ) ) {
467 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
471 return $wp_query->is_month();
475 * Is the query for a single page?
477 * If the $page parameter is specified, this function will additionally
478 * check if the query is for one of the pages specified.
483 * @see WP_Query::is_page()
487 * @param mixed $page Page ID, title, slug, or array of such.
490 function is_page( $page = '' ) {
493 if ( ! isset( $wp_query ) ) {
494 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
498 return $wp_query->is_page( $page );
502 * Is the query for paged result and not for the first page?
504 * @see WP_Query::is_paged()
510 function is_paged() {
513 if ( ! isset( $wp_query ) ) {
514 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
518 return $wp_query->is_paged();
522 * Is the query for a post or page preview?
524 * @see WP_Query::is_preview()
530 function is_preview() {
533 if ( ! isset( $wp_query ) ) {
534 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
538 return $wp_query->is_preview();
542 * Is the query for the robots file?
544 * @see WP_Query::is_robots()
550 function is_robots() {
553 if ( ! isset( $wp_query ) ) {
554 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
558 return $wp_query->is_robots();
562 * Is the query for a search?
564 * @see WP_Query::is_search()
570 function is_search() {
573 if ( ! isset( $wp_query ) ) {
574 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
578 return $wp_query->is_search();
582 * Is the query for a single post?
584 * Works for any post type, except attachments and pages
586 * If the $post parameter is specified, this function will additionally
587 * check if the query is for one of the Posts specified.
592 * @see WP_Query::is_single()
596 * @param mixed $post Post ID, title, slug, or array of such.
599 function is_single( $post = '' ) {
602 if ( ! isset( $wp_query ) ) {
603 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
607 return $wp_query->is_single( $post );
611 * Is the query for a single post of any post type (post, attachment, page, ... )?
613 * If the $post_types parameter is specified, this function will additionally
614 * check if the query is for one of the Posts Types specified.
619 * @see WP_Query::is_singular()
623 * @param mixed $post_types Optional. Post Type or array of Post Types
626 function is_singular( $post_types = '' ) {
629 if ( ! isset( $wp_query ) ) {
630 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
634 return $wp_query->is_singular( $post_types );
638 * Is the query for a specific time?
640 * @see WP_Query::is_time()
649 if ( ! isset( $wp_query ) ) {
650 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
654 return $wp_query->is_time();
658 * Is the query for a trackback endpoint call?
660 * @see WP_Query::is_trackback()
666 function is_trackback() {
669 if ( ! isset( $wp_query ) ) {
670 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
674 return $wp_query->is_trackback();
678 * Is the query for a specific year?
680 * @see WP_Query::is_year()
689 if ( ! isset( $wp_query ) ) {
690 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
694 return $wp_query->is_year();
698 * Is the query a 404 (returns no results)?
700 * @see WP_Query::is_404()
709 if ( ! isset( $wp_query ) ) {
710 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
714 return $wp_query->is_404();
718 * Is the query the main query?
724 function is_main_query() {
726 return $wp_query->is_main_query();
730 * The Loop. Post loop control.
734 * Whether current WordPress query has results to loop over.
736 * @see WP_Query::have_posts()
742 function have_posts() {
745 return $wp_query->have_posts();
749 * Whether the caller is in the Loop.
754 * @return bool True if caller is within loop, false if loop hasn't started or ended.
756 function in_the_loop() {
759 return $wp_query->in_the_loop;
763 * Rewind the loop posts.
765 * @see WP_Query::rewind_posts()
771 function rewind_posts() {
774 return $wp_query->rewind_posts();
778 * Iterate the post index in the loop.
780 * @see WP_Query::the_post()
784 function the_post() {
787 $wp_query->the_post();
795 * Whether there are comments to loop over.
797 * @see WP_Query::have_comments()
803 function have_comments() {
805 return $wp_query->have_comments();
809 * Iterate comment index in the comment loop.
811 * @see WP_Query::the_comment()
817 function the_comment() {
819 return $wp_query->the_comment();
827 * The WordPress Query class.
829 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
836 * Query vars set by the user
845 * Query vars, after parsing
851 var $query_vars = array();
854 * Taxonomy query, as passed to get_tax_sql()
858 * @var object WP_Tax_Query
863 * Metadata query container
867 * @var object WP_Meta_Query
869 var $meta_query = false;
872 * Holds the data for a single object that is queried.
874 * Holds the contents of a post, page, category, attachment.
883 * The ID of the queried object.
889 var $queried_object_id;
892 * Get post database query.
910 * The amount of posts for the current query.
919 * Index of the current item in the loop.
925 var $current_post = -1;
928 * Whether the loop has started and the caller is in the loop.
934 var $in_the_loop = false;
937 * The current post ID.
946 * The list of comments for current post.
955 * The amount of comments for the posts.
961 var $comment_count = 0;
964 * The index of the comment in the comment loop.
970 var $current_comment = -1;
973 * Current comment ID.
982 * Amount of posts if limit clause was not used.
988 var $found_posts = 0;
991 * The amount of pages.
997 var $max_num_pages = 0;
1000 * The amount of comment pages.
1006 var $max_num_comment_pages = 0;
1009 * Set if query is single post.
1015 var $is_single = false;
1018 * Set if query is preview of blog.
1024 var $is_preview = false;
1027 * Set if query returns a page.
1033 var $is_page = false;
1036 * Set if query is an archive list.
1042 var $is_archive = false;
1045 * Set if query is part of a date.
1051 var $is_date = false;
1054 * Set if query contains a year.
1060 var $is_year = false;
1063 * Set if query contains a month.
1069 var $is_month = false;
1072 * Set if query contains a day.
1078 var $is_day = false;
1081 * Set if query contains time.
1087 var $is_time = false;
1090 * Set if query contains an author.
1096 var $is_author = false;
1099 * Set if query contains category.
1105 var $is_category = false;
1108 * Set if query contains tag.
1114 var $is_tag = false;
1117 * Set if query contains taxonomy.
1123 var $is_tax = false;
1126 * Set if query was part of a search result.
1132 var $is_search = false;
1135 * Set if query is feed display.
1141 var $is_feed = false;
1144 * Set if query is comment feed display.
1150 var $is_comment_feed = false;
1153 * Set if query is trackback.
1159 var $is_trackback = false;
1162 * Set if query is blog homepage.
1168 var $is_home = false;
1171 * Set if query couldn't found anything.
1177 var $is_404 = false;
1180 * Set if query is within comments popup window.
1186 var $is_comments_popup = false;
1189 * Set if query is paged
1195 var $is_paged = false;
1198 * Set if query is part of administration page.
1204 var $is_admin = false;
1207 * Set if query is an attachment.
1213 var $is_attachment = false;
1216 * Set if is single, is a page, or is an attachment.
1222 var $is_singular = false;
1225 * Set if query is for robots.
1231 var $is_robots = false;
1234 * Set if query contains posts.
1236 * Basically, the homepage if the option isn't set for the static homepage.
1242 var $is_posts_page = false;
1245 * Set if query is for a post type archive.
1251 var $is_post_type_archive = false;
1254 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1255 * whether we have to re-parse because something has changed
1260 var $query_vars_hash = false;
1263 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1264 * via pre_get_posts hooks.
1269 var $query_vars_changed = true;
1272 * Set if post thumbnails are cached
1278 var $thumbnails_cached = false;
1281 * Resets query flags to false.
1283 * The query flags are what page info WordPress was able to figure out.
1288 function init_query_flags() {
1289 $this->is_single = false;
1290 $this->is_preview = false;
1291 $this->is_page = false;
1292 $this->is_archive = false;
1293 $this->is_date = false;
1294 $this->is_year = false;
1295 $this->is_month = false;
1296 $this->is_day = false;
1297 $this->is_time = false;
1298 $this->is_author = false;
1299 $this->is_category = false;
1300 $this->is_tag = false;
1301 $this->is_tax = false;
1302 $this->is_search = false;
1303 $this->is_feed = false;
1304 $this->is_comment_feed = false;
1305 $this->is_trackback = false;
1306 $this->is_home = false;
1307 $this->is_404 = false;
1308 $this->is_comments_popup = false;
1309 $this->is_paged = false;
1310 $this->is_admin = false;
1311 $this->is_attachment = false;
1312 $this->is_singular = false;
1313 $this->is_robots = false;
1314 $this->is_posts_page = false;
1315 $this->is_post_type_archive = false;
1319 * Initiates object properties and sets default values.
1325 unset($this->posts);
1326 unset($this->query);
1327 $this->query_vars = array();
1328 unset($this->queried_object);
1329 unset($this->queried_object_id);
1330 $this->post_count = 0;
1331 $this->current_post = -1;
1332 $this->in_the_loop = false;
1333 unset( $this->request );
1334 unset( $this->post );
1335 unset( $this->comments );
1336 unset( $this->comment );
1337 $this->comment_count = 0;
1338 $this->current_comment = -1;
1339 $this->found_posts = 0;
1340 $this->max_num_pages = 0;
1341 $this->max_num_comment_pages = 0;
1343 $this->init_query_flags();
1347 * Reparse the query vars.
1352 function parse_query_vars() {
1353 $this->parse_query();
1357 * Fills in the query variables, which do not exist within the parameter.
1362 * @param array $array Defined query variables.
1363 * @return array Complete query variables with undefined ones filled in empty.
1365 function fill_query_vars($array) {
1403 foreach ( $keys as $key ) {
1404 if ( !isset($array[$key]) )
1408 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1409 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1411 foreach ( $array_keys as $key ) {
1412 if ( !isset($array[$key]) )
1413 $array[$key] = array();
1419 * Parse a query string and set query type booleans.
1424 * @param string|array $query Optional query.
1426 function parse_query( $query = '' ) {
1427 if ( ! empty( $query ) ) {
1429 $this->query = $this->query_vars = wp_parse_args( $query );
1430 } elseif ( ! isset( $this->query ) ) {
1431 $this->query = $this->query_vars;
1434 $this->query_vars = $this->fill_query_vars($this->query_vars);
1435 $qv = &$this->query_vars;
1436 $this->query_vars_changed = true;
1438 if ( ! empty($qv['robots']) )
1439 $this->is_robots = true;
1441 $qv['p'] = absint($qv['p']);
1442 $qv['page_id'] = absint($qv['page_id']);
1443 $qv['year'] = absint($qv['year']);
1444 $qv['monthnum'] = absint($qv['monthnum']);
1445 $qv['day'] = absint($qv['day']);
1446 $qv['w'] = absint($qv['w']);
1447 $qv['m'] = absint($qv['m']);
1448 $qv['paged'] = absint($qv['paged']);
1449 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1450 $qv['pagename'] = trim( $qv['pagename'] );
1451 $qv['name'] = trim( $qv['name'] );
1452 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1453 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1454 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1456 // Compat. Map subpost to attachment.
1457 if ( '' != $qv['subpost'] )
1458 $qv['attachment'] = $qv['subpost'];
1459 if ( '' != $qv['subpost_id'] )
1460 $qv['attachment_id'] = $qv['subpost_id'];
1462 $qv['attachment_id'] = absint($qv['attachment_id']);
1464 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1465 $this->is_single = true;
1466 $this->is_attachment = true;
1467 } elseif ( '' != $qv['name'] ) {
1468 $this->is_single = true;
1469 } elseif ( $qv['p'] ) {
1470 $this->is_single = true;
1471 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1472 // If year, month, day, hour, minute, and second are set, a single
1473 // post is being queried.
1474 $this->is_single = true;
1475 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1476 $this->is_page = true;
1477 $this->is_single = false;
1479 // Look for archive queries. Dates, categories, authors, search, post type archives.
1481 if ( !empty($qv['s']) ) {
1482 $this->is_search = true;
1485 if ( '' !== $qv['second'] ) {
1486 $this->is_time = true;
1487 $this->is_date = true;
1490 if ( '' !== $qv['minute'] ) {
1491 $this->is_time = true;
1492 $this->is_date = true;
1495 if ( '' !== $qv['hour'] ) {
1496 $this->is_time = true;
1497 $this->is_date = true;
1501 if ( ! $this->is_date ) {
1502 $this->is_day = true;
1503 $this->is_date = true;
1507 if ( $qv['monthnum'] ) {
1508 if ( ! $this->is_date ) {
1509 $this->is_month = true;
1510 $this->is_date = true;
1514 if ( $qv['year'] ) {
1515 if ( ! $this->is_date ) {
1516 $this->is_year = true;
1517 $this->is_date = true;
1522 $this->is_date = true;
1523 if ( strlen($qv['m']) > 9 ) {
1524 $this->is_time = true;
1525 } else if ( strlen($qv['m']) > 7 ) {
1526 $this->is_day = true;
1527 } else if ( strlen($qv['m']) > 5 ) {
1528 $this->is_month = true;
1530 $this->is_year = true;
1534 if ( '' != $qv['w'] ) {
1535 $this->is_date = true;
1538 $this->query_vars_hash = false;
1539 $this->parse_tax_query( $qv );
1541 foreach ( $this->tax_query->queries as $tax_query ) {
1542 if ( 'NOT IN' != $tax_query['operator'] ) {
1543 switch ( $tax_query['taxonomy'] ) {
1545 $this->is_category = true;
1548 $this->is_tag = true;
1551 $this->is_tax = true;
1555 unset( $tax_query );
1557 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1558 $this->is_author = false;
1560 $this->is_author = true;
1563 if ( '' != $qv['author_name'] )
1564 $this->is_author = true;
1566 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1567 $post_type_obj = get_post_type_object( $qv['post_type'] );
1568 if ( ! empty( $post_type_obj->has_archive ) )
1569 $this->is_post_type_archive = true;
1572 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1573 $this->is_archive = true;
1576 if ( '' != $qv['feed'] )
1577 $this->is_feed = true;
1579 if ( '' != $qv['tb'] )
1580 $this->is_trackback = true;
1582 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1583 $this->is_paged = true;
1585 if ( '' != $qv['comments_popup'] )
1586 $this->is_comments_popup = true;
1588 // if we're previewing inside the write screen
1589 if ( '' != $qv['preview'] )
1590 $this->is_preview = true;
1593 $this->is_admin = true;
1595 if ( false !== strpos($qv['feed'], 'comments-') ) {
1596 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1597 $qv['withcomments'] = 1;
1600 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1602 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1603 $this->is_comment_feed = true;
1605 if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
1606 $this->is_home = true;
1608 // Correct is_* for page_on_front and page_for_posts
1609 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1610 $_query = wp_parse_args($this->query);
1611 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1612 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1613 unset($_query['pagename']);
1614 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1615 $this->is_page = true;
1616 $this->is_home = false;
1617 $qv['page_id'] = get_option('page_on_front');
1618 // Correct <!--nextpage--> for page_on_front
1619 if ( !empty($qv['paged']) ) {
1620 $qv['page'] = $qv['paged'];
1621 unset($qv['paged']);
1626 if ( '' != $qv['pagename'] ) {
1627 $this->queried_object = get_page_by_path($qv['pagename']);
1628 if ( !empty($this->queried_object) )
1629 $this->queried_object_id = (int) $this->queried_object->ID;
1631 unset($this->queried_object);
1633 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1634 $this->is_page = false;
1635 $this->is_home = true;
1636 $this->is_posts_page = true;
1640 if ( $qv['page_id'] ) {
1641 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1642 $this->is_page = false;
1643 $this->is_home = true;
1644 $this->is_posts_page = true;
1648 if ( !empty($qv['post_type']) ) {
1649 if ( is_array($qv['post_type']) )
1650 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1652 $qv['post_type'] = sanitize_key($qv['post_type']);
1655 if ( ! empty( $qv['post_status'] ) ) {
1656 if ( is_array( $qv['post_status'] ) )
1657 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1659 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1662 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1663 $this->is_comment_feed = false;
1665 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1666 // Done correcting is_* for page_on_front and page_for_posts
1668 if ( '404' == $qv['error'] )
1671 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1672 $this->query_vars_changed = false;
1674 do_action_ref_array('parse_query', array(&$this));
1678 * Parses various taxonomy related query vars.
1683 * @param array &$q The query variables
1685 function parse_tax_query( &$q ) {
1686 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1687 $tax_query = $q['tax_query'];
1689 $tax_query = array();
1692 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1693 $tax_query[] = array(
1694 'taxonomy' => $q['taxonomy'],
1695 'terms' => array( $q['term'] ),
1700 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1701 if ( 'post_tag' == $taxonomy )
1702 continue; // Handled further down in the $q['tag'] block
1704 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1705 $tax_query_defaults = array(
1706 'taxonomy' => $taxonomy,
1710 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1711 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1714 $term = $q[$t->query_var];
1716 if ( strpos($term, '+') !== false ) {
1717 $terms = preg_split( '/[+]+/', $term );
1718 foreach ( $terms as $term ) {
1719 $tax_query[] = array_merge( $tax_query_defaults, array(
1720 'terms' => array( $term )
1724 $tax_query[] = array_merge( $tax_query_defaults, array(
1725 'terms' => preg_split( '/[,]+/', $term )
1732 if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1733 $q['cat'] = ''.urldecode($q['cat']).'';
1734 $q['cat'] = addslashes_gpc($q['cat']);
1735 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1737 $req_cats = array();
1738 foreach ( (array) $cat_array as $cat ) {
1739 $cat = intval($cat);
1744 $q['category__in'][] = $cat;
1745 $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1747 $q['category__not_in'][] = $cat;
1748 $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1751 $q['cat'] = implode(',', $req_cats);
1754 if ( !empty($q['category__in']) ) {
1755 $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
1756 $tax_query[] = array(
1757 'taxonomy' => 'category',
1758 'terms' => $q['category__in'],
1759 'field' => 'term_id',
1760 'include_children' => false
1764 if ( !empty($q['category__not_in']) ) {
1765 $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
1766 $tax_query[] = array(
1767 'taxonomy' => 'category',
1768 'terms' => $q['category__not_in'],
1769 'operator' => 'NOT IN',
1770 'include_children' => false
1774 if ( !empty($q['category__and']) ) {
1775 $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
1776 $tax_query[] = array(
1777 'taxonomy' => 'category',
1778 'terms' => $q['category__and'],
1779 'field' => 'term_id',
1780 'operator' => 'AND',
1781 'include_children' => false
1786 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1787 if ( strpos($q['tag'], ',') !== false ) {
1788 $tags = preg_split('/[,\s]+/', $q['tag']);
1789 foreach ( (array) $tags as $tag ) {
1790 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1791 $q['tag_slug__in'][] = $tag;
1793 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1794 $tags = preg_split('/[+\s]+/', $q['tag']);
1795 foreach ( (array) $tags as $tag ) {
1796 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1797 $q['tag_slug__and'][] = $tag;
1800 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1801 $q['tag_slug__in'][] = $q['tag'];
1805 if ( !empty($q['tag_id']) ) {
1806 $q['tag_id'] = absint( $q['tag_id'] );
1807 $tax_query[] = array(
1808 'taxonomy' => 'post_tag',
1809 'terms' => $q['tag_id']
1813 if ( !empty($q['tag__in']) ) {
1814 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1815 $tax_query[] = array(
1816 'taxonomy' => 'post_tag',
1817 'terms' => $q['tag__in']
1821 if ( !empty($q['tag__not_in']) ) {
1822 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1823 $tax_query[] = array(
1824 'taxonomy' => 'post_tag',
1825 'terms' => $q['tag__not_in'],
1826 'operator' => 'NOT IN'
1830 if ( !empty($q['tag__and']) ) {
1831 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1832 $tax_query[] = array(
1833 'taxonomy' => 'post_tag',
1834 'terms' => $q['tag__and'],
1839 if ( !empty($q['tag_slug__in']) ) {
1840 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1841 $tax_query[] = array(
1842 'taxonomy' => 'post_tag',
1843 'terms' => $q['tag_slug__in'],
1848 if ( !empty($q['tag_slug__and']) ) {
1849 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1850 $tax_query[] = array(
1851 'taxonomy' => 'post_tag',
1852 'terms' => $q['tag_slug__and'],
1858 $this->tax_query = new WP_Tax_Query( $tax_query );
1862 * Sets the 404 property and saves whether query is feed.
1867 function set_404() {
1868 $is_feed = $this->is_feed;
1870 $this->init_query_flags();
1871 $this->is_404 = true;
1873 $this->is_feed = $is_feed;
1877 * Retrieve query variable.
1882 * @param string $query_var Query variable key.
1885 function get($query_var) {
1886 if ( isset($this->query_vars[$query_var]) )
1887 return $this->query_vars[$query_var];
1893 * Set query variable.
1898 * @param string $query_var Query variable key.
1899 * @param mixed $value Query variable value.
1901 function set($query_var, $value) {
1902 $this->query_vars[$query_var] = $value;
1906 * Retrieve the posts based on query variables.
1908 * There are a few filters and actions that can be used to modify the post
1913 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1915 * @return array List of posts.
1917 function &get_posts() {
1918 global $wpdb, $user_ID, $_wp_using_ext_object_cache;
1920 $this->parse_query();
1922 do_action_ref_array('pre_get_posts', array(&$this));
1925 $q = &$this->query_vars;
1927 // Fill again in case pre_get_posts unset some vars.
1928 $q = $this->fill_query_vars($q);
1931 $this->meta_query = new WP_Meta_Query();
1932 $this->meta_query->parse_query_vars( $q );
1934 // Set a flag if a pre_get_posts hook changed the query vars.
1935 $hash = md5( serialize( $this->query_vars ) );
1936 if ( $hash != $this->query_vars_hash ) {
1937 $this->query_vars_changed = true;
1938 $this->query_vars_hash = $hash;
1942 // First let's clear some variables
1945 $whichmimetype = '';
1952 $post_status_join = false;
1955 if ( isset( $q['caller_get_posts'] ) ) {
1956 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
1957 if ( !isset( $q['ignore_sticky_posts'] ) )
1958 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
1961 if ( !isset( $q['ignore_sticky_posts'] ) )
1962 $q['ignore_sticky_posts'] = false;
1964 if ( !isset($q['suppress_filters']) )
1965 $q['suppress_filters'] = false;
1967 if ( !isset($q['cache_results']) ) {
1968 if ( $_wp_using_ext_object_cache )
1969 $q['cache_results'] = false;
1971 $q['cache_results'] = true;
1974 if ( !isset($q['update_post_term_cache']) )
1975 $q['update_post_term_cache'] = true;
1977 if ( !isset($q['update_post_meta_cache']) )
1978 $q['update_post_meta_cache'] = true;
1980 if ( !isset($q['post_type']) ) {
1981 if ( $this->is_search )
1982 $q['post_type'] = 'any';
1984 $q['post_type'] = '';
1986 $post_type = $q['post_type'];
1987 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1988 $q['posts_per_page'] = get_option('posts_per_page');
1989 if ( isset($q['showposts']) && $q['showposts'] ) {
1990 $q['showposts'] = (int) $q['showposts'];
1991 $q['posts_per_page'] = $q['showposts'];
1993 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1994 $q['posts_per_page'] = $q['posts_per_archive_page'];
1995 if ( !isset($q['nopaging']) ) {
1996 if ( $q['posts_per_page'] == -1 ) {
1997 $q['nopaging'] = true;
1999 $q['nopaging'] = false;
2002 if ( $this->is_feed ) {
2003 $q['posts_per_page'] = get_option('posts_per_rss');
2004 $q['nopaging'] = false;
2006 $q['posts_per_page'] = (int) $q['posts_per_page'];
2007 if ( $q['posts_per_page'] < -1 )
2008 $q['posts_per_page'] = abs($q['posts_per_page']);
2009 else if ( $q['posts_per_page'] == 0 )
2010 $q['posts_per_page'] = 1;
2012 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2013 $q['comments_per_page'] = get_option('comments_per_page');
2015 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2016 $this->is_page = true;
2017 $this->is_home = false;
2018 $q['page_id'] = get_option('page_on_front');
2021 if ( isset($q['page']) ) {
2022 $q['page'] = trim($q['page'], '/');
2023 $q['page'] = absint($q['page']);
2026 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2027 if ( isset($q['no_found_rows']) )
2028 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2030 $q['no_found_rows'] = false;
2032 switch ( $q['fields'] ) {
2034 $fields = "$wpdb->posts.ID";
2037 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2040 $fields = "$wpdb->posts.*";
2043 // If a month is specified in the querystring, load that month
2045 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
2046 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2047 if ( strlen($q['m']) > 5 )
2048 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2049 if ( strlen($q['m']) > 7 )
2050 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2051 if ( strlen($q['m']) > 9 )
2052 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2053 if ( strlen($q['m']) > 11 )
2054 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2055 if ( strlen($q['m']) > 13 )
2056 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2059 if ( '' !== $q['hour'] )
2060 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
2062 if ( '' !== $q['minute'] )
2063 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
2065 if ( '' !== $q['second'] )
2066 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
2069 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
2071 if ( $q['monthnum'] )
2072 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
2075 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
2077 // If we've got a post_type AND its not "any" post_type.
2078 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2079 foreach ( (array)$q['post_type'] as $_post_type ) {
2080 $ptype_obj = get_post_type_object($_post_type);
2081 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2084 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2085 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2086 $q['name'] = $q[ $ptype_obj->query_var ];
2088 // Hierarchical post_types will operate through the
2089 $q['pagename'] = $q[ $ptype_obj->query_var ];
2093 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2099 if ( '' != $q['name'] ) {
2100 $q['name'] = sanitize_title_for_query( $q['name'] );
2101 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2102 } elseif ( '' != $q['pagename'] ) {
2103 if ( isset($this->queried_object_id) ) {
2104 $reqpage = $this->queried_object_id;
2106 if ( 'page' != $q['post_type'] ) {
2107 foreach ( (array)$q['post_type'] as $_post_type ) {
2108 $ptype_obj = get_post_type_object($_post_type);
2109 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2112 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2118 $reqpage = get_page_by_path($q['pagename']);
2120 if ( !empty($reqpage) )
2121 $reqpage = $reqpage->ID;
2126 $page_for_posts = get_option('page_for_posts');
2127 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2128 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2129 $q['name'] = $q['pagename'];
2130 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2131 $reqpage_obj = get_page($reqpage);
2132 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2133 $this->is_attachment = true;
2134 $post_type = $q['post_type'] = 'attachment';
2135 $this->is_page = true;
2136 $q['attachment_id'] = $reqpage;
2139 } elseif ( '' != $q['attachment'] ) {
2140 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2141 $q['name'] = $q['attachment'];
2142 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2146 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
2148 if ( intval($q['comments_popup']) )
2149 $q['p'] = absint($q['comments_popup']);
2151 // If an attachment is requested by number, let it supersede any post number.
2152 if ( $q['attachment_id'] )
2153 $q['p'] = absint($q['attachment_id']);
2155 // If a post number is specified, load that post
2157 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2158 } elseif ( $q['post__in'] ) {
2159 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2160 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2161 } elseif ( $q['post__not_in'] ) {
2162 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2163 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2166 if ( is_numeric($q['post_parent']) )
2167 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2169 if ( $q['page_id'] ) {
2170 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2171 $q['p'] = $q['page_id'];
2172 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2176 // If a search pattern is specified, load the posts that match
2177 if ( !empty($q['s']) ) {
2178 // added slashes screw with quote grouping when done early, so done later
2179 $q['s'] = stripslashes($q['s']);
2180 if ( !empty($q['sentence']) ) {
2181 $q['search_terms'] = array($q['s']);
2183 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
2184 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
2186 $n = !empty($q['exact']) ? '' : '%';
2188 foreach( (array) $q['search_terms'] as $term ) {
2189 $term = esc_sql( like_escape( $term ) );
2190 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
2191 $searchand = ' AND ';
2194 if ( !empty($search) ) {
2195 $search = " AND ({$search}) ";
2196 if ( !is_user_logged_in() )
2197 $search .= " AND ($wpdb->posts.post_password = '') ";
2201 // Allow plugins to contextually add/remove/modify the search section of the database query
2202 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
2205 if ( !$this->is_singular ) {
2206 $this->parse_tax_query( $q );
2208 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2210 $join .= $clauses['join'];
2211 $where .= $clauses['where'];
2214 if ( $this->is_tax ) {
2215 if ( empty($post_type) ) {
2217 $post_status_join = true;
2218 } elseif ( in_array('attachment', (array) $post_type) ) {
2219 $post_status_join = true;
2224 if ( !empty($this->tax_query->queries) ) {
2225 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2226 if ( !empty( $tax_query_in_and ) ) {
2227 if ( !isset( $q['taxonomy'] ) ) {
2228 foreach ( $tax_query_in_and as $a_tax_query ) {
2229 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2230 $q['taxonomy'] = $a_tax_query['taxonomy'];
2231 if ( 'slug' == $a_tax_query['field'] )
2232 $q['term'] = $a_tax_query['terms'][0];
2234 $q['term_id'] = $a_tax_query['terms'][0];
2241 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2242 if ( !empty( $cat_query ) ) {
2243 $cat_query = reset( $cat_query );
2244 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2246 $this->set( 'cat', $the_cat->term_id );
2247 $this->set( 'category_name', $the_cat->slug );
2251 unset( $cat_query );
2253 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2254 if ( !empty( $tag_query ) ) {
2255 $tag_query = reset( $tag_query );
2256 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2258 $this->set( 'tag_id', $the_tag->term_id );
2262 unset( $tag_query );
2266 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2267 $groupby = "{$wpdb->posts}.ID";
2270 // Author/user stuff
2272 if ( empty($q['author']) || ($q['author'] == '0') ) {
2275 $q['author'] = (string)urldecode($q['author']);
2276 $q['author'] = addslashes_gpc($q['author']);
2277 if ( strpos($q['author'], '-') !== false ) {
2280 $q['author'] = explode('-', $q['author']);
2281 $q['author'] = (string)absint($q['author'][1]);
2286 $author_array = preg_split('/[,\s]+/', $q['author']);
2287 $_author_array = array();
2288 foreach ( $author_array as $key => $_author )
2289 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
2290 $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
2291 unset($author_array, $_author_array);
2294 // Author stuff for nice URLs
2296 if ( '' != $q['author_name'] ) {
2297 if ( strpos($q['author_name'], '/') !== false ) {
2298 $q['author_name'] = explode('/', $q['author_name']);
2299 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2300 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2302 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2305 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2306 $q['author'] = get_user_by('slug', $q['author_name']);
2308 $q['author'] = $q['author']->ID;
2309 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2312 // MIME-Type stuff for attachment browsing
2314 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2315 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2317 $where .= $search . $whichauthor . $whichmimetype;
2319 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2320 $q['order'] = 'DESC';
2323 if ( empty($q['orderby']) ) {
2324 $orderby = "$wpdb->posts.post_date " . $q['order'];
2325 } elseif ( 'none' == $q['orderby'] ) {
2328 // Used to filter values
2329 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2330 if ( !empty($q['meta_key']) ) {
2331 $allowed_keys[] = $q['meta_key'];
2332 $allowed_keys[] = 'meta_value';
2333 $allowed_keys[] = 'meta_value_num';
2335 $q['orderby'] = urldecode($q['orderby']);
2336 $q['orderby'] = addslashes_gpc($q['orderby']);
2338 $orderby_array = array();
2339 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2340 // Only allow certain values for safety
2341 if ( ! in_array($orderby, $allowed_keys) )
2344 switch ( $orderby ) {
2346 $orderby = "$wpdb->posts.menu_order";
2349 $orderby = "$wpdb->posts.ID";
2352 $orderby = 'RAND()';
2354 case $q['meta_key']:
2356 $orderby = "$wpdb->postmeta.meta_value";
2358 case 'meta_value_num':
2359 $orderby = "$wpdb->postmeta.meta_value+0";
2361 case 'comment_count':
2362 $orderby = "$wpdb->posts.comment_count";
2365 $orderby = "$wpdb->posts.post_" . $orderby;
2368 $orderby_array[] = $orderby;
2370 $orderby = implode( ',', $orderby_array );
2372 if ( empty( $orderby ) )
2373 $orderby = "$wpdb->posts.post_date ".$q['order'];
2375 $orderby .= " {$q['order']}";
2378 if ( is_array( $post_type ) ) {
2379 $post_type_cap = 'multiple_post_type';
2381 $post_type_object = get_post_type_object( $post_type );
2382 if ( empty( $post_type_object ) )
2383 $post_type_cap = $post_type;
2386 if ( 'any' == $post_type ) {
2387 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2388 if ( ! empty( $in_search_post_types ) )
2389 $where .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
2390 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2391 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2392 } elseif ( ! empty( $post_type ) ) {
2393 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2394 $post_type_object = get_post_type_object ( $post_type );
2395 } elseif ( $this->is_attachment ) {
2396 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2397 $post_type_object = get_post_type_object ( 'attachment' );
2398 } elseif ( $this->is_page ) {
2399 $where .= " AND $wpdb->posts.post_type = 'page'";
2400 $post_type_object = get_post_type_object ( 'page' );
2402 $where .= " AND $wpdb->posts.post_type = 'post'";
2403 $post_type_object = get_post_type_object ( 'post' );
2406 if ( ! empty( $post_type_object ) ) {
2407 $edit_cap = $post_type_object->cap->edit_post;
2408 $read_cap = $post_type_object->cap->read_post;
2409 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2410 $read_private_cap = $post_type_object->cap->read_private_posts;
2412 $edit_cap = 'edit_' . $post_type_cap;
2413 $read_cap = 'read_' . $post_type_cap;
2414 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2415 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2418 if ( ! empty( $q['post_status'] ) ) {
2419 $statuswheres = array();
2420 $q_status = $q['post_status'];
2421 if ( ! is_array( $q_status ) )
2422 $q_status = explode(',', $q_status);
2423 $r_status = array();
2424 $p_status = array();
2425 $e_status = array();
2426 if ( in_array('any', $q_status) ) {
2427 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2428 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2430 foreach ( get_post_stati() as $status ) {
2431 if ( in_array( $status, $q_status ) ) {
2432 if ( 'private' == $status )
2433 $p_status[] = "$wpdb->posts.post_status = '$status'";
2435 $r_status[] = "$wpdb->posts.post_status = '$status'";
2440 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2441 $r_status = array_merge($r_status, $p_status);
2445 if ( !empty($e_status) ) {
2446 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2448 if ( !empty($r_status) ) {
2449 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2450 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2452 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2454 if ( !empty($p_status) ) {
2455 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2456 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2458 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2460 if ( $post_status_join ) {
2461 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2462 foreach ( $statuswheres as $index => $statuswhere )
2463 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2465 foreach ( $statuswheres as $statuswhere )
2466 $where .= " AND $statuswhere";
2467 } elseif ( !$this->is_singular ) {
2468 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2470 // Add public states.
2471 $public_states = get_post_stati( array('public' => true) );
2472 foreach ( (array) $public_states as $state ) {
2473 if ( 'publish' == $state ) // Publish is hard-coded above.
2475 $where .= " OR $wpdb->posts.post_status = '$state'";
2478 if ( $this->is_admin ) {
2479 // Add protected states that should show in the admin all list.
2480 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2481 foreach ( (array) $admin_all_states as $state )
2482 $where .= " OR $wpdb->posts.post_status = '$state'";
2485 if ( is_user_logged_in() ) {
2486 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2487 $private_states = get_post_stati( array('private' => true) );
2488 foreach ( (array) $private_states as $state )
2489 $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = '$state'";
2495 if ( !empty( $this->meta_query->queries ) ) {
2496 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2497 $join .= $clauses['join'];
2498 $where .= $clauses['where'];
2501 // Apply filters on where and join prior to paging so that any
2502 // manipulations to them are reflected in the paging by day queries.
2503 if ( !$q['suppress_filters'] ) {
2504 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2505 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2509 if ( empty($q['nopaging']) && !$this->is_singular ) {
2510 $page = absint($q['paged']);
2514 if ( empty($q['offset']) ) {
2515 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2516 } else { // we're ignoring $page and using 'offset'
2517 $q['offset'] = absint($q['offset']);
2518 $pgstrt = $q['offset'] . ', ';
2520 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2524 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2525 if ( $this->is_archive || $this->is_search ) {
2526 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2527 $cwhere = "WHERE comment_approved = '1' $where";
2528 $cgroupby = "$wpdb->comments.comment_id";
2529 } else { // Other non singular e.g. front
2530 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2531 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2535 if ( !$q['suppress_filters'] ) {
2536 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2537 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2538 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2539 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2540 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2542 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2543 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2545 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2546 $this->comment_count = count($this->comments);
2548 $post_ids = array();
2550 foreach ( $this->comments as $comment )
2551 $post_ids[] = (int) $comment->comment_post_ID;
2553 $post_ids = join(',', $post_ids);
2556 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2561 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2563 // Apply post-paging filters on where and join. Only plugins that
2564 // manipulate paging queries should use these hooks.
2565 if ( !$q['suppress_filters'] ) {
2566 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2567 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2568 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2569 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2570 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2571 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2572 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2574 // Filter all clauses at once, for convenience
2575 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2576 foreach ( $pieces as $piece )
2577 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2580 // Announce current selection parameters. For use by caching plugins.
2581 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2583 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2584 if ( !$q['suppress_filters'] ) {
2585 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2586 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2587 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2588 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2589 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2590 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2591 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2593 // Filter all clauses at once, for convenience
2594 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2595 foreach ( $pieces as $piece )
2596 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2599 if ( ! empty($groupby) )
2600 $groupby = 'GROUP BY ' . $groupby;
2601 if ( !empty( $orderby ) )
2602 $orderby = 'ORDER BY ' . $orderby;
2605 if ( !$q['no_found_rows'] && !empty($limits) )
2606 $found_rows = 'SQL_CALC_FOUND_ROWS';
2608 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2610 if ( !$q['suppress_filters'] ) {
2611 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2614 if ( 'ids' == $q['fields'] ) {
2615 $this->posts = $wpdb->get_col($this->request);
2617 return $this->posts;
2620 if ( 'id=>parent' == $q['fields'] ) {
2621 $this->posts = $wpdb->get_results($this->request);
2624 foreach ( $this->posts as $post )
2625 $r[ $post->ID ] = $post->post_parent;
2630 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2631 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2633 if ( $split_the_query ) {
2634 // First get the IDs and then fill in the objects
2636 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2638 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2640 $ids = $wpdb->get_col( $this->request );
2643 $this->set_found_posts( $q, $limits );
2645 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2647 $this->posts = array_map( 'get_post', $ids );
2649 $this->found_posts = $this->max_num_pages = 0;
2650 $this->posts = array();
2653 $this->posts = $wpdb->get_results( $this->request );
2654 $this->set_found_posts( $q, $limits );
2657 // Raw results filter. Prior to status checks.
2658 if ( !$q['suppress_filters'] )
2659 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2661 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2662 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2663 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2664 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2665 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2666 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2667 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2668 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2669 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2670 $this->comments = $wpdb->get_results($comments_request);
2671 $this->comment_count = count($this->comments);
2674 // Check post status to determine if post should be displayed.
2675 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2676 $status = get_post_status($this->posts[0]);
2677 $post_status_obj = get_post_status_object($status);
2678 //$type = get_post_type($this->posts[0]);
2679 if ( !$post_status_obj->public ) {
2680 if ( ! is_user_logged_in() ) {
2681 // User must be logged in to view unpublished posts.
2682 $this->posts = array();
2684 if ( $post_status_obj->protected ) {
2685 // User must have edit permissions on the draft to preview.
2686 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2687 $this->posts = array();
2689 $this->is_preview = true;
2690 if ( 'future' != $status )
2691 $this->posts[0]->post_date = current_time('mysql');
2693 } elseif ( $post_status_obj->private ) {
2694 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2695 $this->posts = array();
2697 $this->posts = array();
2702 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2703 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
2706 // Put sticky posts at the top of the posts array
2707 $sticky_posts = get_option('sticky_posts');
2708 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2709 $num_posts = count($this->posts);
2711 // Loop over posts and relocate stickies to the front.
2712 for ( $i = 0; $i < $num_posts; $i++ ) {
2713 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2714 $sticky_post = $this->posts[$i];
2715 // Remove sticky from current position
2716 array_splice($this->posts, $i, 1);
2717 // Move to front, after other stickies
2718 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2719 // Increment the sticky offset. The next sticky will be placed at this offset.
2721 // Remove post from sticky posts array
2722 $offset = array_search($sticky_post->ID, $sticky_posts);
2723 unset( $sticky_posts[$offset] );
2727 // If any posts have been excluded specifically, Ignore those that are sticky.
2728 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
2729 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
2731 // Fetch sticky posts that weren't in the query results
2732 if ( !empty($sticky_posts) ) {
2733 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2734 // honor post type(s) if not set to any
2735 $stickies_where = '';
2736 if ( 'any' != $post_type && '' != $post_type ) {
2737 if ( is_array( $post_type ) ) {
2738 $post_types = join( "', '", $post_type );
2740 $post_types = $post_type;
2742 $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
2745 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
2746 foreach ( $stickies as $sticky_post ) {
2747 // Ignore sticky posts the current user cannot read or are not published.
2748 if ( 'publish' != $sticky_post->post_status )
2750 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2756 if ( !$q['suppress_filters'] )
2757 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
2759 $this->post_count = count($this->posts);
2762 foreach ( $this->posts as $i => $post ) {
2763 $this->posts[$i] = sanitize_post( $post, 'raw' );
2766 if ( $q['cache_results'] )
2767 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
2769 if ( $this->post_count > 0 ) {
2770 $this->post = $this->posts[0];
2773 return $this->posts;
2776 function set_found_posts( $q, $limits ) {
2779 if ( $q['no_found_rows'] || empty( $limits ) )
2782 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
2783 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
2785 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
2789 * Set up the next post and iterate current post index.
2794 * @return object Next post.
2796 function next_post() {
2798 $this->current_post++;
2800 $this->post = $this->posts[$this->current_post];
2805 * Sets up the current post.
2807 * Retrieves the next post, sets up the post, sets the 'in the loop'
2813 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2815 function the_post() {
2817 $this->in_the_loop = true;
2819 if ( $this->current_post == -1 ) // loop has just started
2820 do_action_ref_array('loop_start', array(&$this));
2822 $post = $this->next_post();
2823 setup_postdata($post);
2827 * Whether there are more posts available in the loop.
2829 * Calls action 'loop_end', when the loop is complete.
2833 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2835 * @return bool True if posts are available, false if end of loop.
2837 function have_posts() {
2838 if ( $this->current_post + 1 < $this->post_count ) {
2840 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
2841 do_action_ref_array('loop_end', array(&$this));
2842 // Do some cleaning up after the loop
2843 $this->rewind_posts();
2846 $this->in_the_loop = false;
2851 * Rewind the posts and reset post index.
2856 function rewind_posts() {
2857 $this->current_post = -1;
2858 if ( $this->post_count > 0 ) {
2859 $this->post = $this->posts[0];
2864 * Iterate current comment index and return comment object.
2869 * @return object Comment object.
2871 function next_comment() {
2872 $this->current_comment++;
2874 $this->comment = $this->comments[$this->current_comment];
2875 return $this->comment;
2879 * Sets up the current comment.
2883 * @global object $comment Current comment.
2884 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2886 function the_comment() {
2889 $comment = $this->next_comment();
2891 if ( $this->current_comment == 0 ) {
2892 do_action('comment_loop_start');
2897 * Whether there are more comments available.
2899 * Automatically rewinds comments when finished.
2904 * @return bool True, if more comments. False, if no more posts.
2906 function have_comments() {
2907 if ( $this->current_comment + 1 < $this->comment_count ) {
2909 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
2910 $this->rewind_comments();
2917 * Rewind the comments, resets the comment index and comment to first.
2922 function rewind_comments() {
2923 $this->current_comment = -1;
2924 if ( $this->comment_count > 0 ) {
2925 $this->comment = $this->comments[0];
2930 * Sets up the WordPress query by parsing query string.
2935 * @param string $query URL query string.
2936 * @return array List of posts.
2938 function &query( $query ) {
2940 $this->query = $this->query_vars = wp_parse_args( $query );
2941 return $this->get_posts();
2945 * Retrieve queried object.
2947 * If queried object is not set, then the queried object will be set from
2948 * the category, tag, taxonomy, posts page, single post, page, or author
2949 * query variable. After it is set up, it will be returned.
2956 function get_queried_object() {
2957 if ( isset($this->queried_object) )
2958 return $this->queried_object;
2960 $this->queried_object = null;
2961 $this->queried_object_id = 0;
2963 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
2964 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2966 $query = reset( $tax_query_in_and );
2968 if ( 'term_id' == $query['field'] )
2969 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
2971 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
2973 if ( $term && ! is_wp_error($term) ) {
2974 $this->queried_object = $term;
2975 $this->queried_object_id = (int) $term->term_id;
2977 if ( $this->is_category )
2978 _make_cat_compat( $this->queried_object );
2980 } elseif ( $this->is_post_type_archive ) {
2981 $this->queried_object = get_post_type_object( $this->get('post_type') );
2982 } elseif ( $this->is_posts_page ) {
2983 $page_for_posts = get_option('page_for_posts');
2984 $this->queried_object = get_page( $page_for_posts );
2985 $this->queried_object_id = (int) $this->queried_object->ID;
2986 } elseif ( $this->is_singular && !is_null($this->post) ) {
2987 $this->queried_object = $this->post;
2988 $this->queried_object_id = (int) $this->post->ID;
2989 } elseif ( $this->is_author ) {
2990 $this->queried_object_id = (int) $this->get('author');
2991 $this->queried_object = get_userdata( $this->queried_object_id );
2994 return $this->queried_object;
2998 * Retrieve ID of the current queried object.
3005 function get_queried_object_id() {
3006 $this->get_queried_object();
3008 if ( isset($this->queried_object_id) ) {
3009 return $this->queried_object_id;
3018 * Sets up the WordPress query, if parameter is not empty.
3023 * @param string $query URL query string.
3026 function __construct($query = '') {
3027 if ( ! empty($query) ) {
3028 $this->query($query);
3033 * Is the query for an archive page?
3035 * Month, Year, Category, Author, Post Type archive...
3041 function is_archive() {
3042 return (bool) $this->is_archive;
3046 * Is the query for a post type archive page?
3050 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3053 function is_post_type_archive( $post_types = '' ) {
3054 if ( empty( $post_types ) || !$this->is_post_type_archive )
3055 return (bool) $this->is_post_type_archive;
3057 $post_type_object = $this->get_queried_object();
3059 return in_array( $post_type_object->name, (array) $post_types );
3063 * Is the query for an attachment page?
3069 function is_attachment() {
3070 return (bool) $this->is_attachment;
3074 * Is the query for an author archive page?
3076 * If the $author parameter is specified, this function will additionally
3077 * check if the query is for one of the authors specified.
3081 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3084 function is_author( $author = '' ) {
3085 if ( !$this->is_author )
3088 if ( empty($author) )
3091 $author_obj = $this->get_queried_object();
3093 $author = (array) $author;
3095 if ( in_array( $author_obj->ID, $author ) )
3097 elseif ( in_array( $author_obj->nickname, $author ) )
3099 elseif ( in_array( $author_obj->user_nicename, $author ) )
3106 * Is the query for a category archive page?
3108 * If the $category parameter is specified, this function will additionally
3109 * check if the query is for one of the categories specified.
3113 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3116 function is_category( $category = '' ) {
3117 if ( !$this->is_category )
3120 if ( empty($category) )
3123 $cat_obj = $this->get_queried_object();
3125 $category = (array) $category;
3127 if ( in_array( $cat_obj->term_id, $category ) )
3129 elseif ( in_array( $cat_obj->name, $category ) )
3131 elseif ( in_array( $cat_obj->slug, $category ) )
3138 * Is the query for a tag archive page?
3140 * If the $tag parameter is specified, this function will additionally
3141 * check if the query is for one of the tags specified.
3145 * @param mixed $slug Optional. Tag slug or array of slugs.
3148 function is_tag( $slug = '' ) {
3149 if ( !$this->is_tag )
3152 if ( empty( $slug ) )
3155 $tag_obj = $this->get_queried_object();
3157 $slug = (array) $slug;
3159 if ( in_array( $tag_obj->slug, $slug ) )
3166 * Is the query for a taxonomy archive page?
3168 * If the $taxonomy parameter is specified, this function will additionally
3169 * check if the query is for that specific $taxonomy.
3171 * If the $term parameter is specified in addition to the $taxonomy parameter,
3172 * this function will additionally check if the query is for one of the terms
3177 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3178 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3181 function is_tax( $taxonomy = '', $term = '' ) {
3182 global $wp_taxonomies;
3184 if ( !$this->is_tax )
3187 if ( empty( $taxonomy ) )
3190 $queried_object = $this->get_queried_object();
3191 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3192 $term_array = (array) $term;
3194 if ( empty( $term ) ) // Only a Taxonomy provided
3195 return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
3197 return isset( $queried_object->term_id ) &&
3198 count( array_intersect(
3199 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3205 * Whether the current URL is within the comments popup window.
3211 function is_comments_popup() {
3212 return (bool) $this->is_comments_popup;
3216 * Is the query for a date archive?
3222 function is_date() {
3223 return (bool) $this->is_date;
3227 * Is the query for a day archive?
3234 return (bool) $this->is_day;
3238 * Is the query for a feed?
3242 * @param string|array $feeds Optional feed types to check.
3245 function is_feed( $feeds = '' ) {
3246 if ( empty( $feeds ) || ! $this->is_feed )
3247 return (bool) $this->is_feed;
3248 $qv = $this->get( 'feed' );
3249 if ( 'feed' == $qv )
3250 $qv = get_default_feed();
3251 return in_array( $qv, (array) $feeds );
3255 * Is the query for a comments feed?
3261 function is_comment_feed() {
3262 return (bool) $this->is_comment_feed;
3266 * Is the query for the front page of the site?
3268 * This is for what is displayed at your site's main URL.
3270 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3272 * If you set a static page for the front page of your site, this function will return
3273 * true when viewing that page.
3275 * Otherwise the same as @see WP_Query::is_home()
3279 * @uses get_option()
3281 * @return bool True, if front of site.
3283 function is_front_page() {
3285 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3287 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3294 * Is the query for the blog homepage?
3296 * This is the page which shows the time based blog content of your site.
3298 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3300 * If you set a static page for the front page of your site, this function will return
3301 * true only on the page you set as the "Posts page".
3303 * @see WP_Query::is_front_page()
3307 * @return bool True if blog view homepage.
3309 function is_home() {
3310 return (bool) $this->is_home;
3314 * Is the query for a month archive?
3320 function is_month() {
3321 return (bool) $this->is_month;
3325 * Is the query for a single page?
3327 * If the $page parameter is specified, this function will additionally
3328 * check if the query is for one of the pages specified.
3330 * @see WP_Query::is_single()
3331 * @see WP_Query::is_singular()
3335 * @param mixed $page Page ID, title, slug, or array of such.
3338 function is_page( $page = '' ) {
3339 if ( !$this->is_page )
3342 if ( empty( $page ) )
3345 $page_obj = $this->get_queried_object();
3347 $page = (array) $page;
3349 if ( in_array( $page_obj->ID, $page ) )
3351 elseif ( in_array( $page_obj->post_title, $page ) )
3353 else if ( in_array( $page_obj->post_name, $page ) )
3360 * Is the query for paged result and not for the first page?
3366 function is_paged() {
3367 return (bool) $this->is_paged;
3371 * Is the query for a post or page preview?
3377 function is_preview() {
3378 return (bool) $this->is_preview;
3382 * Is the query for the robots file?
3388 function is_robots() {
3389 return (bool) $this->is_robots;
3393 * Is the query for a search?
3399 function is_search() {
3400 return (bool) $this->is_search;
3404 * Is the query for a single post?
3406 * Works for any post type, except attachments and pages
3408 * If the $post parameter is specified, this function will additionally
3409 * check if the query is for one of the Posts specified.
3411 * @see WP_Query::is_page()
3412 * @see WP_Query::is_singular()
3416 * @param mixed $post Post ID, title, slug, or array of such.
3419 function is_single( $post = '' ) {
3420 if ( !$this->is_single )
3426 $post_obj = $this->get_queried_object();
3428 $post = (array) $post;
3430 if ( in_array( $post_obj->ID, $post ) )
3432 elseif ( in_array( $post_obj->post_title, $post ) )
3434 elseif ( in_array( $post_obj->post_name, $post ) )
3441 * Is the query for a single post of any post type (post, attachment, page, ... )?
3443 * If the $post_types parameter is specified, this function will additionally
3444 * check if the query is for one of the Posts Types specified.
3446 * @see WP_Query::is_page()
3447 * @see WP_Query::is_single()
3451 * @param mixed $post_types Optional. Post Type or array of Post Types
3454 function is_singular( $post_types = '' ) {
3455 if ( empty( $post_types ) || !$this->is_singular )
3456 return (bool) $this->is_singular;
3458 $post_obj = $this->get_queried_object();
3460 return in_array( $post_obj->post_type, (array) $post_types );
3464 * Is the query for a specific time?
3470 function is_time() {
3471 return (bool) $this->is_time;
3475 * Is the query for a trackback endpoint call?
3481 function is_trackback() {
3482 return (bool) $this->is_trackback;
3486 * Is the query for a specific year?
3492 function is_year() {
3493 return (bool) $this->is_year;
3497 * Is the query a 404 (returns no results)?
3504 return (bool) $this->is_404;
3508 * Is the query the main query?
3514 function is_main_query() {
3515 global $wp_the_query;
3516 return $wp_the_query === $this;
3521 * Redirect old slugs to the correct permalink.
3523 * Attempts to find the current slug from the past slugs.
3529 * @return null If no link is found, null is returned.
3531 function wp_old_slug_redirect() {
3533 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3536 // Guess the current post_type based on the query vars.
3537 if ( get_query_var('post_type') )
3538 $post_type = get_query_var('post_type');
3539 elseif ( !empty($wp_query->query_vars['pagename']) )
3540 $post_type = 'page';
3542 $post_type = 'post';
3544 if ( is_array( $post_type ) ) {
3545 if ( count( $post_type ) > 1 )
3547 $post_type = array_shift( $post_type );
3550 // Do not attempt redirect for hierarchical post types
3551 if ( is_post_type_hierarchical( $post_type ) )
3554 $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
3556 // if year, monthnum, or day have been specified, make our query more precise
3557 // just in case there are multiple identical _wp_old_slug values
3558 if ( '' != $wp_query->query_vars['year'] )
3559 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3560 if ( '' != $wp_query->query_vars['monthnum'] )
3561 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3562 if ( '' != $wp_query->query_vars['day'] )
3563 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3565 $id = (int) $wpdb->get_var($query);
3570 $link = get_permalink($id);
3575 wp_redirect( $link, 301 ); // Permanent redirect
3581 * Set up global post data.
3585 * @param object $post Post data.
3586 * @uses do_action_ref_array() Calls 'the_post'
3587 * @return bool True when finished.
3589 function setup_postdata($post) {
3590 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3592 $id = (int) $post->ID;
3594 $authordata = get_userdata($post->post_author);
3596 $currentday = mysql2date('d.m.y', $post->post_date, false);
3597 $currentmonth = mysql2date('m', $post->post_date, false);
3599 $page = get_query_var('page');
3602 if ( is_single() || is_page() || is_feed() )
3604 $content = $post->post_content;
3605 if ( strpos( $content, '<!--nextpage-->' ) ) {
3609 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
3610 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
3611 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
3612 $pages = explode('<!--nextpage-->', $content);
3613 $numpages = count($pages);
3615 $pages = array( $post->post_content );
3619 do_action_ref_array('the_post', array(&$post));