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 * Is the query the main query?
725 function is_main_query() {
727 return $wp_query->is_main_query();
731 * The Loop. Post loop control.
735 * Whether current WordPress query has results to loop over.
737 * @see WP_Query::have_posts()
743 function have_posts() {
746 return $wp_query->have_posts();
750 * Whether the caller is in the Loop.
755 * @return bool True if caller is within loop, false if loop hasn't started or ended.
757 function in_the_loop() {
760 return $wp_query->in_the_loop;
764 * Rewind the loop posts.
766 * @see WP_Query::rewind_posts()
772 function rewind_posts() {
775 return $wp_query->rewind_posts();
779 * Iterate the post index in the loop.
781 * @see WP_Query::the_post()
785 function the_post() {
788 $wp_query->the_post();
796 * Whether there are comments to loop over.
798 * @see WP_Query::have_comments()
804 function have_comments() {
806 return $wp_query->have_comments();
810 * Iterate comment index in the comment loop.
812 * @see WP_Query::the_comment()
818 function the_comment() {
820 return $wp_query->the_comment();
828 * The WordPress Query class.
830 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
837 * Query vars set by the user
846 * Query vars, after parsing
852 var $query_vars = array();
855 * Taxonomy query, as passed to get_tax_sql()
859 * @var object WP_Tax_Query
864 * Metadata query container
868 * @var object WP_Meta_Query
870 var $meta_query = false;
873 * Holds the data for a single object that is queried.
875 * Holds the contents of a post, page, category, attachment.
884 * The ID of the queried object.
890 var $queried_object_id;
893 * Get post database query.
911 * The amount of posts for the current query.
920 * Index of the current item in the loop.
926 var $current_post = -1;
929 * Whether the loop has started and the caller is in the loop.
935 var $in_the_loop = false;
938 * The current post ID.
947 * The list of comments for current post.
956 * The amount of comments for the posts.
962 var $comment_count = 0;
965 * The index of the comment in the comment loop.
971 var $current_comment = -1;
974 * Current comment ID.
983 * Amount of posts if limit clause was not used.
989 var $found_posts = 0;
992 * The amount of pages.
998 var $max_num_pages = 0;
1001 * The amount of comment pages.
1007 var $max_num_comment_pages = 0;
1010 * Set if query is single post.
1016 var $is_single = false;
1019 * Set if query is preview of blog.
1025 var $is_preview = false;
1028 * Set if query returns a page.
1034 var $is_page = false;
1037 * Set if query is an archive list.
1043 var $is_archive = false;
1046 * Set if query is part of a date.
1052 var $is_date = false;
1055 * Set if query contains a year.
1061 var $is_year = false;
1064 * Set if query contains a month.
1070 var $is_month = false;
1073 * Set if query contains a day.
1079 var $is_day = false;
1082 * Set if query contains time.
1088 var $is_time = false;
1091 * Set if query contains an author.
1097 var $is_author = false;
1100 * Set if query contains category.
1106 var $is_category = false;
1109 * Set if query contains tag.
1115 var $is_tag = false;
1118 * Set if query contains taxonomy.
1124 var $is_tax = false;
1127 * Set if query was part of a search result.
1133 var $is_search = false;
1136 * Set if query is feed display.
1142 var $is_feed = false;
1145 * Set if query is comment feed display.
1151 var $is_comment_feed = false;
1154 * Set if query is trackback.
1160 var $is_trackback = false;
1163 * Set if query is blog homepage.
1169 var $is_home = false;
1172 * Set if query couldn't found anything.
1178 var $is_404 = false;
1181 * Set if query is within comments popup window.
1187 var $is_comments_popup = false;
1190 * Set if query is paged
1196 var $is_paged = false;
1199 * Set if query is part of administration page.
1205 var $is_admin = false;
1208 * Set if query is an attachment.
1214 var $is_attachment = false;
1217 * Set if is single, is a page, or is an attachment.
1223 var $is_singular = false;
1226 * Set if query is for robots.
1232 var $is_robots = false;
1235 * Set if query contains posts.
1237 * Basically, the homepage if the option isn't set for the static homepage.
1243 var $is_posts_page = false;
1246 * Set if query is for a post type archive.
1252 var $is_post_type_archive = false;
1255 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1256 * whether we have to re-parse because something has changed
1261 var $query_vars_hash = false;
1264 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1265 * via pre_get_posts hooks.
1270 var $query_vars_changed = true;
1273 * Set if post thumbnails are cached
1279 var $thumbnails_cached = false;
1282 * Resets query flags to false.
1284 * The query flags are what page info WordPress was able to figure out.
1289 function init_query_flags() {
1290 $this->is_single = false;
1291 $this->is_preview = false;
1292 $this->is_page = false;
1293 $this->is_archive = false;
1294 $this->is_date = false;
1295 $this->is_year = false;
1296 $this->is_month = false;
1297 $this->is_day = false;
1298 $this->is_time = false;
1299 $this->is_author = false;
1300 $this->is_category = false;
1301 $this->is_tag = false;
1302 $this->is_tax = false;
1303 $this->is_search = false;
1304 $this->is_feed = false;
1305 $this->is_comment_feed = false;
1306 $this->is_trackback = false;
1307 $this->is_home = false;
1308 $this->is_404 = false;
1309 $this->is_comments_popup = false;
1310 $this->is_paged = false;
1311 $this->is_admin = false;
1312 $this->is_attachment = false;
1313 $this->is_singular = false;
1314 $this->is_robots = false;
1315 $this->is_posts_page = false;
1316 $this->is_post_type_archive = false;
1320 * Initiates object properties and sets default values.
1326 unset($this->posts);
1327 unset($this->query);
1328 $this->query_vars = array();
1329 unset($this->queried_object);
1330 unset($this->queried_object_id);
1331 $this->post_count = 0;
1332 $this->current_post = -1;
1333 $this->in_the_loop = false;
1334 unset( $this->request );
1335 unset( $this->post );
1336 unset( $this->comments );
1337 unset( $this->comment );
1338 $this->comment_count = 0;
1339 $this->current_comment = -1;
1340 $this->found_posts = 0;
1341 $this->max_num_pages = 0;
1342 $this->max_num_comment_pages = 0;
1344 $this->init_query_flags();
1348 * Reparse the query vars.
1353 function parse_query_vars() {
1354 $this->parse_query();
1358 * Fills in the query variables, which do not exist within the parameter.
1363 * @param array $array Defined query variables.
1364 * @return array Complete query variables with undefined ones filled in empty.
1366 function fill_query_vars($array) {
1404 foreach ( $keys as $key ) {
1405 if ( !isset($array[$key]) )
1409 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1410 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1412 foreach ( $array_keys as $key ) {
1413 if ( !isset($array[$key]) )
1414 $array[$key] = array();
1420 * Parse a query string and set query type booleans.
1425 * @param string|array $query Optional query.
1427 function parse_query( $query = '' ) {
1428 if ( ! empty( $query ) ) {
1430 $this->query = $this->query_vars = wp_parse_args( $query );
1431 } elseif ( ! isset( $this->query ) ) {
1432 $this->query = $this->query_vars;
1435 $this->query_vars = $this->fill_query_vars($this->query_vars);
1436 $qv = &$this->query_vars;
1437 $this->query_vars_changed = true;
1439 if ( ! empty($qv['robots']) )
1440 $this->is_robots = true;
1442 $qv['p'] = absint($qv['p']);
1443 $qv['page_id'] = absint($qv['page_id']);
1444 $qv['year'] = absint($qv['year']);
1445 $qv['monthnum'] = absint($qv['monthnum']);
1446 $qv['day'] = absint($qv['day']);
1447 $qv['w'] = absint($qv['w']);
1448 $qv['m'] = absint($qv['m']);
1449 $qv['paged'] = absint($qv['paged']);
1450 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1451 $qv['pagename'] = trim( $qv['pagename'] );
1452 $qv['name'] = trim( $qv['name'] );
1453 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1454 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1455 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1457 // Compat. Map subpost to attachment.
1458 if ( '' != $qv['subpost'] )
1459 $qv['attachment'] = $qv['subpost'];
1460 if ( '' != $qv['subpost_id'] )
1461 $qv['attachment_id'] = $qv['subpost_id'];
1463 $qv['attachment_id'] = absint($qv['attachment_id']);
1465 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1466 $this->is_single = true;
1467 $this->is_attachment = true;
1468 } elseif ( '' != $qv['name'] ) {
1469 $this->is_single = true;
1470 } elseif ( $qv['p'] ) {
1471 $this->is_single = true;
1472 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1473 // If year, month, day, hour, minute, and second are set, a single
1474 // post is being queried.
1475 $this->is_single = true;
1476 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1477 $this->is_page = true;
1478 $this->is_single = false;
1480 // Look for archive queries. Dates, categories, authors, search, post type archives.
1482 if ( !empty($qv['s']) ) {
1483 $this->is_search = true;
1486 if ( '' !== $qv['second'] ) {
1487 $this->is_time = true;
1488 $this->is_date = true;
1491 if ( '' !== $qv['minute'] ) {
1492 $this->is_time = true;
1493 $this->is_date = true;
1496 if ( '' !== $qv['hour'] ) {
1497 $this->is_time = true;
1498 $this->is_date = true;
1502 if ( ! $this->is_date ) {
1503 $this->is_day = true;
1504 $this->is_date = true;
1508 if ( $qv['monthnum'] ) {
1509 if ( ! $this->is_date ) {
1510 $this->is_month = true;
1511 $this->is_date = true;
1515 if ( $qv['year'] ) {
1516 if ( ! $this->is_date ) {
1517 $this->is_year = true;
1518 $this->is_date = true;
1523 $this->is_date = true;
1524 if ( strlen($qv['m']) > 9 ) {
1525 $this->is_time = true;
1526 } else if ( strlen($qv['m']) > 7 ) {
1527 $this->is_day = true;
1528 } else if ( strlen($qv['m']) > 5 ) {
1529 $this->is_month = true;
1531 $this->is_year = true;
1535 if ( '' != $qv['w'] ) {
1536 $this->is_date = true;
1539 $this->query_vars_hash = false;
1540 $this->parse_tax_query( $qv );
1542 foreach ( $this->tax_query->queries as $tax_query ) {
1543 if ( 'NOT IN' != $tax_query['operator'] ) {
1544 switch ( $tax_query['taxonomy'] ) {
1546 $this->is_category = true;
1549 $this->is_tag = true;
1552 $this->is_tax = true;
1556 unset( $tax_query );
1558 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1559 $this->is_author = false;
1561 $this->is_author = true;
1564 if ( '' != $qv['author_name'] )
1565 $this->is_author = true;
1567 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1568 $post_type_obj = get_post_type_object( $qv['post_type'] );
1569 if ( ! empty( $post_type_obj->has_archive ) )
1570 $this->is_post_type_archive = true;
1573 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1574 $this->is_archive = true;
1577 if ( '' != $qv['feed'] )
1578 $this->is_feed = true;
1580 if ( '' != $qv['tb'] )
1581 $this->is_trackback = true;
1583 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1584 $this->is_paged = true;
1586 if ( '' != $qv['comments_popup'] )
1587 $this->is_comments_popup = true;
1589 // if we're previewing inside the write screen
1590 if ( '' != $qv['preview'] )
1591 $this->is_preview = true;
1594 $this->is_admin = true;
1596 if ( false !== strpos($qv['feed'], 'comments-') ) {
1597 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1598 $qv['withcomments'] = 1;
1601 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1603 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1604 $this->is_comment_feed = true;
1606 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 ) )
1607 $this->is_home = true;
1609 // Correct is_* for page_on_front and page_for_posts
1610 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1611 $_query = wp_parse_args($this->query);
1612 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1613 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1614 unset($_query['pagename']);
1615 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1616 $this->is_page = true;
1617 $this->is_home = false;
1618 $qv['page_id'] = get_option('page_on_front');
1619 // Correct <!--nextpage--> for page_on_front
1620 if ( !empty($qv['paged']) ) {
1621 $qv['page'] = $qv['paged'];
1622 unset($qv['paged']);
1627 if ( '' != $qv['pagename'] ) {
1628 $this->queried_object =& get_page_by_path($qv['pagename']);
1629 if ( !empty($this->queried_object) )
1630 $this->queried_object_id = (int) $this->queried_object->ID;
1632 unset($this->queried_object);
1634 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1635 $this->is_page = false;
1636 $this->is_home = true;
1637 $this->is_posts_page = true;
1641 if ( $qv['page_id'] ) {
1642 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1643 $this->is_page = false;
1644 $this->is_home = true;
1645 $this->is_posts_page = true;
1649 if ( !empty($qv['post_type']) ) {
1650 if ( is_array($qv['post_type']) )
1651 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1653 $qv['post_type'] = sanitize_key($qv['post_type']);
1656 if ( ! empty( $qv['post_status'] ) ) {
1657 if ( is_array( $qv['post_status'] ) )
1658 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1660 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1663 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1664 $this->is_comment_feed = false;
1666 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1667 // Done correcting is_* for page_on_front and page_for_posts
1669 if ( '404' == $qv['error'] )
1672 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1673 $this->query_vars_changed = false;
1675 do_action_ref_array('parse_query', array(&$this));
1679 * Parses various taxonomy related query vars.
1684 * @param array &$q The query variables
1686 function parse_tax_query( &$q ) {
1687 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1688 $tax_query = $q['tax_query'];
1690 $tax_query = array();
1693 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1694 $tax_query[] = array(
1695 'taxonomy' => $q['taxonomy'],
1696 'terms' => array( $q['term'] ),
1701 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1702 if ( 'post_tag' == $taxonomy )
1703 continue; // Handled further down in the $q['tag'] block
1705 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1706 $tax_query_defaults = array(
1707 'taxonomy' => $taxonomy,
1711 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1712 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1715 $term = $q[$t->query_var];
1717 if ( strpos($term, '+') !== false ) {
1718 $terms = preg_split( '/[+]+/', $term );
1719 foreach ( $terms as $term ) {
1720 $tax_query[] = array_merge( $tax_query_defaults, array(
1721 'terms' => array( $term )
1725 $tax_query[] = array_merge( $tax_query_defaults, array(
1726 'terms' => preg_split( '/[,]+/', $term )
1733 if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1734 $q['cat'] = ''.urldecode($q['cat']).'';
1735 $q['cat'] = addslashes_gpc($q['cat']);
1736 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1738 $req_cats = array();
1739 foreach ( (array) $cat_array as $cat ) {
1740 $cat = intval($cat);
1745 $q['category__in'][] = $cat;
1746 $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1748 $q['category__not_in'][] = $cat;
1749 $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1752 $q['cat'] = implode(',', $req_cats);
1755 if ( !empty($q['category__in']) ) {
1756 $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
1757 $tax_query[] = array(
1758 'taxonomy' => 'category',
1759 'terms' => $q['category__in'],
1760 'field' => 'term_id',
1761 'include_children' => false
1765 if ( !empty($q['category__not_in']) ) {
1766 $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
1767 $tax_query[] = array(
1768 'taxonomy' => 'category',
1769 'terms' => $q['category__not_in'],
1770 'operator' => 'NOT IN',
1771 'include_children' => false
1775 if ( !empty($q['category__and']) ) {
1776 $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
1777 $tax_query[] = array(
1778 'taxonomy' => 'category',
1779 'terms' => $q['category__and'],
1780 'field' => 'term_id',
1781 'operator' => 'AND',
1782 'include_children' => false
1787 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1788 if ( strpos($q['tag'], ',') !== false ) {
1789 $tags = preg_split('/[,\s]+/', $q['tag']);
1790 foreach ( (array) $tags as $tag ) {
1791 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1792 $q['tag_slug__in'][] = $tag;
1794 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1795 $tags = preg_split('/[+\s]+/', $q['tag']);
1796 foreach ( (array) $tags as $tag ) {
1797 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1798 $q['tag_slug__and'][] = $tag;
1801 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1802 $q['tag_slug__in'][] = $q['tag'];
1806 if ( !empty($q['tag_id']) ) {
1807 $q['tag_id'] = absint( $q['tag_id'] );
1808 $tax_query[] = array(
1809 'taxonomy' => 'post_tag',
1810 'terms' => $q['tag_id']
1814 if ( !empty($q['tag__in']) ) {
1815 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1816 $tax_query[] = array(
1817 'taxonomy' => 'post_tag',
1818 'terms' => $q['tag__in']
1822 if ( !empty($q['tag__not_in']) ) {
1823 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1824 $tax_query[] = array(
1825 'taxonomy' => 'post_tag',
1826 'terms' => $q['tag__not_in'],
1827 'operator' => 'NOT IN'
1831 if ( !empty($q['tag__and']) ) {
1832 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1833 $tax_query[] = array(
1834 'taxonomy' => 'post_tag',
1835 'terms' => $q['tag__and'],
1840 if ( !empty($q['tag_slug__in']) ) {
1841 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1842 $tax_query[] = array(
1843 'taxonomy' => 'post_tag',
1844 'terms' => $q['tag_slug__in'],
1849 if ( !empty($q['tag_slug__and']) ) {
1850 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1851 $tax_query[] = array(
1852 'taxonomy' => 'post_tag',
1853 'terms' => $q['tag_slug__and'],
1859 $this->tax_query = new WP_Tax_Query( $tax_query );
1863 * Sets the 404 property and saves whether query is feed.
1868 function set_404() {
1869 $is_feed = $this->is_feed;
1871 $this->init_query_flags();
1872 $this->is_404 = true;
1874 $this->is_feed = $is_feed;
1878 * Retrieve query variable.
1883 * @param string $query_var Query variable key.
1886 function get($query_var) {
1887 if ( isset($this->query_vars[$query_var]) )
1888 return $this->query_vars[$query_var];
1894 * Set query variable.
1899 * @param string $query_var Query variable key.
1900 * @param mixed $value Query variable value.
1902 function set($query_var, $value) {
1903 $this->query_vars[$query_var] = $value;
1907 * Retrieve the posts based on query variables.
1909 * There are a few filters and actions that can be used to modify the post
1914 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1916 * @return array List of posts.
1918 function &get_posts() {
1919 global $wpdb, $user_ID, $_wp_using_ext_object_cache;
1921 $this->parse_query();
1923 do_action_ref_array('pre_get_posts', array(&$this));
1926 $q = &$this->query_vars;
1928 // Fill again in case pre_get_posts unset some vars.
1929 $q = $this->fill_query_vars($q);
1932 $this->meta_query = new WP_Meta_Query();
1933 $this->meta_query->parse_query_vars( $q );
1935 // Set a flag if a pre_get_posts hook changed the query vars.
1936 $hash = md5( serialize( $this->query_vars ) );
1937 if ( $hash != $this->query_vars_hash ) {
1938 $this->query_vars_changed = true;
1939 $this->query_vars_hash = $hash;
1943 // First let's clear some variables
1946 $whichmimetype = '';
1953 $post_status_join = false;
1956 if ( isset( $q['caller_get_posts'] ) ) {
1957 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
1958 if ( !isset( $q['ignore_sticky_posts'] ) )
1959 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
1962 if ( !isset( $q['ignore_sticky_posts'] ) )
1963 $q['ignore_sticky_posts'] = false;
1965 if ( !isset($q['suppress_filters']) )
1966 $q['suppress_filters'] = false;
1968 if ( !isset($q['cache_results']) ) {
1969 if ( $_wp_using_ext_object_cache )
1970 $q['cache_results'] = false;
1972 $q['cache_results'] = true;
1975 if ( !isset($q['update_post_term_cache']) )
1976 $q['update_post_term_cache'] = true;
1978 if ( !isset($q['update_post_meta_cache']) )
1979 $q['update_post_meta_cache'] = true;
1981 if ( !isset($q['post_type']) ) {
1982 if ( $this->is_search )
1983 $q['post_type'] = 'any';
1985 $q['post_type'] = '';
1987 $post_type = $q['post_type'];
1988 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1989 $q['posts_per_page'] = get_option('posts_per_page');
1990 if ( isset($q['showposts']) && $q['showposts'] ) {
1991 $q['showposts'] = (int) $q['showposts'];
1992 $q['posts_per_page'] = $q['showposts'];
1994 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1995 $q['posts_per_page'] = $q['posts_per_archive_page'];
1996 if ( !isset($q['nopaging']) ) {
1997 if ( $q['posts_per_page'] == -1 ) {
1998 $q['nopaging'] = true;
2000 $q['nopaging'] = false;
2003 if ( $this->is_feed ) {
2004 $q['posts_per_page'] = get_option('posts_per_rss');
2005 $q['nopaging'] = false;
2007 $q['posts_per_page'] = (int) $q['posts_per_page'];
2008 if ( $q['posts_per_page'] < -1 )
2009 $q['posts_per_page'] = abs($q['posts_per_page']);
2010 else if ( $q['posts_per_page'] == 0 )
2011 $q['posts_per_page'] = 1;
2013 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2014 $q['comments_per_page'] = get_option('comments_per_page');
2016 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2017 $this->is_page = true;
2018 $this->is_home = false;
2019 $q['page_id'] = get_option('page_on_front');
2022 if ( isset($q['page']) ) {
2023 $q['page'] = trim($q['page'], '/');
2024 $q['page'] = absint($q['page']);
2027 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2028 if ( isset($q['no_found_rows']) )
2029 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2031 $q['no_found_rows'] = false;
2033 switch ( $q['fields'] ) {
2035 $fields = "$wpdb->posts.ID";
2038 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2041 $fields = "$wpdb->posts.*";
2044 // If a month is specified in the querystring, load that month
2046 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
2047 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2048 if ( strlen($q['m']) > 5 )
2049 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2050 if ( strlen($q['m']) > 7 )
2051 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2052 if ( strlen($q['m']) > 9 )
2053 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2054 if ( strlen($q['m']) > 11 )
2055 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2056 if ( strlen($q['m']) > 13 )
2057 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2060 if ( '' !== $q['hour'] )
2061 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
2063 if ( '' !== $q['minute'] )
2064 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
2066 if ( '' !== $q['second'] )
2067 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
2070 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
2072 if ( $q['monthnum'] )
2073 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
2076 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
2078 // If we've got a post_type AND its not "any" post_type.
2079 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2080 foreach ( (array)$q['post_type'] as $_post_type ) {
2081 $ptype_obj = get_post_type_object($_post_type);
2082 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2085 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2086 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2087 $q['name'] = $q[ $ptype_obj->query_var ];
2089 // Hierarchical post_types will operate through the
2090 $q['pagename'] = $q[ $ptype_obj->query_var ];
2094 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2100 if ( '' != $q['name'] ) {
2101 $q['name'] = sanitize_title_for_query( $q['name'] );
2102 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2103 } elseif ( '' != $q['pagename'] ) {
2104 if ( isset($this->queried_object_id) ) {
2105 $reqpage = $this->queried_object_id;
2107 if ( 'page' != $q['post_type'] ) {
2108 foreach ( (array)$q['post_type'] as $_post_type ) {
2109 $ptype_obj = get_post_type_object($_post_type);
2110 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2113 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2119 $reqpage = get_page_by_path($q['pagename']);
2121 if ( !empty($reqpage) )
2122 $reqpage = $reqpage->ID;
2127 $page_for_posts = get_option('page_for_posts');
2128 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2129 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2130 $q['name'] = $q['pagename'];
2131 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2132 $reqpage_obj = get_page($reqpage);
2133 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2134 $this->is_attachment = true;
2135 $post_type = $q['post_type'] = 'attachment';
2136 $this->is_page = true;
2137 $q['attachment_id'] = $reqpage;
2140 } elseif ( '' != $q['attachment'] ) {
2141 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2142 $q['name'] = $q['attachment'];
2143 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2147 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
2149 if ( intval($q['comments_popup']) )
2150 $q['p'] = absint($q['comments_popup']);
2152 // If an attachment is requested by number, let it supersede any post number.
2153 if ( $q['attachment_id'] )
2154 $q['p'] = absint($q['attachment_id']);
2156 // If a post number is specified, load that post
2158 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2159 } elseif ( $q['post__in'] ) {
2160 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2161 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2162 } elseif ( $q['post__not_in'] ) {
2163 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2164 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2167 if ( is_numeric($q['post_parent']) )
2168 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2170 if ( $q['page_id'] ) {
2171 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2172 $q['p'] = $q['page_id'];
2173 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2177 // If a search pattern is specified, load the posts that match
2178 if ( !empty($q['s']) ) {
2179 // added slashes screw with quote grouping when done early, so done later
2180 $q['s'] = stripslashes($q['s']);
2181 if ( !empty($q['sentence']) ) {
2182 $q['search_terms'] = array($q['s']);
2184 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
2185 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
2187 $n = !empty($q['exact']) ? '' : '%';
2189 foreach( (array) $q['search_terms'] as $term ) {
2190 $term = esc_sql( like_escape( $term ) );
2191 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
2192 $searchand = ' AND ';
2195 if ( !empty($search) ) {
2196 $search = " AND ({$search}) ";
2197 if ( !is_user_logged_in() )
2198 $search .= " AND ($wpdb->posts.post_password = '') ";
2202 // Allow plugins to contextually add/remove/modify the search section of the database query
2203 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
2206 if ( !$this->is_singular ) {
2207 $this->parse_tax_query( $q );
2209 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2211 $join .= $clauses['join'];
2212 $where .= $clauses['where'];
2215 if ( $this->is_tax ) {
2216 if ( empty($post_type) ) {
2218 $post_status_join = true;
2219 } elseif ( in_array('attachment', (array) $post_type) ) {
2220 $post_status_join = true;
2225 if ( !empty($this->tax_query->queries) ) {
2226 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2227 if ( !empty( $tax_query_in_and ) ) {
2228 if ( !isset( $q['taxonomy'] ) ) {
2229 foreach ( $tax_query_in_and as $a_tax_query ) {
2230 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2231 $q['taxonomy'] = $a_tax_query['taxonomy'];
2232 if ( 'slug' == $a_tax_query['field'] )
2233 $q['term'] = $a_tax_query['terms'][0];
2235 $q['term_id'] = $a_tax_query['terms'][0];
2242 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2243 if ( !empty( $cat_query ) ) {
2244 $cat_query = reset( $cat_query );
2245 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2247 $this->set( 'cat', $the_cat->term_id );
2248 $this->set( 'category_name', $the_cat->slug );
2252 unset( $cat_query );
2254 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2255 if ( !empty( $tag_query ) ) {
2256 $tag_query = reset( $tag_query );
2257 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2259 $this->set( 'tag_id', $the_tag->term_id );
2263 unset( $tag_query );
2267 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2268 $groupby = "{$wpdb->posts}.ID";
2271 // Author/user stuff
2273 if ( empty($q['author']) || ($q['author'] == '0') ) {
2276 $q['author'] = (string)urldecode($q['author']);
2277 $q['author'] = addslashes_gpc($q['author']);
2278 if ( strpos($q['author'], '-') !== false ) {
2281 $q['author'] = explode('-', $q['author']);
2282 $q['author'] = (string)absint($q['author'][1]);
2287 $author_array = preg_split('/[,\s]+/', $q['author']);
2288 $_author_array = array();
2289 foreach ( $author_array as $key => $_author )
2290 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
2291 $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
2292 unset($author_array, $_author_array);
2295 // Author stuff for nice URLs
2297 if ( '' != $q['author_name'] ) {
2298 if ( strpos($q['author_name'], '/') !== false ) {
2299 $q['author_name'] = explode('/', $q['author_name']);
2300 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2301 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2303 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2306 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2307 $q['author'] = get_user_by('slug', $q['author_name']);
2309 $q['author'] = $q['author']->ID;
2310 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2313 // MIME-Type stuff for attachment browsing
2315 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
2316 $table_alias = $post_status_join ? $wpdb->posts : '';
2317 $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
2320 $where .= $search . $whichauthor . $whichmimetype;
2322 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2323 $q['order'] = 'DESC';
2326 if ( empty($q['orderby']) ) {
2327 $orderby = "$wpdb->posts.post_date " . $q['order'];
2328 } elseif ( 'none' == $q['orderby'] ) {
2331 // Used to filter values
2332 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2333 if ( !empty($q['meta_key']) ) {
2334 $allowed_keys[] = $q['meta_key'];
2335 $allowed_keys[] = 'meta_value';
2336 $allowed_keys[] = 'meta_value_num';
2338 $q['orderby'] = urldecode($q['orderby']);
2339 $q['orderby'] = addslashes_gpc($q['orderby']);
2341 $orderby_array = array();
2342 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2343 // Only allow certain values for safety
2344 if ( ! in_array($orderby, $allowed_keys) )
2347 switch ( $orderby ) {
2351 $orderby = "$wpdb->posts.ID";
2354 $orderby = 'RAND()';
2356 case $q['meta_key']:
2358 $orderby = "$wpdb->postmeta.meta_value";
2360 case 'meta_value_num':
2361 $orderby = "$wpdb->postmeta.meta_value+0";
2363 case 'comment_count':
2364 $orderby = "$wpdb->posts.comment_count";
2367 $orderby = "$wpdb->posts.post_" . $orderby;
2370 $orderby_array[] = $orderby;
2372 $orderby = implode( ',', $orderby_array );
2374 if ( empty( $orderby ) )
2375 $orderby = "$wpdb->posts.post_date ".$q['order'];
2377 $orderby .= " {$q['order']}";
2380 if ( is_array( $post_type ) ) {
2381 $post_type_cap = 'multiple_post_type';
2383 $post_type_object = get_post_type_object( $post_type );
2384 if ( empty( $post_type_object ) )
2385 $post_type_cap = $post_type;
2388 if ( 'any' == $post_type ) {
2389 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2390 if ( ! empty( $in_search_post_types ) )
2391 $where .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
2392 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2393 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2394 } elseif ( ! empty( $post_type ) ) {
2395 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2396 $post_type_object = get_post_type_object ( $post_type );
2397 } elseif ( $this->is_attachment ) {
2398 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2399 $post_type_object = get_post_type_object ( 'attachment' );
2400 } elseif ( $this->is_page ) {
2401 $where .= " AND $wpdb->posts.post_type = 'page'";
2402 $post_type_object = get_post_type_object ( 'page' );
2404 $where .= " AND $wpdb->posts.post_type = 'post'";
2405 $post_type_object = get_post_type_object ( 'post' );
2408 if ( ! empty( $post_type_object ) ) {
2409 $edit_cap = $post_type_object->cap->edit_post;
2410 $read_cap = $post_type_object->cap->read_post;
2411 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2412 $read_private_cap = $post_type_object->cap->read_private_posts;
2414 $edit_cap = 'edit_' . $post_type_cap;
2415 $read_cap = 'read_' . $post_type_cap;
2416 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2417 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2420 if ( ! empty( $q['post_status'] ) ) {
2421 $statuswheres = array();
2422 $q_status = $q['post_status'];
2423 if ( ! is_array( $q_status ) )
2424 $q_status = explode(',', $q_status);
2425 $r_status = array();
2426 $p_status = array();
2427 $e_status = array();
2428 if ( in_array('any', $q_status) ) {
2429 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2430 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2432 foreach ( get_post_stati() as $status ) {
2433 if ( in_array( $status, $q_status ) ) {
2434 if ( 'private' == $status )
2435 $p_status[] = "$wpdb->posts.post_status = '$status'";
2437 $r_status[] = "$wpdb->posts.post_status = '$status'";
2442 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2443 $r_status = array_merge($r_status, $p_status);
2447 if ( !empty($e_status) ) {
2448 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2450 if ( !empty($r_status) ) {
2451 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2452 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2454 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2456 if ( !empty($p_status) ) {
2457 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2458 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2460 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2462 if ( $post_status_join ) {
2463 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2464 foreach ( $statuswheres as $index => $statuswhere )
2465 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2467 foreach ( $statuswheres as $statuswhere )
2468 $where .= " AND $statuswhere";
2469 } elseif ( !$this->is_singular ) {
2470 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2472 // Add public states.
2473 $public_states = get_post_stati( array('public' => true) );
2474 foreach ( (array) $public_states as $state ) {
2475 if ( 'publish' == $state ) // Publish is hard-coded above.
2477 $where .= " OR $wpdb->posts.post_status = '$state'";
2481 // Add protected states that should show in the admin all list.
2482 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2483 foreach ( (array) $admin_all_states as $state )
2484 $where .= " OR $wpdb->posts.post_status = '$state'";
2487 if ( is_user_logged_in() ) {
2488 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2489 $private_states = get_post_stati( array('private' => true) );
2490 foreach ( (array) $private_states as $state )
2491 $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'";
2497 if ( !empty( $this->meta_query->queries ) ) {
2498 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2499 $join .= $clauses['join'];
2500 $where .= $clauses['where'];
2503 // Apply filters on where and join prior to paging so that any
2504 // manipulations to them are reflected in the paging by day queries.
2505 if ( !$q['suppress_filters'] ) {
2506 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2507 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2511 if ( empty($q['nopaging']) && !$this->is_singular ) {
2512 $page = absint($q['paged']);
2516 if ( empty($q['offset']) ) {
2518 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2519 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2520 } else { // we're ignoring $page and using 'offset'
2521 $q['offset'] = absint($q['offset']);
2522 $pgstrt = $q['offset'] . ', ';
2523 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2528 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2529 if ( $this->is_archive || $this->is_search ) {
2530 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2531 $cwhere = "WHERE comment_approved = '1' $where";
2532 $cgroupby = "$wpdb->comments.comment_id";
2533 } else { // Other non singular e.g. front
2534 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2535 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2539 if ( !$q['suppress_filters'] ) {
2540 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2541 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2542 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2543 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2544 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2546 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2547 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2549 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2550 $this->comment_count = count($this->comments);
2552 $post_ids = array();
2554 foreach ( $this->comments as $comment )
2555 $post_ids[] = (int) $comment->comment_post_ID;
2557 $post_ids = join(',', $post_ids);
2560 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2565 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2567 // Apply post-paging filters on where and join. Only plugins that
2568 // manipulate paging queries should use these hooks.
2569 if ( !$q['suppress_filters'] ) {
2570 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2571 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2572 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2573 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2574 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2575 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2576 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2578 // Filter all clauses at once, for convenience
2579 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2580 foreach ( $pieces as $piece )
2581 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2584 // Announce current selection parameters. For use by caching plugins.
2585 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2587 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2588 if ( !$q['suppress_filters'] ) {
2589 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2590 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2591 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2592 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2593 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2594 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2595 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2597 // Filter all clauses at once, for convenience
2598 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2599 foreach ( $pieces as $piece )
2600 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2603 if ( ! empty($groupby) )
2604 $groupby = 'GROUP BY ' . $groupby;
2605 if ( !empty( $orderby ) )
2606 $orderby = 'ORDER BY ' . $orderby;
2609 if ( !$q['no_found_rows'] && !empty($limits) )
2610 $found_rows = 'SQL_CALC_FOUND_ROWS';
2612 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2613 if ( !$q['suppress_filters'] )
2614 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
2616 if ( 'ids' == $q['fields'] ) {
2617 $this->posts = $wpdb->get_col($this->request);
2619 return $this->posts;
2622 if ( 'id=>parent' == $q['fields'] ) {
2623 $this->posts = $wpdb->get_results($this->request);
2626 foreach ( $this->posts as $post )
2627 $r[ $post->ID ] = $post->post_parent;
2632 $this->posts = $wpdb->get_results($this->request);
2634 // Raw results filter. Prior to status checks.
2635 if ( !$q['suppress_filters'] )
2636 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2638 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2639 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2640 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2641 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2642 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2643 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2644 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2645 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2646 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2647 $this->comments = $wpdb->get_results($comments_request);
2648 $this->comment_count = count($this->comments);
2651 if ( !$q['no_found_rows'] && !empty($limits) ) {
2652 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
2653 $this->found_posts = $wpdb->get_var( $found_posts_query );
2654 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
2655 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
2658 // Check post status to determine if post should be displayed.
2659 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2660 $status = get_post_status($this->posts[0]);
2661 $post_status_obj = get_post_status_object($status);
2662 //$type = get_post_type($this->posts[0]);
2663 if ( !$post_status_obj->public ) {
2664 if ( ! is_user_logged_in() ) {
2665 // User must be logged in to view unpublished posts.
2666 $this->posts = array();
2668 if ( $post_status_obj->protected ) {
2669 // User must have edit permissions on the draft to preview.
2670 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2671 $this->posts = array();
2673 $this->is_preview = true;
2674 if ( 'future' != $status )
2675 $this->posts[0]->post_date = current_time('mysql');
2677 } elseif ( $post_status_obj->private ) {
2678 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2679 $this->posts = array();
2681 $this->posts = array();
2686 if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2687 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
2690 // Put sticky posts at the top of the posts array
2691 $sticky_posts = get_option('sticky_posts');
2692 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2693 $num_posts = count($this->posts);
2695 // Loop over posts and relocate stickies to the front.
2696 for ( $i = 0; $i < $num_posts; $i++ ) {
2697 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2698 $sticky_post = $this->posts[$i];
2699 // Remove sticky from current position
2700 array_splice($this->posts, $i, 1);
2701 // Move to front, after other stickies
2702 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2703 // Increment the sticky offset. The next sticky will be placed at this offset.
2705 // Remove post from sticky posts array
2706 $offset = array_search($sticky_post->ID, $sticky_posts);
2707 unset( $sticky_posts[$offset] );
2711 // If any posts have been excluded specifically, Ignore those that are sticky.
2712 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
2713 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
2715 // Fetch sticky posts that weren't in the query results
2716 if ( !empty($sticky_posts) ) {
2717 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2718 // honor post type(s) if not set to any
2719 $stickies_where = '';
2720 if ( 'any' != $post_type && '' != $post_type ) {
2721 if ( is_array( $post_type ) ) {
2722 $post_types = join( "', '", $post_type );
2724 $post_types = $post_type;
2726 $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
2729 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
2730 foreach ( $stickies as $sticky_post ) {
2731 // Ignore sticky posts the current user cannot read or are not published.
2732 if ( 'publish' != $sticky_post->post_status )
2734 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2740 if ( !$q['suppress_filters'] )
2741 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
2743 $this->post_count = count($this->posts);
2746 foreach ( $this->posts as $i => $post ) {
2747 $this->posts[$i] = sanitize_post( $post, 'raw' );
2750 if ( $q['cache_results'] )
2751 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
2753 if ( $this->post_count > 0 ) {
2754 $this->post = $this->posts[0];
2757 return $this->posts;
2761 * Set up the next post and iterate current post index.
2766 * @return object Next post.
2768 function next_post() {
2770 $this->current_post++;
2772 $this->post = $this->posts[$this->current_post];
2777 * Sets up the current post.
2779 * Retrieves the next post, sets up the post, sets the 'in the loop'
2785 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2787 function the_post() {
2789 $this->in_the_loop = true;
2791 if ( $this->current_post == -1 ) // loop has just started
2792 do_action_ref_array('loop_start', array(&$this));
2794 $post = $this->next_post();
2795 setup_postdata($post);
2799 * Whether there are more posts available in the loop.
2801 * Calls action 'loop_end', when the loop is complete.
2805 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2807 * @return bool True if posts are available, false if end of loop.
2809 function have_posts() {
2810 if ( $this->current_post + 1 < $this->post_count ) {
2812 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
2813 do_action_ref_array('loop_end', array(&$this));
2814 // Do some cleaning up after the loop
2815 $this->rewind_posts();
2818 $this->in_the_loop = false;
2823 * Rewind the posts and reset post index.
2828 function rewind_posts() {
2829 $this->current_post = -1;
2830 if ( $this->post_count > 0 ) {
2831 $this->post = $this->posts[0];
2836 * Iterate current comment index and return comment object.
2841 * @return object Comment object.
2843 function next_comment() {
2844 $this->current_comment++;
2846 $this->comment = $this->comments[$this->current_comment];
2847 return $this->comment;
2851 * Sets up the current comment.
2855 * @global object $comment Current comment.
2856 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2858 function the_comment() {
2861 $comment = $this->next_comment();
2863 if ( $this->current_comment == 0 ) {
2864 do_action('comment_loop_start');
2869 * Whether there are more comments available.
2871 * Automatically rewinds comments when finished.
2876 * @return bool True, if more comments. False, if no more posts.
2878 function have_comments() {
2879 if ( $this->current_comment + 1 < $this->comment_count ) {
2881 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
2882 $this->rewind_comments();
2889 * Rewind the comments, resets the comment index and comment to first.
2894 function rewind_comments() {
2895 $this->current_comment = -1;
2896 if ( $this->comment_count > 0 ) {
2897 $this->comment = $this->comments[0];
2902 * Sets up the WordPress query by parsing query string.
2907 * @param string $query URL query string.
2908 * @return array List of posts.
2910 function &query( $query ) {
2912 $this->query = $this->query_vars = wp_parse_args( $query );
2913 return $this->get_posts();
2917 * Retrieve queried object.
2919 * If queried object is not set, then the queried object will be set from
2920 * the category, tag, taxonomy, posts page, single post, page, or author
2921 * query variable. After it is set up, it will be returned.
2928 function get_queried_object() {
2929 if ( isset($this->queried_object) )
2930 return $this->queried_object;
2932 $this->queried_object = NULL;
2933 $this->queried_object_id = 0;
2935 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
2936 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2938 $query = reset( $tax_query_in_and );
2940 if ( 'term_id' == $query['field'] )
2941 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
2943 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
2945 if ( $term && ! is_wp_error($term) ) {
2946 $this->queried_object = $term;
2947 $this->queried_object_id = (int) $term->term_id;
2949 if ( $this->is_category )
2950 _make_cat_compat( $this->queried_object );
2952 } elseif ( $this->is_post_type_archive ) {
2953 $this->queried_object = get_post_type_object( $this->get('post_type') );
2954 } elseif ( $this->is_posts_page ) {
2955 $page_for_posts = get_option('page_for_posts');
2956 $this->queried_object = & get_page( $page_for_posts );
2957 $this->queried_object_id = (int) $this->queried_object->ID;
2958 } elseif ( $this->is_singular && !is_null($this->post) ) {
2959 $this->queried_object = $this->post;
2960 $this->queried_object_id = (int) $this->post->ID;
2961 } elseif ( $this->is_author ) {
2962 $this->queried_object_id = (int) $this->get('author');
2963 $this->queried_object = get_userdata( $this->queried_object_id );
2966 return $this->queried_object;
2970 * Retrieve ID of the current queried object.
2977 function get_queried_object_id() {
2978 $this->get_queried_object();
2980 if ( isset($this->queried_object_id) ) {
2981 return $this->queried_object_id;
2990 * Sets up the WordPress query, if parameter is not empty.
2995 * @param string $query URL query string.
2998 function __construct($query = '') {
2999 if ( ! empty($query) ) {
3000 $this->query($query);
3005 * Is the query for an archive page?
3007 * Month, Year, Category, Author, Post Type archive...
3013 function is_archive() {
3014 return (bool) $this->is_archive;
3018 * Is the query for a post type archive page?
3022 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3025 function is_post_type_archive( $post_types = '' ) {
3026 if ( empty( $post_types ) || !$this->is_post_type_archive )
3027 return (bool) $this->is_post_type_archive;
3029 $post_type_object = $this->get_queried_object();
3031 return in_array( $post_type_object->name, (array) $post_types );
3035 * Is the query for an attachment page?
3041 function is_attachment() {
3042 return (bool) $this->is_attachment;
3046 * Is the query for an author archive page?
3048 * If the $author parameter is specified, this function will additionally
3049 * check if the query is for one of the authors specified.
3053 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3056 function is_author( $author = '' ) {
3057 if ( !$this->is_author )
3060 if ( empty($author) )
3063 $author_obj = $this->get_queried_object();
3065 $author = (array) $author;
3067 if ( in_array( $author_obj->ID, $author ) )
3069 elseif ( in_array( $author_obj->nickname, $author ) )
3071 elseif ( in_array( $author_obj->user_nicename, $author ) )
3078 * Is the query for a category archive page?
3080 * If the $category parameter is specified, this function will additionally
3081 * check if the query is for one of the categories specified.
3085 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3088 function is_category( $category = '' ) {
3089 if ( !$this->is_category )
3092 if ( empty($category) )
3095 $cat_obj = $this->get_queried_object();
3097 $category = (array) $category;
3099 if ( in_array( $cat_obj->term_id, $category ) )
3101 elseif ( in_array( $cat_obj->name, $category ) )
3103 elseif ( in_array( $cat_obj->slug, $category ) )
3110 * Is the query for a tag archive page?
3112 * If the $tag parameter is specified, this function will additionally
3113 * check if the query is for one of the tags specified.
3117 * @param mixed $slug Optional. Tag slug or array of slugs.
3120 function is_tag( $slug = '' ) {
3121 if ( !$this->is_tag )
3124 if ( empty( $slug ) )
3127 $tag_obj = $this->get_queried_object();
3129 $slug = (array) $slug;
3131 if ( in_array( $tag_obj->slug, $slug ) )
3138 * Is the query for a taxonomy archive page?
3140 * If the $taxonomy parameter is specified, this function will additionally
3141 * check if the query is for that specific $taxonomy.
3143 * If the $term parameter is specified in addition to the $taxonomy parameter,
3144 * this function will additionally check if the query is for one of the terms
3149 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3150 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3153 function is_tax( $taxonomy = '', $term = '' ) {
3154 global $wp_taxonomies;
3156 if ( !$this->is_tax )
3159 if ( empty( $taxonomy ) )
3162 $queried_object = $this->get_queried_object();
3163 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3164 $term_array = (array) $term;
3166 if ( empty( $term ) ) // Only a Taxonomy provided
3167 return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
3169 return isset( $queried_object->term_id ) &&
3170 count( array_intersect(
3171 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3177 * Whether the current URL is within the comments popup window.
3183 function is_comments_popup() {
3184 return (bool) $this->is_comments_popup;
3188 * Is the query for a date archive?
3194 function is_date() {
3195 return (bool) $this->is_date;
3200 * Is the query for a day archive?
3207 return (bool) $this->is_day;
3211 * Is the query for a feed?
3215 * @param string|array $feeds Optional feed types to check.
3218 function is_feed( $feeds = '' ) {
3219 if ( empty( $feeds ) || ! $this->is_feed )
3220 return (bool) $this->is_feed;
3221 $qv = $this->get( 'feed' );
3222 if ( 'feed' == $qv )
3223 $qv = get_default_feed();
3224 return in_array( $qv, (array) $feeds );
3228 * Is the query for a comments feed?
3234 function is_comment_feed() {
3235 return (bool) $this->is_comment_feed;
3239 * Is the query for the front page of the site?
3241 * This is for what is displayed at your site's main URL.
3243 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3245 * If you set a static page for the front page of your site, this function will return
3246 * true when viewing that page.
3248 * Otherwise the same as @see WP_Query::is_home()
3252 * @uses get_option()
3254 * @return bool True, if front of site.
3256 function is_front_page() {
3258 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3260 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3267 * Is the query for the blog homepage?
3269 * This is the page which shows the time based blog content of your site.
3271 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3273 * If you set a static page for the front page of your site, this function will return
3274 * true only on the page you set as the "Posts page".
3276 * @see WP_Query::is_front_page()
3280 * @return bool True if blog view homepage.
3282 function is_home() {
3283 return (bool) $this->is_home;
3287 * Is the query for a month archive?
3293 function is_month() {
3294 return (bool) $this->is_month;
3298 * Is the query for a single page?
3300 * If the $page parameter is specified, this function will additionally
3301 * check if the query is for one of the pages specified.
3303 * @see WP_Query::is_single()
3304 * @see WP_Query::is_singular()
3308 * @param mixed $page Page ID, title, slug, or array of such.
3311 function is_page( $page = '' ) {
3312 if ( !$this->is_page )
3315 if ( empty( $page ) )
3318 $page_obj = $this->get_queried_object();
3320 $page = (array) $page;
3322 if ( in_array( $page_obj->ID, $page ) )
3324 elseif ( in_array( $page_obj->post_title, $page ) )
3326 else if ( in_array( $page_obj->post_name, $page ) )
3333 * Is the query for paged result and not for the first page?
3339 function is_paged() {
3340 return (bool) $this->is_paged;
3344 * Is the query for a post or page preview?
3350 function is_preview() {
3351 return (bool) $this->is_preview;
3355 * Is the query for the robots file?
3361 function is_robots() {
3362 return (bool) $this->is_robots;
3366 * Is the query for a search?
3372 function is_search() {
3373 return (bool) $this->is_search;
3377 * Is the query for a single post?
3379 * Works for any post type, except attachments and pages
3381 * If the $post parameter is specified, this function will additionally
3382 * check if the query is for one of the Posts specified.
3384 * @see WP_Query::is_page()
3385 * @see WP_Query::is_singular()
3389 * @param mixed $post Post ID, title, slug, or array of such.
3392 function is_single( $post = '' ) {
3393 if ( !$this->is_single )
3399 $post_obj = $this->get_queried_object();
3401 $post = (array) $post;
3403 if ( in_array( $post_obj->ID, $post ) )
3405 elseif ( in_array( $post_obj->post_title, $post ) )
3407 elseif ( in_array( $post_obj->post_name, $post ) )
3414 * Is the query for a single post of any post type (post, attachment, page, ... )?
3416 * If the $post_types parameter is specified, this function will additionally
3417 * check if the query is for one of the Posts Types specified.
3419 * @see WP_Query::is_page()
3420 * @see WP_Query::is_single()
3424 * @param mixed $post_types Optional. Post Type or array of Post Types
3427 function is_singular( $post_types = '' ) {
3428 if ( empty( $post_types ) || !$this->is_singular )
3429 return (bool) $this->is_singular;
3431 $post_obj = $this->get_queried_object();
3433 return in_array( $post_obj->post_type, (array) $post_types );
3437 * Is the query for a specific time?
3443 function is_time() {
3444 return (bool) $this->is_time;
3448 * Is the query for a trackback endpoint call?
3454 function is_trackback() {
3455 return (bool) $this->is_trackback;
3459 * Is the query for a specific year?
3465 function is_year() {
3466 return (bool) $this->is_year;
3470 * Is the query a 404 (returns no results)?
3477 return (bool) $this->is_404;
3481 * Is the query the main query?
3487 function is_main_query() {
3488 global $wp_the_query;
3489 return $wp_the_query === $this;
3494 * Redirect old slugs to the correct permalink.
3496 * Attempts to find the current slug from the past slugs.
3502 * @return null If no link is found, null is returned.
3504 function wp_old_slug_redirect() {
3506 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3509 // Guess the current post_type based on the query vars.
3510 if ( get_query_var('post_type') )
3511 $post_type = get_query_var('post_type');
3512 elseif ( !empty($wp_query->query_vars['pagename']) )
3513 $post_type = 'page';
3515 $post_type = 'post';
3517 if ( is_array( $post_type ) ) {
3518 if ( count( $post_type ) > 1 )
3520 $post_type = array_shift( $post_type );
3523 // Do not attempt redirect for hierarchical post types
3524 if ( is_post_type_hierarchical( $post_type ) )
3527 $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']);
3529 // if year, monthnum, or day have been specified, make our query more precise
3530 // just in case there are multiple identical _wp_old_slug values
3531 if ( '' != $wp_query->query_vars['year'] )
3532 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3533 if ( '' != $wp_query->query_vars['monthnum'] )
3534 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3535 if ( '' != $wp_query->query_vars['day'] )
3536 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3538 $id = (int) $wpdb->get_var($query);
3543 $link = get_permalink($id);
3548 wp_redirect( $link, 301 ); // Permanent redirect
3554 * Set up global post data.
3558 * @param object $post Post data.
3559 * @uses do_action_ref_array() Calls 'the_post'
3560 * @return bool True when finished.
3562 function setup_postdata($post) {
3563 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3565 $id = (int) $post->ID;
3567 $authordata = get_userdata($post->post_author);
3569 $currentday = mysql2date('d.m.y', $post->post_date, false);
3570 $currentmonth = mysql2date('m', $post->post_date, false);
3572 $page = get_query_var('page');
3575 if ( is_single() || is_page() || is_feed() )
3577 $content = $post->post_content;
3578 if ( strpos( $content, '<!--nextpage-->' ) ) {
3582 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
3583 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
3584 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
3585 $pages = explode('<!--nextpage-->', $content);
3586 $numpages = count($pages);
3588 $pages = array( $post->post_content );
3592 do_action_ref_array('the_post', array(&$post));