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);
32 * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
34 * @uses WP_Query::get_queried_object
41 function get_queried_object() {
43 return $wp_query->get_queried_object();
47 * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
49 * @uses WP_Query::get_queried_object_id()
56 function get_queried_object_id() {
58 return $wp_query->get_queried_object_id();
64 * @see WP_Query::set()
68 * @param string $var Query variable key.
72 function set_query_var($var, $value) {
75 return $wp_query->set($var, $value);
79 * Set up The Loop with query parameters.
81 * This will override the current WordPress Loop and shouldn't be used more than
82 * once. This must not be used within the WordPress Loop.
87 * @param string $query
88 * @return array List of posts
90 function &query_posts($query) {
91 unset($GLOBALS['wp_query']);
92 $GLOBALS['wp_query'] =& new WP_Query();
93 return $GLOBALS['wp_query']->query($query);
97 * Destroy the previous query and set up a new query.
99 * This should be used after {@link query_posts()} and before another {@link
100 * query_posts()}. This will remove obscure bugs that occur when the previous
101 * wp_query object is not destroyed properly before another is set up.
106 function wp_reset_query() {
107 unset($GLOBALS['wp_query']);
108 $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
113 * After looping through a separate query, this function restores
114 * the $post global to the current post in the main query
119 function wp_reset_postdata() {
121 if ( !empty($wp_query->post) ) {
122 $GLOBALS['post'] = $wp_query->post;
123 setup_postdata($wp_query->post);
132 * Is the query for an archive page?
134 * Month, Year, Category, Author, Post Type archive...
136 * @see WP_Query::is_archive()
142 function is_archive() {
145 if ( ! isset( $wp_query ) ) {
146 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
150 return $wp_query->is_archive();
154 * Is the query for a post type archive page?
156 * @see WP_Query::is_post_type_archive()
160 * @param mixed $post_types Optional. Post type or array of posts types to check against.
163 function is_post_type_archive( $post_types = '' ) {
166 if ( ! isset( $wp_query ) ) {
167 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
171 return $wp_query->is_post_type_archive( $post_types );
175 * Is the query for an attachment page?
177 * @see WP_Query::is_attachment()
183 function is_attachment() {
186 if ( ! isset( $wp_query ) ) {
187 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
191 return $wp_query->is_attachment();
195 * Is the query for an author archive page?
197 * If the $author parameter is specified, this function will additionally
198 * check if the query is for one of the authors specified.
200 * @see WP_Query::is_author()
204 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
207 function is_author( $author = '' ) {
210 if ( ! isset( $wp_query ) ) {
211 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
215 return $wp_query->is_author( $author );
219 * Is the query for a category archive page?
221 * If the $category parameter is specified, this function will additionally
222 * check if the query is for one of the categories specified.
224 * @see WP_Query::is_category()
228 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
231 function is_category( $category = '' ) {
234 if ( ! isset( $wp_query ) ) {
235 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
239 return $wp_query->is_category( $category );
243 * Is the query for a tag archive page?
245 * If the $tag parameter is specified, this function will additionally
246 * check if the query is for one of the tags specified.
248 * @see WP_Query::is_tag()
252 * @param mixed $slug Optional. Tag slug or array of slugs.
255 function is_tag( $slug = '' ) {
258 if ( ! isset( $wp_query ) ) {
259 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
263 return $wp_query->is_tag( $slug );
267 * Is the query for a taxonomy archive page?
269 * If the $taxonomy parameter is specified, this function will additionally
270 * check if the query is for that specific $taxonomy.
272 * If the $term parameter is specified in addition to the $taxonomy parameter,
273 * this function will additionally check if the query is for one of the terms
276 * @see WP_Query::is_tax()
280 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
281 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
284 function is_tax( $taxonomy = '', $term = '' ) {
287 if ( ! isset( $wp_query ) ) {
288 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
292 return $wp_query->is_tax( $taxonomy, $term );
296 * Whether the current URL is within the comments popup window.
298 * @see WP_Query::is_comments_popup()
304 function is_comments_popup() {
307 if ( ! isset( $wp_query ) ) {
308 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
312 return $wp_query->is_comments_popup();
316 * Is the query for a date archive?
318 * @see WP_Query::is_date()
327 if ( ! isset( $wp_query ) ) {
328 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
332 return $wp_query->is_date();
336 * Is the query for a day archive?
338 * @see WP_Query::is_day()
347 if ( ! isset( $wp_query ) ) {
348 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
352 return $wp_query->is_day();
356 * Is the query for a feed?
358 * @see WP_Query::is_feed()
362 * @param string|array $feeds Optional feed types to check.
365 function is_feed( $feeds = '' ) {
368 if ( ! isset( $wp_query ) ) {
369 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
373 return $wp_query->is_feed( $feeds );
377 * Is the query for a comments feed?
379 * @see WP_Query::is_comments_feed()
385 function is_comment_feed() {
388 if ( ! isset( $wp_query ) ) {
389 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
393 return $wp_query->is_comment_feed();
397 * Is the query for the front page of the site?
399 * This is for what is displayed at your site's main URL.
401 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
403 * If you set a static page for the front page of your site, this function will return
404 * true when viewing that page.
406 * Otherwise the same as @see is_home()
408 * @see WP_Query::is_front_page()
413 * @return bool True, if front of site.
415 function is_front_page() {
418 if ( ! isset( $wp_query ) ) {
419 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
423 return $wp_query->is_front_page();
427 * Is the query for the blog homepage?
429 * This is the page which shows the time based blog content of your site.
431 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
433 * If you set a static page for the front page of your site, this function will return
434 * true only on the page you set as the "Posts page".
436 * @see is_front_page()
438 * @see WP_Query::is_home()
442 * @return bool True if blog view homepage.
447 if ( ! isset( $wp_query ) ) {
448 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
452 return $wp_query->is_home();
456 * Is the query for a month archive?
458 * @see WP_Query::is_month()
464 function is_month() {
467 if ( ! isset( $wp_query ) ) {
468 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
472 return $wp_query->is_month();
476 * Is the query for a single page?
478 * If the $page parameter is specified, this function will additionally
479 * check if the query is for one of the pages specified.
484 * @see WP_Query::is_page()
488 * @param mixed $page Page ID, title, slug, or array of such.
491 function is_page( $page = '' ) {
494 if ( ! isset( $wp_query ) ) {
495 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
499 return $wp_query->is_page( $page );
503 * Is the query for paged result and not for the first page?
505 * @see WP_Query::is_paged()
511 function is_paged() {
514 if ( ! isset( $wp_query ) ) {
515 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
519 return $wp_query->is_paged();
523 * Is the query for a post or page preview?
525 * @see WP_Query::is_preview()
531 function is_preview() {
534 if ( ! isset( $wp_query ) ) {
535 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
539 return $wp_query->is_preview();
543 * Is the query for the robots file?
545 * @see WP_Query::is_robots()
551 function is_robots() {
554 if ( ! isset( $wp_query ) ) {
555 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
559 return $wp_query->is_robots();
563 * Is the query for a search?
565 * @see WP_Query::is_search()
571 function is_search() {
574 if ( ! isset( $wp_query ) ) {
575 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
579 return $wp_query->is_search();
583 * Is the query for a single post?
585 * Works for any post type, except attachments and pages
587 * If the $post parameter is specified, this function will additionally
588 * check if the query is for one of the Posts specified.
593 * @see WP_Query::is_single()
597 * @param mixed $post Post ID, title, slug, or array of such.
600 function is_single( $post = '' ) {
603 if ( ! isset( $wp_query ) ) {
604 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
608 return $wp_query->is_single( $post );
612 * Is the query for a single post of any post type (post, attachment, page, ... )?
614 * If the $post_types parameter is specified, this function will additionally
615 * check if the query is for one of the Posts Types specified.
620 * @see WP_Query::is_singular()
624 * @param mixed $post_types Optional. Post Type or array of Post Types
627 function is_singular( $post_types = '' ) {
630 if ( ! isset( $wp_query ) ) {
631 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
635 return $wp_query->is_singular( $post_types );
639 * Is the query for a specific time?
641 * @see WP_Query::is_time()
650 if ( ! isset( $wp_query ) ) {
651 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
655 return $wp_query->is_time();
659 * Is the query for a trackback endpoint call?
661 * @see WP_Query::is_trackback()
667 function is_trackback() {
670 if ( ! isset( $wp_query ) ) {
671 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
675 return $wp_query->is_trackback();
679 * Is the query for a specific year?
681 * @see WP_Query::is_year()
690 if ( ! isset( $wp_query ) ) {
691 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
695 return $wp_query->is_year();
699 * Is the query a 404 (returns no results)?
701 * @see WP_Query::is_404()
710 if ( ! isset( $wp_query ) ) {
711 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
715 return $wp_query->is_404();
719 * The Loop. Post loop control.
723 * Whether current WordPress query has results to loop over.
725 * @see WP_Query::have_posts()
731 function have_posts() {
734 return $wp_query->have_posts();
738 * Whether the caller is in the Loop.
743 * @return bool True if caller is within loop, false if loop hasn't started or ended.
745 function in_the_loop() {
748 return $wp_query->in_the_loop;
752 * Rewind the loop posts.
754 * @see WP_Query::rewind_posts()
760 function rewind_posts() {
763 return $wp_query->rewind_posts();
767 * Iterate the post index in the loop.
769 * @see WP_Query::the_post()
773 function the_post() {
776 $wp_query->the_post();
784 * Whether there are comments to loop over.
786 * @see WP_Query::have_comments()
792 function have_comments() {
794 return $wp_query->have_comments();
798 * Iterate comment index in the comment loop.
800 * @see WP_Query::the_comment()
806 function the_comment() {
808 return $wp_query->the_comment();
816 * The WordPress Query class.
818 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
825 * Query vars set by the user
834 * Query vars, after parsing
840 var $query_vars = array();
843 * Taxonomy query, as passed to get_tax_sql()
847 * @var object WP_Tax_Query
852 * Holds the data for a single object that is queried.
854 * Holds the contents of a post, page, category, attachment.
863 * The ID of the queried object.
869 var $queried_object_id;
872 * Get post database query.
890 * The amount of posts for the current query.
899 * Index of the current item in the loop.
905 var $current_post = -1;
908 * Whether the loop has started and the caller is in the loop.
914 var $in_the_loop = false;
917 * The current post ID.
926 * The list of comments for current post.
935 * The amount of comments for the posts.
941 var $comment_count = 0;
944 * The index of the comment in the comment loop.
950 var $current_comment = -1;
953 * Current comment ID.
962 * Amount of posts if limit clause was not used.
968 var $found_posts = 0;
971 * The amount of pages.
977 var $max_num_pages = 0;
980 * The amount of comment pages.
986 var $max_num_comment_pages = 0;
989 * Set if query is single post.
995 var $is_single = false;
998 * Set if query is preview of blog.
1004 var $is_preview = false;
1007 * Set if query returns a page.
1013 var $is_page = false;
1016 * Set if query is an archive list.
1022 var $is_archive = false;
1025 * Set if query is part of a date.
1031 var $is_date = false;
1034 * Set if query contains a year.
1040 var $is_year = false;
1043 * Set if query contains a month.
1049 var $is_month = false;
1052 * Set if query contains a day.
1058 var $is_day = false;
1061 * Set if query contains time.
1067 var $is_time = false;
1070 * Set if query contains an author.
1076 var $is_author = false;
1079 * Set if query contains category.
1085 var $is_category = false;
1088 * Set if query contains tag.
1094 var $is_tag = false;
1097 * Set if query contains taxonomy.
1103 var $is_tax = false;
1106 * Set if query was part of a search result.
1112 var $is_search = false;
1115 * Set if query is feed display.
1121 var $is_feed = false;
1124 * Set if query is comment feed display.
1130 var $is_comment_feed = false;
1133 * Set if query is trackback.
1139 var $is_trackback = false;
1142 * Set if query is blog homepage.
1148 var $is_home = false;
1151 * Set if query couldn't found anything.
1157 var $is_404 = false;
1160 * Set if query is within comments popup window.
1166 var $is_comments_popup = false;
1169 * Set if query is paged
1175 var $is_paged = false;
1178 * Set if query is part of administration page.
1184 var $is_admin = false;
1187 * Set if query is an attachment.
1193 var $is_attachment = false;
1196 * Set if is single, is a page, or is an attachment.
1202 var $is_singular = false;
1205 * Set if query is for robots.
1211 var $is_robots = false;
1214 * Set if query contains posts.
1216 * Basically, the homepage if the option isn't set for the static homepage.
1222 var $is_posts_page = false;
1225 * Set if query is for a post type archive.
1231 var $is_post_type_archive = false;
1234 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1235 * whether we have to re-parse because something has changed
1240 var $query_vars_hash = false;
1243 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1244 * via pre_get_posts hooks.
1249 var $query_vars_changed = true;
1252 * Resets query flags to false.
1254 * The query flags are what page info WordPress was able to figure out.
1259 function init_query_flags() {
1260 $this->is_single = false;
1261 $this->is_preview = false;
1262 $this->is_page = false;
1263 $this->is_archive = false;
1264 $this->is_date = false;
1265 $this->is_year = false;
1266 $this->is_month = false;
1267 $this->is_day = false;
1268 $this->is_time = false;
1269 $this->is_author = false;
1270 $this->is_category = false;
1271 $this->is_tag = false;
1272 $this->is_tax = false;
1273 $this->is_search = false;
1274 $this->is_feed = false;
1275 $this->is_comment_feed = false;
1276 $this->is_trackback = false;
1277 $this->is_home = false;
1278 $this->is_404 = false;
1279 $this->is_comments_popup = false;
1280 $this->is_paged = false;
1281 $this->is_admin = false;
1282 $this->is_attachment = false;
1283 $this->is_singular = false;
1284 $this->is_robots = false;
1285 $this->is_posts_page = false;
1286 $this->is_post_type_archive = false;
1290 * Initiates object properties and sets default values.
1296 unset($this->posts);
1297 unset($this->query);
1298 $this->query_vars = array();
1299 unset($this->queried_object);
1300 unset($this->queried_object_id);
1301 $this->post_count = 0;
1302 $this->current_post = -1;
1303 $this->in_the_loop = false;
1304 unset( $this->request );
1305 unset( $this->post );
1306 unset( $this->comments );
1307 unset( $this->comment );
1308 $this->comment_count = 0;
1309 $this->current_comment = -1;
1310 $this->found_posts = 0;
1311 $this->max_num_pages = 0;
1312 $this->max_num_comment_pages = 0;
1314 $this->init_query_flags();
1318 * Reparse the query vars.
1323 function parse_query_vars() {
1324 $this->parse_query();
1328 * Fills in the query variables, which do not exist within the parameter.
1333 * @param array $array Defined query variables.
1334 * @return array Complete query variables with undefined ones filled in empty.
1336 function fill_query_vars($array) {
1374 foreach ( $keys as $key ) {
1375 if ( !isset($array[$key]) )
1379 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1380 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1382 foreach ( $array_keys as $key ) {
1383 if ( !isset($array[$key]) )
1384 $array[$key] = array();
1390 * Parse a query string and set query type booleans.
1395 * @param string|array $query Optional query.
1397 function parse_query( $query = '' ) {
1398 if ( ! empty( $query ) ) {
1400 $this->query = $this->query_vars = wp_parse_args( $query );
1401 } elseif ( ! isset( $this->query ) ) {
1402 $this->query = $this->query_vars;
1405 $this->query_vars = $this->fill_query_vars($this->query_vars);
1406 $qv = &$this->query_vars;
1407 $this->query_vars_changed = true;
1409 if ( ! empty($qv['robots']) )
1410 $this->is_robots = true;
1412 $qv['p'] = absint($qv['p']);
1413 $qv['page_id'] = absint($qv['page_id']);
1414 $qv['year'] = absint($qv['year']);
1415 $qv['monthnum'] = absint($qv['monthnum']);
1416 $qv['day'] = absint($qv['day']);
1417 $qv['w'] = absint($qv['w']);
1418 $qv['m'] = absint($qv['m']);
1419 $qv['paged'] = absint($qv['paged']);
1420 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1421 $qv['pagename'] = trim( $qv['pagename'] );
1422 $qv['name'] = trim( $qv['name'] );
1423 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1424 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1425 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1427 // Compat. Map subpost to attachment.
1428 if ( '' != $qv['subpost'] )
1429 $qv['attachment'] = $qv['subpost'];
1430 if ( '' != $qv['subpost_id'] )
1431 $qv['attachment_id'] = $qv['subpost_id'];
1433 $qv['attachment_id'] = absint($qv['attachment_id']);
1435 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1436 $this->is_single = true;
1437 $this->is_attachment = true;
1438 } elseif ( '' != $qv['name'] ) {
1439 $this->is_single = true;
1440 } elseif ( $qv['p'] ) {
1441 $this->is_single = true;
1442 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1443 // If year, month, day, hour, minute, and second are set, a single
1444 // post is being queried.
1445 $this->is_single = true;
1446 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1447 $this->is_page = true;
1448 $this->is_single = false;
1450 // Look for archive queries. Dates, categories, authors, search, post type archives.
1452 if ( !empty($qv['s']) ) {
1453 $this->is_search = true;
1456 if ( '' !== $qv['second'] ) {
1457 $this->is_time = true;
1458 $this->is_date = true;
1461 if ( '' !== $qv['minute'] ) {
1462 $this->is_time = true;
1463 $this->is_date = true;
1466 if ( '' !== $qv['hour'] ) {
1467 $this->is_time = true;
1468 $this->is_date = true;
1472 if ( ! $this->is_date ) {
1473 $this->is_day = true;
1474 $this->is_date = true;
1478 if ( $qv['monthnum'] ) {
1479 if ( ! $this->is_date ) {
1480 $this->is_month = true;
1481 $this->is_date = true;
1485 if ( $qv['year'] ) {
1486 if ( ! $this->is_date ) {
1487 $this->is_year = true;
1488 $this->is_date = true;
1493 $this->is_date = true;
1494 if ( strlen($qv['m']) > 9 ) {
1495 $this->is_time = true;
1496 } else if ( strlen($qv['m']) > 7 ) {
1497 $this->is_day = true;
1498 } else if ( strlen($qv['m']) > 5 ) {
1499 $this->is_month = true;
1501 $this->is_year = true;
1505 if ( '' != $qv['w'] ) {
1506 $this->is_date = true;
1509 $this->query_vars_hash = false;
1510 $this->parse_tax_query( $qv );
1512 foreach ( $this->tax_query->queries as $tax_query ) {
1513 if ( 'NOT IN' != $tax_query['operator'] ) {
1514 switch ( $tax_query['taxonomy'] ) {
1516 $this->is_category = true;
1519 $this->is_tag = true;
1522 $this->is_tax = true;
1526 unset( $tax_query );
1528 _parse_meta_query( $qv );
1530 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1531 $this->is_author = false;
1533 $this->is_author = true;
1536 if ( '' != $qv['author_name'] )
1537 $this->is_author = true;
1539 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1540 $post_type_obj = get_post_type_object( $qv['post_type'] );
1541 if ( ! empty( $post_type_obj->has_archive ) )
1542 $this->is_post_type_archive = true;
1545 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1546 $this->is_archive = true;
1549 if ( '' != $qv['feed'] )
1550 $this->is_feed = true;
1552 if ( '' != $qv['tb'] )
1553 $this->is_trackback = true;
1555 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1556 $this->is_paged = true;
1558 if ( '' != $qv['comments_popup'] )
1559 $this->is_comments_popup = true;
1561 // if we're previewing inside the write screen
1562 if ( '' != $qv['preview'] )
1563 $this->is_preview = true;
1566 $this->is_admin = true;
1568 if ( false !== strpos($qv['feed'], 'comments-') ) {
1569 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1570 $qv['withcomments'] = 1;
1573 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1575 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1576 $this->is_comment_feed = true;
1578 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 ) )
1579 $this->is_home = true;
1581 // Correct is_* for page_on_front and page_for_posts
1582 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1583 $_query = wp_parse_args($this->query);
1584 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1585 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1586 unset($_query['pagename']);
1587 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1588 $this->is_page = true;
1589 $this->is_home = false;
1590 $qv['page_id'] = get_option('page_on_front');
1591 // Correct <!--nextpage--> for page_on_front
1592 if ( !empty($qv['paged']) ) {
1593 $qv['page'] = $qv['paged'];
1594 unset($qv['paged']);
1599 if ( '' != $qv['pagename'] ) {
1600 $this->queried_object =& get_page_by_path($qv['pagename']);
1601 if ( !empty($this->queried_object) )
1602 $this->queried_object_id = (int) $this->queried_object->ID;
1604 unset($this->queried_object);
1606 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1607 $this->is_page = false;
1608 $this->is_home = true;
1609 $this->is_posts_page = true;
1613 if ( $qv['page_id'] ) {
1614 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1615 $this->is_page = false;
1616 $this->is_home = true;
1617 $this->is_posts_page = true;
1621 if ( !empty($qv['post_type']) ) {
1622 if ( is_array($qv['post_type']) )
1623 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1625 $qv['post_type'] = sanitize_key($qv['post_type']);
1628 if ( ! empty( $qv['post_status'] ) ) {
1629 if ( is_array( $qv['post_status'] ) )
1630 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1632 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1635 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1636 $this->is_comment_feed = false;
1638 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1639 // Done correcting is_* for page_on_front and page_for_posts
1641 if ( '404' == $qv['error'] )
1644 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1645 $this->query_vars_changed = false;
1647 do_action_ref_array('parse_query', array(&$this));
1651 * Parses various taxonomy related query vars.
1656 * @param array &$q The query variables
1658 function parse_tax_query( &$q ) {
1659 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1660 $tax_query = $q['tax_query'];
1662 $tax_query = array();
1665 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1666 $tax_query[] = array(
1667 'taxonomy' => $q['taxonomy'],
1668 'terms' => array( $q['term'] ),
1673 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1674 if ( 'post_tag' == $taxonomy )
1675 continue; // Handled further down in the $q['tag'] block
1677 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1678 $tax_query_defaults = array(
1679 'taxonomy' => $taxonomy,
1683 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1684 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1687 $term = $q[$t->query_var];
1689 if ( strpos($term, '+') !== false ) {
1690 $terms = preg_split( '/[+]+/', $term );
1691 foreach ( $terms as $term ) {
1692 $tax_query[] = array_merge( $tax_query_defaults, array(
1693 'terms' => array( $term )
1697 $tax_query[] = array_merge( $tax_query_defaults, array(
1698 'terms' => preg_split( '/[,]+/', $term )
1705 if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1706 $q['cat'] = ''.urldecode($q['cat']).'';
1707 $q['cat'] = addslashes_gpc($q['cat']);
1708 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1710 $req_cats = array();
1711 foreach ( (array) $cat_array as $cat ) {
1712 $cat = intval($cat);
1717 $q['category__in'][] = $cat;
1718 $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1720 $q['category__not_in'][] = $cat;
1721 $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1724 $q['cat'] = implode(',', $req_cats);
1727 if ( !empty($q['category__in']) ) {
1728 $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
1729 $tax_query[] = array(
1730 'taxonomy' => 'category',
1731 'terms' => $q['category__in'],
1732 'field' => 'term_id',
1733 'include_children' => false
1737 if ( !empty($q['category__not_in']) ) {
1738 $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
1739 $tax_query[] = array(
1740 'taxonomy' => 'category',
1741 'terms' => $q['category__not_in'],
1742 'operator' => 'NOT IN',
1743 'include_children' => false
1747 if ( !empty($q['category__and']) ) {
1748 $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
1749 $tax_query[] = array(
1750 'taxonomy' => 'category',
1751 'terms' => $q['category__and'],
1752 'field' => 'term_id',
1753 'operator' => 'AND',
1754 'include_children' => false
1759 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1760 if ( strpos($q['tag'], ',') !== false ) {
1761 $tags = preg_split('/[,\s]+/', $q['tag']);
1762 foreach ( (array) $tags as $tag ) {
1763 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1764 $q['tag_slug__in'][] = $tag;
1766 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1767 $tags = preg_split('/[+\s]+/', $q['tag']);
1768 foreach ( (array) $tags as $tag ) {
1769 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1770 $q['tag_slug__and'][] = $tag;
1773 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1774 $q['tag_slug__in'][] = $q['tag'];
1778 if ( !empty($q['tag_id']) ) {
1779 $q['tag_id'] = absint( $q['tag_id'] );
1780 $tax_query[] = array(
1781 'taxonomy' => 'post_tag',
1782 'terms' => $q['tag_id']
1786 if ( !empty($q['tag__in']) ) {
1787 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1788 $tax_query[] = array(
1789 'taxonomy' => 'post_tag',
1790 'terms' => $q['tag__in']
1794 if ( !empty($q['tag__not_in']) ) {
1795 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1796 $tax_query[] = array(
1797 'taxonomy' => 'post_tag',
1798 'terms' => $q['tag__not_in'],
1799 'operator' => 'NOT IN'
1803 if ( !empty($q['tag__and']) ) {
1804 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1805 $tax_query[] = array(
1806 'taxonomy' => 'post_tag',
1807 'terms' => $q['tag__and'],
1812 if ( !empty($q['tag_slug__in']) ) {
1813 $q['tag_slug__in'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__in'] ) );
1814 $tax_query[] = array(
1815 'taxonomy' => 'post_tag',
1816 'terms' => $q['tag_slug__in'],
1821 if ( !empty($q['tag_slug__and']) ) {
1822 $q['tag_slug__and'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__and'] ) );
1823 $tax_query[] = array(
1824 'taxonomy' => 'post_tag',
1825 'terms' => $q['tag_slug__and'],
1831 $this->tax_query = new WP_Tax_Query( $tax_query );
1835 * Sets the 404 property and saves whether query is feed.
1840 function set_404() {
1841 $is_feed = $this->is_feed;
1843 $this->init_query_flags();
1844 $this->is_404 = true;
1846 $this->is_feed = $is_feed;
1850 * Retrieve query variable.
1855 * @param string $query_var Query variable key.
1858 function get($query_var) {
1859 if ( isset($this->query_vars[$query_var]) )
1860 return $this->query_vars[$query_var];
1866 * Set query variable.
1871 * @param string $query_var Query variable key.
1872 * @param mixed $value Query variable value.
1874 function set($query_var, $value) {
1875 $this->query_vars[$query_var] = $value;
1879 * Retrieve the posts based on query variables.
1881 * There are a few filters and actions that can be used to modify the post
1886 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1888 * @return array List of posts.
1890 function &get_posts() {
1891 global $wpdb, $user_ID, $_wp_using_ext_object_cache;
1893 $this->parse_query();
1895 do_action_ref_array('pre_get_posts', array(&$this));
1898 $q = &$this->query_vars;
1900 // Fill again in case pre_get_posts unset some vars.
1901 $q = $this->fill_query_vars($q);
1903 // Set a flag if a pre_get_posts hook changed the query vars.
1904 $hash = md5( serialize( $this->query_vars ) );
1905 if ( $hash != $this->query_vars_hash ) {
1906 $this->query_vars_changed = true;
1907 $this->query_vars_hash = $hash;
1911 // First let's clear some variables
1914 $whichmimetype = '';
1921 $post_status_join = false;
1924 if ( isset( $q['caller_get_posts'] ) ) {
1925 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
1926 if ( !isset( $q['ignore_sticky_posts'] ) )
1927 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
1930 if ( !isset( $q['ignore_sticky_posts'] ) )
1931 $q['ignore_sticky_posts'] = false;
1933 if ( !isset($q['suppress_filters']) )
1934 $q['suppress_filters'] = false;
1936 if ( !isset($q['cache_results']) ) {
1937 if ( $_wp_using_ext_object_cache )
1938 $q['cache_results'] = false;
1940 $q['cache_results'] = true;
1943 if ( !isset($q['update_post_term_cache']) )
1944 $q['update_post_term_cache'] = true;
1946 if ( !isset($q['update_post_meta_cache']) )
1947 $q['update_post_meta_cache'] = true;
1949 if ( !isset($q['post_type']) ) {
1950 if ( $this->is_search )
1951 $q['post_type'] = 'any';
1953 $q['post_type'] = '';
1955 $post_type = $q['post_type'];
1956 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1957 $q['posts_per_page'] = get_option('posts_per_page');
1958 if ( isset($q['showposts']) && $q['showposts'] ) {
1959 $q['showposts'] = (int) $q['showposts'];
1960 $q['posts_per_page'] = $q['showposts'];
1962 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1963 $q['posts_per_page'] = $q['posts_per_archive_page'];
1964 if ( !isset($q['nopaging']) ) {
1965 if ( $q['posts_per_page'] == -1 ) {
1966 $q['nopaging'] = true;
1968 $q['nopaging'] = false;
1971 if ( $this->is_feed ) {
1972 $q['posts_per_page'] = get_option('posts_per_rss');
1973 $q['nopaging'] = false;
1975 $q['posts_per_page'] = (int) $q['posts_per_page'];
1976 if ( $q['posts_per_page'] < -1 )
1977 $q['posts_per_page'] = abs($q['posts_per_page']);
1978 else if ( $q['posts_per_page'] == 0 )
1979 $q['posts_per_page'] = 1;
1981 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
1982 $q['comments_per_page'] = get_option('comments_per_page');
1984 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
1985 $this->is_page = true;
1986 $this->is_home = false;
1987 $q['page_id'] = get_option('page_on_front');
1990 if ( isset($q['page']) ) {
1991 $q['page'] = trim($q['page'], '/');
1992 $q['page'] = absint($q['page']);
1995 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
1996 if ( isset($q['no_found_rows']) )
1997 $q['no_found_rows'] = (bool) $q['no_found_rows'];
1999 $q['no_found_rows'] = false;
2001 switch ( $q['fields'] ) {
2003 $fields = "$wpdb->posts.ID";
2006 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2009 $fields = "$wpdb->posts.*";
2012 // If a month is specified in the querystring, load that month
2014 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
2015 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2016 if ( strlen($q['m']) > 5 )
2017 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2018 if ( strlen($q['m']) > 7 )
2019 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2020 if ( strlen($q['m']) > 9 )
2021 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2022 if ( strlen($q['m']) > 11 )
2023 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2024 if ( strlen($q['m']) > 13 )
2025 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2028 if ( '' !== $q['hour'] )
2029 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
2031 if ( '' !== $q['minute'] )
2032 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
2034 if ( '' !== $q['second'] )
2035 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
2038 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
2040 if ( $q['monthnum'] )
2041 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
2044 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
2046 // If we've got a post_type AND its not "any" post_type.
2047 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2048 foreach ( (array)$q['post_type'] as $_post_type ) {
2049 $ptype_obj = get_post_type_object($_post_type);
2050 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2053 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2054 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2055 $q['name'] = $q[ $ptype_obj->query_var ];
2057 // Hierarchical post_types will operate through the
2058 $q['pagename'] = $q[ $ptype_obj->query_var ];
2062 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2068 if ( '' != $q['name'] ) {
2069 $q['name'] = sanitize_title_for_query( $q['name'] );
2070 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2071 } elseif ( '' != $q['pagename'] ) {
2072 if ( isset($this->queried_object_id) ) {
2073 $reqpage = $this->queried_object_id;
2075 if ( 'page' != $q['post_type'] ) {
2076 foreach ( (array)$q['post_type'] as $_post_type ) {
2077 $ptype_obj = get_post_type_object($_post_type);
2078 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2081 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2087 $reqpage = get_page_by_path($q['pagename']);
2089 if ( !empty($reqpage) )
2090 $reqpage = $reqpage->ID;
2095 $page_for_posts = get_option('page_for_posts');
2096 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2097 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2098 $q['name'] = $q['pagename'];
2099 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2100 $reqpage_obj = get_page($reqpage);
2101 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2102 $this->is_attachment = true;
2103 $post_type = $q['post_type'] = 'attachment';
2104 $this->is_page = true;
2105 $q['attachment_id'] = $reqpage;
2108 } elseif ( '' != $q['attachment'] ) {
2109 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2110 $q['name'] = $q['attachment'];
2111 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2115 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
2117 if ( intval($q['comments_popup']) )
2118 $q['p'] = absint($q['comments_popup']);
2120 // If an attachment is requested by number, let it supercede any post number.
2121 if ( $q['attachment_id'] )
2122 $q['p'] = absint($q['attachment_id']);
2124 // If a post number is specified, load that post
2126 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2127 } elseif ( $q['post__in'] ) {
2128 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2129 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2130 } elseif ( $q['post__not_in'] ) {
2131 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2132 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2135 if ( is_numeric($q['post_parent']) )
2136 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2138 if ( $q['page_id'] ) {
2139 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2140 $q['p'] = $q['page_id'];
2141 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2145 // If a search pattern is specified, load the posts that match
2146 if ( !empty($q['s']) ) {
2147 // added slashes screw with quote grouping when done early, so done later
2148 $q['s'] = stripslashes($q['s']);
2149 if ( !empty($q['sentence']) ) {
2150 $q['search_terms'] = array($q['s']);
2152 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
2153 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
2155 $n = !empty($q['exact']) ? '' : '%';
2157 foreach( (array) $q['search_terms'] as $term ) {
2158 $term = esc_sql( like_escape( $term ) );
2159 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
2160 $searchand = ' AND ';
2162 $term = esc_sql( like_escape( $q['s'] ) );
2163 if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
2164 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
2166 if ( !empty($search) ) {
2167 $search = " AND ({$search}) ";
2168 if ( !is_user_logged_in() )
2169 $search .= " AND ($wpdb->posts.post_password = '') ";
2173 // Allow plugins to contextually add/remove/modify the search section of the database query
2174 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
2177 if ( !$this->is_singular ) {
2178 $this->parse_tax_query( $q );
2180 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2182 $join .= $clauses['join'];
2183 $where .= $clauses['where'];
2186 if ( $this->is_tax ) {
2187 if ( empty($post_type) ) {
2189 $post_status_join = true;
2190 } elseif ( in_array('attachment', (array) $post_type) ) {
2191 $post_status_join = true;
2196 if ( !empty($this->tax_query->queries) ) {
2197 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2198 if ( !empty( $tax_query_in_and ) ) {
2199 if ( !isset( $q['taxonomy'] ) ) {
2200 foreach ( $tax_query_in_and as $a_tax_query ) {
2201 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2202 $q['taxonomy'] = $a_tax_query['taxonomy'];
2203 if ( 'slug' == $a_tax_query['field'] )
2204 $q['term'] = $a_tax_query['terms'][0];
2206 $q['term_id'] = $a_tax_query['terms'][0];
2213 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2214 if ( !empty( $cat_query ) ) {
2215 $cat_query = reset( $cat_query );
2216 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2218 $this->set( 'cat', $the_cat->term_id );
2219 $this->set( 'category_name', $the_cat->slug );
2223 unset( $cat_query );
2225 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2226 if ( !empty( $tag_query ) ) {
2227 $tag_query = reset( $tag_query );
2228 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2230 $this->set( 'tag_id', $the_tag->term_id );
2234 unset( $tag_query );
2238 // Author/user stuff
2240 if ( empty($q['author']) || ($q['author'] == '0') ) {
2243 $q['author'] = (string)urldecode($q['author']);
2244 $q['author'] = addslashes_gpc($q['author']);
2245 if ( strpos($q['author'], '-') !== false ) {
2248 $q['author'] = explode('-', $q['author']);
2249 $q['author'] = (string)absint($q['author'][1]);
2254 $author_array = preg_split('/[,\s]+/', $q['author']);
2255 $_author_array = array();
2256 foreach ( $author_array as $key => $_author )
2257 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
2258 $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
2259 unset($author_array, $_author_array);
2262 // Author stuff for nice URLs
2264 if ( '' != $q['author_name'] ) {
2265 if ( strpos($q['author_name'], '/') !== false ) {
2266 $q['author_name'] = explode('/', $q['author_name']);
2267 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2268 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2270 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
2273 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2274 $q['author'] = get_user_by('slug', $q['author_name']);
2276 $q['author'] = $q['author']->ID;
2277 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2280 // MIME-Type stuff for attachment browsing
2282 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
2283 $table_alias = $post_status_join ? $wpdb->posts : '';
2284 $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
2287 $where .= $search . $whichauthor . $whichmimetype;
2289 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2290 $q['order'] = 'DESC';
2293 if ( empty($q['orderby']) ) {
2294 $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
2295 } elseif ( 'none' == $q['orderby'] ) {
2298 // Used to filter values
2299 $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2300 if ( !empty($q['meta_key']) ) {
2301 $allowed_keys[] = $q['meta_key'];
2302 $allowed_keys[] = 'meta_value';
2303 $allowed_keys[] = 'meta_value_num';
2305 $q['orderby'] = urldecode($q['orderby']);
2306 $q['orderby'] = addslashes_gpc($q['orderby']);
2307 $orderby_array = explode(' ', $q['orderby']);
2310 foreach ( $orderby_array as $i => $orderby ) {
2311 // Only allow certain values for safety
2312 if ( ! in_array($orderby, $allowed_keys) )
2315 switch ( $orderby ) {
2319 $orderby = "$wpdb->posts.ID";
2322 $orderby = 'RAND()';
2324 case $q['meta_key']:
2326 $orderby = "$wpdb->postmeta.meta_value";
2328 case 'meta_value_num':
2329 $orderby = "$wpdb->postmeta.meta_value+0";
2331 case 'comment_count':
2332 $orderby = "$wpdb->posts.comment_count";
2335 $orderby = "$wpdb->posts.post_" . $orderby;
2338 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
2341 // append ASC or DESC at the end
2342 if ( !empty($q['orderby']))
2343 $q['orderby'] .= " {$q['order']}";
2345 if ( empty($q['orderby']) )
2346 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
2349 if ( is_array( $post_type ) ) {
2350 $post_type_cap = 'multiple_post_type';
2352 $post_type_object = get_post_type_object( $post_type );
2353 if ( empty( $post_type_object ) )
2354 $post_type_cap = $post_type;
2357 $exclude_post_types = '';
2358 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2359 if ( ! empty( $in_search_post_types ) )
2360 $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
2362 if ( 'any' == $post_type ) {
2363 $where .= $exclude_post_types;
2364 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2365 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2366 } elseif ( ! empty( $post_type ) ) {
2367 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2368 $post_type_object = get_post_type_object ( $post_type );
2369 } elseif ( $this->is_attachment ) {
2370 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2371 $post_type_object = get_post_type_object ( 'attachment' );
2372 } elseif ( $this->is_page ) {
2373 $where .= " AND $wpdb->posts.post_type = 'page'";
2374 $post_type_object = get_post_type_object ( 'page' );
2376 $where .= " AND $wpdb->posts.post_type = 'post'";
2377 $post_type_object = get_post_type_object ( 'post' );
2380 if ( ! empty( $post_type_object ) ) {
2381 $edit_cap = $post_type_object->cap->edit_post;
2382 $read_cap = $post_type_object->cap->read_post;
2383 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2384 $read_private_cap = $post_type_object->cap->read_private_posts;
2386 $edit_cap = 'edit_' . $post_type_cap;
2387 $read_cap = 'read_' . $post_type_cap;
2388 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2389 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2392 if ( ! empty( $q['post_status'] ) ) {
2393 $statuswheres = array();
2394 $q_status = $q['post_status'];
2395 if ( ! is_array( $q_status ) )
2396 $q_status = explode(',', $q_status);
2397 $r_status = array();
2398 $p_status = array();
2399 $e_status = array();
2400 if ( in_array('any', $q_status) ) {
2401 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2402 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2404 foreach ( get_post_stati() as $status ) {
2405 if ( in_array( $status, $q_status ) ) {
2406 if ( 'private' == $status )
2407 $p_status[] = "$wpdb->posts.post_status = '$status'";
2409 $r_status[] = "$wpdb->posts.post_status = '$status'";
2414 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2415 $r_status = array_merge($r_status, $p_status);
2419 if ( !empty($e_status) ) {
2420 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2422 if ( !empty($r_status) ) {
2423 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2424 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2426 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2428 if ( !empty($p_status) ) {
2429 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2430 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2432 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2434 if ( $post_status_join ) {
2435 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2436 foreach ( $statuswheres as $index => $statuswhere )
2437 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2439 foreach ( $statuswheres as $statuswhere )
2440 $where .= " AND $statuswhere";
2441 } elseif ( !$this->is_singular ) {
2442 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2444 // Add public states.
2445 $public_states = get_post_stati( array('public' => true) );
2446 foreach ( (array) $public_states as $state ) {
2447 if ( 'publish' == $state ) // Publish is hard-coded above.
2449 $where .= " OR $wpdb->posts.post_status = '$state'";
2453 // Add protected states that should show in the admin all list.
2454 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2455 foreach ( (array) $admin_all_states as $state )
2456 $where .= " OR $wpdb->posts.post_status = '$state'";
2459 if ( is_user_logged_in() ) {
2460 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2461 $private_states = get_post_stati( array('private' => true) );
2462 foreach ( (array) $private_states as $state )
2463 $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'";
2469 // Parse the meta query again if query vars have changed.
2470 if ( $this->query_vars_changed ) {
2471 $meta_query_hash = md5( serialize( $q['meta_query'] ) );
2472 $_meta_query = $q['meta_query'];
2473 unset( $q['meta_query'] );
2474 _parse_meta_query( $q );
2475 if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
2476 $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
2479 if ( !empty( $q['meta_query'] ) ) {
2480 $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
2481 $join .= $clauses['join'];
2482 $where .= $clauses['where'];
2485 if ( ! empty( $this->tax_query->queries ) || ! empty( $q['meta_query'] ) ) {
2486 $groupby = "{$wpdb->posts}.ID";
2489 // Apply filters on where and join prior to paging so that any
2490 // manipulations to them are reflected in the paging by day queries.
2491 if ( !$q['suppress_filters'] ) {
2492 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2493 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2497 if ( empty($q['nopaging']) && !$this->is_singular ) {
2498 $page = absint($q['paged']);
2502 if ( empty($q['offset']) ) {
2504 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2505 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2506 } else { // we're ignoring $page and using 'offset'
2507 $q['offset'] = absint($q['offset']);
2508 $pgstrt = $q['offset'] . ', ';
2509 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2514 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2515 if ( $this->is_archive || $this->is_search ) {
2516 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2517 $cwhere = "WHERE comment_approved = '1' $where";
2518 $cgroupby = "$wpdb->comments.comment_id";
2519 } else { // Other non singular e.g. front
2520 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2521 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2525 if ( !$q['suppress_filters'] ) {
2526 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2527 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2528 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2529 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2530 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2532 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2533 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2535 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2536 $this->comment_count = count($this->comments);
2538 $post_ids = array();
2540 foreach ( $this->comments as $comment )
2541 $post_ids[] = (int) $comment->comment_post_ID;
2543 $post_ids = join(',', $post_ids);
2546 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2551 $orderby = $q['orderby'];
2553 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2555 // Apply post-paging filters on where and join. Only plugins that
2556 // manipulate paging queries should use these hooks.
2557 if ( !$q['suppress_filters'] ) {
2558 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2559 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2560 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2561 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2562 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2563 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2564 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2566 // Filter all clauses at once, for convenience
2567 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2568 foreach ( $pieces as $piece )
2569 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2572 // Announce current selection parameters. For use by caching plugins.
2573 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2575 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2576 if ( !$q['suppress_filters'] ) {
2577 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2578 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2579 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2580 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2581 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2582 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2583 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2585 // Filter all clauses at once, for convenience
2586 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2587 foreach ( $pieces as $piece )
2588 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2591 if ( ! empty($groupby) )
2592 $groupby = 'GROUP BY ' . $groupby;
2593 if ( !empty( $orderby ) )
2594 $orderby = 'ORDER BY ' . $orderby;
2597 if ( !$q['no_found_rows'] && !empty($limits) )
2598 $found_rows = 'SQL_CALC_FOUND_ROWS';
2600 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2601 if ( !$q['suppress_filters'] )
2602 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
2604 if ( 'ids' == $q['fields'] ) {
2605 $this->posts = $wpdb->get_col($this->request);
2607 return $this->posts;
2610 if ( 'id=>parent' == $q['fields'] ) {
2611 $this->posts = $wpdb->get_results($this->request);
2614 foreach ( $this->posts as $post )
2615 $r[ $post->ID ] = $post->post_parent;
2620 $this->posts = $wpdb->get_results($this->request);
2622 // Raw results filter. Prior to status checks.
2623 if ( !$q['suppress_filters'] )
2624 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2626 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2627 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2628 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2629 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2630 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2631 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2632 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2633 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2634 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2635 $this->comments = $wpdb->get_results($comments_request);
2636 $this->comment_count = count($this->comments);
2639 if ( !$q['no_found_rows'] && !empty($limits) ) {
2640 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
2641 $this->found_posts = $wpdb->get_var( $found_posts_query );
2642 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
2643 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
2646 // Check post status to determine if post should be displayed.
2647 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2648 $status = get_post_status($this->posts[0]);
2649 $post_status_obj = get_post_status_object($status);
2650 //$type = get_post_type($this->posts[0]);
2651 if ( !$post_status_obj->public ) {
2652 if ( ! is_user_logged_in() ) {
2653 // User must be logged in to view unpublished posts.
2654 $this->posts = array();
2656 if ( $post_status_obj->protected ) {
2657 // User must have edit permissions on the draft to preview.
2658 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2659 $this->posts = array();
2661 $this->is_preview = true;
2662 if ( 'future' != $status )
2663 $this->posts[0]->post_date = current_time('mysql');
2665 } elseif ( $post_status_obj->private ) {
2666 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2667 $this->posts = array();
2669 $this->posts = array();
2674 if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2675 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
2678 // Put sticky posts at the top of the posts array
2679 $sticky_posts = get_option('sticky_posts');
2680 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2681 $num_posts = count($this->posts);
2683 // Loop over posts and relocate stickies to the front.
2684 for ( $i = 0; $i < $num_posts; $i++ ) {
2685 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2686 $sticky_post = $this->posts[$i];
2687 // Remove sticky from current position
2688 array_splice($this->posts, $i, 1);
2689 // Move to front, after other stickies
2690 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2691 // Increment the sticky offset. The next sticky will be placed at this offset.
2693 // Remove post from sticky posts array
2694 $offset = array_search($sticky_post->ID, $sticky_posts);
2695 unset( $sticky_posts[$offset] );
2699 // If any posts have been excluded specifically, Ignore those that are sticky.
2700 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
2701 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
2703 // Fetch sticky posts that weren't in the query results
2704 if ( !empty($sticky_posts) ) {
2705 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2706 // honor post type(s) if not set to any
2707 $stickies_where = '';
2708 if ( 'any' != $post_type && '' != $post_type ) {
2709 if ( is_array( $post_type ) ) {
2710 $post_types = join( "', '", $post_type );
2712 $post_types = $post_type;
2714 $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
2717 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
2718 foreach ( $stickies as $sticky_post ) {
2719 // Ignore sticky posts the current user cannot read or are not published.
2720 if ( 'publish' != $sticky_post->post_status )
2722 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2728 if ( !$q['suppress_filters'] )
2729 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
2731 $this->post_count = count($this->posts);
2733 // Sanitize before caching so it'll only get done once
2734 for ( $i = 0; $i < $this->post_count; $i++ ) {
2735 $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
2738 if ( $q['cache_results'] )
2739 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
2741 if ( $this->post_count > 0 ) {
2742 $this->post = $this->posts[0];
2745 return $this->posts;
2749 * Set up the next post and iterate current post index.
2754 * @return object Next post.
2756 function next_post() {
2758 $this->current_post++;
2760 $this->post = $this->posts[$this->current_post];
2765 * Sets up the current post.
2767 * Retrieves the next post, sets up the post, sets the 'in the loop'
2773 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2775 function the_post() {
2777 $this->in_the_loop = true;
2779 if ( $this->current_post == -1 ) // loop has just started
2780 do_action_ref_array('loop_start', array(&$this));
2782 $post = $this->next_post();
2783 setup_postdata($post);
2787 * Whether there are more posts available in the loop.
2789 * Calls action 'loop_end', when the loop is complete.
2793 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2795 * @return bool True if posts are available, false if end of loop.
2797 function have_posts() {
2798 if ( $this->current_post + 1 < $this->post_count ) {
2800 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
2801 do_action_ref_array('loop_end', array(&$this));
2802 // Do some cleaning up after the loop
2803 $this->rewind_posts();
2806 $this->in_the_loop = false;
2811 * Rewind the posts and reset post index.
2816 function rewind_posts() {
2817 $this->current_post = -1;
2818 if ( $this->post_count > 0 ) {
2819 $this->post = $this->posts[0];
2824 * Iterate current comment index and return comment object.
2829 * @return object Comment object.
2831 function next_comment() {
2832 $this->current_comment++;
2834 $this->comment = $this->comments[$this->current_comment];
2835 return $this->comment;
2839 * Sets up the current comment.
2843 * @global object $comment Current comment.
2844 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2846 function the_comment() {
2849 $comment = $this->next_comment();
2851 if ( $this->current_comment == 0 ) {
2852 do_action('comment_loop_start');
2857 * Whether there are more comments available.
2859 * Automatically rewinds comments when finished.
2864 * @return bool True, if more comments. False, if no more posts.
2866 function have_comments() {
2867 if ( $this->current_comment + 1 < $this->comment_count ) {
2869 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
2870 $this->rewind_comments();
2877 * Rewind the comments, resets the comment index and comment to first.
2882 function rewind_comments() {
2883 $this->current_comment = -1;
2884 if ( $this->comment_count > 0 ) {
2885 $this->comment = $this->comments[0];
2890 * Sets up the WordPress query by parsing query string.
2895 * @param string $query URL query string.
2896 * @return array List of posts.
2898 function &query( $query ) {
2900 $this->query = $this->query_vars = wp_parse_args( $query );
2901 return $this->get_posts();
2905 * Retrieve queried object.
2907 * If queried object is not set, then the queried object will be set from
2908 * the category, tag, taxonomy, posts page, single post, page, or author
2909 * query variable. After it is set up, it will be returned.
2916 function get_queried_object() {
2917 if ( isset($this->queried_object) )
2918 return $this->queried_object;
2920 $this->queried_object = NULL;
2921 $this->queried_object_id = 0;
2923 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
2924 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2926 $query = reset( $tax_query_in_and );
2928 if ( 'term_id' == $query['field'] )
2929 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
2931 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
2933 if ( $term && ! is_wp_error($term) ) {
2934 $this->queried_object = $term;
2935 $this->queried_object_id = (int) $term->term_id;
2937 if ( $this->is_category )
2938 _make_cat_compat( $this->queried_object );
2940 } elseif ( $this->is_post_type_archive ) {
2941 $this->queried_object = get_post_type_object( $this->get('post_type') );
2942 } elseif ( $this->is_posts_page ) {
2943 $page_for_posts = get_option('page_for_posts');
2944 $this->queried_object = & get_page( $page_for_posts );
2945 $this->queried_object_id = (int) $this->queried_object->ID;
2946 } elseif ( $this->is_singular && !is_null($this->post) ) {
2947 $this->queried_object = $this->post;
2948 $this->queried_object_id = (int) $this->post->ID;
2949 } elseif ( $this->is_author ) {
2950 $this->queried_object_id = (int) $this->get('author');
2951 $this->queried_object = get_userdata( $this->queried_object_id );
2954 return $this->queried_object;
2958 * Retrieve ID of the current queried object.
2965 function get_queried_object_id() {
2966 $this->get_queried_object();
2968 if ( isset($this->queried_object_id) ) {
2969 return $this->queried_object_id;
2976 * PHP4 type constructor.
2978 * Sets up the WordPress query, if parameter is not empty.
2983 * @param string $query URL query string.
2986 function WP_Query($query = '') {
2987 if ( ! empty($query) ) {
2988 $this->query($query);
2993 * Is the query for an archive page?
2995 * Month, Year, Category, Author, Post Type archive...
3001 function is_archive() {
3002 return (bool) $this->is_archive;
3006 * Is the query for a post type archive page?
3010 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3013 function is_post_type_archive( $post_types = '' ) {
3014 if ( empty( $post_types ) || !$this->is_post_type_archive )
3015 return (bool) $this->is_post_type_archive;
3017 $post_type_object = $this->get_queried_object();
3019 return in_array( $post_type_object->name, (array) $post_types );
3023 * Is the query for an attachment page?
3029 function is_attachment() {
3030 return (bool) $this->is_attachment;
3034 * Is the query for an author archive page?
3036 * If the $author parameter is specified, this function will additionally
3037 * check if the query is for one of the authors specified.
3041 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3044 function is_author( $author = '' ) {
3045 if ( !$this->is_author )
3048 if ( empty($author) )
3051 $author_obj = $this->get_queried_object();
3053 $author = (array) $author;
3055 if ( in_array( $author_obj->ID, $author ) )
3057 elseif ( in_array( $author_obj->nickname, $author ) )
3059 elseif ( in_array( $author_obj->user_nicename, $author ) )
3066 * Is the query for a category archive page?
3068 * If the $category parameter is specified, this function will additionally
3069 * check if the query is for one of the categories specified.
3073 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3076 function is_category( $category = '' ) {
3077 if ( !$this->is_category )
3080 if ( empty($category) )
3083 $cat_obj = $this->get_queried_object();
3085 $category = (array) $category;
3087 if ( in_array( $cat_obj->term_id, $category ) )
3089 elseif ( in_array( $cat_obj->name, $category ) )
3091 elseif ( in_array( $cat_obj->slug, $category ) )
3098 * Is the query for a tag archive page?
3100 * If the $tag parameter is specified, this function will additionally
3101 * check if the query is for one of the tags specified.
3105 * @param mixed $slug Optional. Tag slug or array of slugs.
3108 function is_tag( $slug = '' ) {
3109 if ( !$this->is_tag )
3112 if ( empty( $slug ) )
3115 $tag_obj = $this->get_queried_object();
3117 $slug = (array) $slug;
3119 if ( in_array( $tag_obj->slug, $slug ) )
3126 * Is the query for a taxonomy archive page?
3128 * If the $taxonomy parameter is specified, this function will additionally
3129 * check if the query is for that specific $taxonomy.
3131 * If the $term parameter is specified in addition to the $taxonomy parameter,
3132 * this function will additionally check if the query is for one of the terms
3137 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3138 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3141 function is_tax( $taxonomy = '', $term = '' ) {
3142 global $wp_taxonomies;
3144 if ( !$this->is_tax )
3147 if ( empty( $taxonomy ) )
3150 $queried_object = $this->get_queried_object();
3151 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3152 $term_array = (array) $term;
3154 if ( empty( $term ) ) // Only a Taxonomy provided
3155 return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
3157 return isset( $queried_object->term_id ) &&
3158 count( array_intersect(
3159 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3165 * Whether the current URL is within the comments popup window.
3171 function is_comments_popup() {
3172 return (bool) $this->is_comments_popup;
3176 * Is the query for a date archive?
3182 function is_date() {
3183 return (bool) $this->is_date;
3188 * Is the query for a day archive?
3195 return (bool) $this->is_day;
3199 * Is the query for a feed?
3203 * @param string|array $feeds Optional feed types to check.
3206 function is_feed( $feeds = '' ) {
3207 if ( empty( $feeds ) || ! $this->is_feed )
3208 return (bool) $this->is_feed;
3209 $qv = $this->get( 'feed' );
3210 if ( 'feed' == $qv )
3211 $qv = get_default_feed();
3212 return in_array( $qv, (array) $feeds );
3216 * Is the query for a comments feed?
3222 function is_comment_feed() {
3223 return (bool) $this->is_comment_feed;
3227 * Is the query for the front page of the site?
3229 * This is for what is displayed at your site's main URL.
3231 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3233 * If you set a static page for the front page of your site, this function will return
3234 * true when viewing that page.
3236 * Otherwise the same as @see WP_Query::is_home()
3240 * @uses get_option()
3242 * @return bool True, if front of site.
3244 function is_front_page() {
3246 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3248 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3255 * Is the query for the blog homepage?
3257 * This is the page which shows the time based blog content of your site.
3259 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3261 * If you set a static page for the front page of your site, this function will return
3262 * true only on the page you set as the "Posts page".
3264 * @see WP_Query::is_front_page()
3268 * @return bool True if blog view homepage.
3270 function is_home() {
3271 return (bool) $this->is_home;
3275 * Is the query for a month archive?
3281 function is_month() {
3282 return (bool) $this->is_month;
3286 * Is the query for a single page?
3288 * If the $page parameter is specified, this function will additionally
3289 * check if the query is for one of the pages specified.
3291 * @see WP_Query::is_single()
3292 * @see WP_Query::is_singular()
3296 * @param mixed $page Page ID, title, slug, or array of such.
3299 function is_page( $page = '' ) {
3300 if ( !$this->is_page )
3303 if ( empty( $page ) )
3306 $page_obj = $this->get_queried_object();
3308 $page = (array) $page;
3310 if ( in_array( $page_obj->ID, $page ) )
3312 elseif ( in_array( $page_obj->post_title, $page ) )
3314 else if ( in_array( $page_obj->post_name, $page ) )
3321 * Is the query for paged result and not for the first page?
3327 function is_paged() {
3328 return (bool) $this->is_paged;
3332 * Is the query for a post or page preview?
3338 function is_preview() {
3339 return (bool) $this->is_preview;
3343 * Is the query for the robots file?
3349 function is_robots() {
3350 return (bool) $this->is_robots;
3354 * Is the query for a search?
3360 function is_search() {
3361 return (bool) $this->is_search;
3365 * Is the query for a single post?
3367 * Works for any post type, except attachments and pages
3369 * If the $post parameter is specified, this function will additionally
3370 * check if the query is for one of the Posts specified.
3372 * @see WP_Query::is_page()
3373 * @see WP_Query::is_singular()
3377 * @param mixed $post Post ID, title, slug, or array of such.
3380 function is_single( $post = '' ) {
3381 if ( !$this->is_single )
3387 $post_obj = $this->get_queried_object();
3389 $post = (array) $post;
3391 if ( in_array( $post_obj->ID, $post ) )
3393 elseif ( in_array( $post_obj->post_title, $post ) )
3395 elseif ( in_array( $post_obj->post_name, $post ) )
3402 * Is the query for a single post of any post type (post, attachment, page, ... )?
3404 * If the $post_types parameter is specified, this function will additionally
3405 * check if the query is for one of the Posts Types specified.
3407 * @see WP_Query::is_page()
3408 * @see WP_Query::is_single()
3412 * @param mixed $post_types Optional. Post Type or array of Post Types
3415 function is_singular( $post_types = '' ) {
3416 if ( empty( $post_types ) || !$this->is_singular )
3417 return (bool) $this->is_singular;
3419 $post_obj = $this->get_queried_object();
3421 return in_array( $post_obj->post_type, (array) $post_types );
3425 * Is the query for a specific time?
3431 function is_time() {
3432 return (bool) $this->is_time;
3436 * Is the query for a trackback endpoint call?
3442 function is_trackback() {
3443 return (bool) $this->is_trackback;
3447 * Is the query for a specific year?
3453 function is_year() {
3454 return (bool) $this->is_year;
3458 * Is the query a 404 (returns no results)?
3465 return (bool) $this->is_404;
3470 * Redirect old slugs to the correct permalink.
3472 * Attempts to find the current slug from the past slugs.
3478 * @return null If no link is found, null is returned.
3480 function wp_old_slug_redirect() {
3482 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3485 // Guess the current post_type based on the query vars.
3486 if ( get_query_var('post_type') )
3487 $post_type = get_query_var('post_type');
3488 elseif ( !empty($wp_query->query_vars['pagename']) )
3489 $post_type = 'page';
3491 $post_type = 'post';
3493 // Do not attempt redirect for hierarchical post types
3494 if ( is_post_type_hierarchical( $post_type ) )
3497 $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']);
3499 // if year, monthnum, or day have been specified, make our query more precise
3500 // just in case there are multiple identical _wp_old_slug values
3501 if ( '' != $wp_query->query_vars['year'] )
3502 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3503 if ( '' != $wp_query->query_vars['monthnum'] )
3504 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3505 if ( '' != $wp_query->query_vars['day'] )
3506 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3508 $id = (int) $wpdb->get_var($query);
3513 $link = get_permalink($id);
3518 wp_redirect($link, '301'); // Permanent redirect
3524 * Set up global post data.
3528 * @param object $post Post data.
3529 * @uses do_action_ref_array() Calls 'the_post'
3530 * @return bool True when finished.
3532 function setup_postdata($post) {
3533 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3535 $id = (int) $post->ID;
3537 $authordata = get_userdata($post->post_author);
3539 $currentday = mysql2date('d.m.y', $post->post_date, false);
3540 $currentmonth = mysql2date('m', $post->post_date, false);
3542 $page = get_query_var('page');
3545 if ( is_single() || is_page() || is_feed() )
3547 $content = $post->post_content;
3548 if ( strpos( $content, '<!--nextpage-->' ) ) {
3552 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
3553 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
3554 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
3555 $pages = explode('<!--nextpage-->', $content);
3556 $numpages = count($pages);
3558 $pages = array( $post->post_content );
3562 do_action_ref_array('the_post', array(&$post));