5 * The query API attempts to get which part of WordPress the user is on. It
6 * also provides functionality for 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.
22 * @param mixed $default Value to return if the query variable is not set. Default ''.
25 function get_query_var( $var, $default = '' ) {
28 return $wp_query->get( $var, $default );
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 $GLOBALS['wp_query'] = new WP_Query();
92 return $GLOBALS['wp_query']->query($query);
96 * Destroy the previous query and set up a new query.
98 * This should be used after {@link query_posts()} and before another {@link
99 * query_posts()}. This will remove obscure bugs that occur when the previous
100 * wp_query object is not destroyed properly before another is set up.
105 function wp_reset_query() {
106 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
111 * After looping through a separate query, this function restores
112 * the $post global to the current post in the main query.
117 function wp_reset_postdata() {
120 if ( isset( $wp_query ) ) {
121 $wp_query->reset_postdata();
130 * Is the query for an existing archive page?
132 * Month, Year, Category, Author, Post Type archive...
134 * @see WP_Query::is_archive()
140 function is_archive() {
143 if ( ! isset( $wp_query ) ) {
144 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
148 return $wp_query->is_archive();
152 * Is the query for an existing post type archive page?
154 * @see WP_Query::is_post_type_archive()
158 * @param mixed $post_types Optional. Post type or array of posts types to check against.
161 function is_post_type_archive( $post_types = '' ) {
164 if ( ! isset( $wp_query ) ) {
165 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
169 return $wp_query->is_post_type_archive( $post_types );
173 * Is the query for an existing attachment page?
175 * @see WP_Query::is_attachment()
179 * @param mixed $attachment Attachment ID, title, slug, or array of such.
182 function is_attachment( $attachment = '' ) {
185 if ( ! isset( $wp_query ) ) {
186 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
190 return $wp_query->is_attachment( $attachment );
194 * Is the query for an existing author archive page?
196 * If the $author parameter is specified, this function will additionally
197 * check if the query is for one of the authors specified.
199 * @see WP_Query::is_author()
203 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
206 function is_author( $author = '' ) {
209 if ( ! isset( $wp_query ) ) {
210 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
214 return $wp_query->is_author( $author );
218 * Is the query for an existing category archive page?
220 * If the $category parameter is specified, this function will additionally
221 * check if the query is for one of the categories specified.
223 * @see WP_Query::is_category()
227 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
230 function is_category( $category = '' ) {
233 if ( ! isset( $wp_query ) ) {
234 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
238 return $wp_query->is_category( $category );
242 * Is the query for an existing tag archive page?
244 * If the $tag parameter is specified, this function will additionally
245 * check if the query is for one of the tags specified.
247 * @see WP_Query::is_tag()
251 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
254 function is_tag( $tag = '' ) {
257 if ( ! isset( $wp_query ) ) {
258 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
262 return $wp_query->is_tag( $tag );
266 * Is the query for an existing taxonomy archive page?
268 * If the $taxonomy parameter is specified, this function will additionally
269 * check if the query is for that specific $taxonomy.
271 * If the $term parameter is specified in addition to the $taxonomy parameter,
272 * this function will additionally check if the query is for one of the terms
275 * @see WP_Query::is_tax()
279 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
280 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
283 function is_tax( $taxonomy = '', $term = '' ) {
286 if ( ! isset( $wp_query ) ) {
287 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
291 return $wp_query->is_tax( $taxonomy, $term );
295 * Whether the current URL is within the comments popup window.
297 * @see WP_Query::is_comments_popup()
303 function is_comments_popup() {
306 if ( ! isset( $wp_query ) ) {
307 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
311 return $wp_query->is_comments_popup();
315 * Is the query for an existing date archive?
317 * @see WP_Query::is_date()
326 if ( ! isset( $wp_query ) ) {
327 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
331 return $wp_query->is_date();
335 * Is the query for an existing day archive?
337 * @see WP_Query::is_day()
346 if ( ! isset( $wp_query ) ) {
347 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
351 return $wp_query->is_day();
355 * Is the query for a feed?
357 * @see WP_Query::is_feed()
361 * @param string|array $feeds Optional feed types to check.
364 function is_feed( $feeds = '' ) {
367 if ( ! isset( $wp_query ) ) {
368 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
372 return $wp_query->is_feed( $feeds );
376 * Is the query for a comments feed?
378 * @see WP_Query::is_comments_feed()
384 function is_comment_feed() {
387 if ( ! isset( $wp_query ) ) {
388 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
392 return $wp_query->is_comment_feed();
396 * Is the query for the front page of the site?
398 * This is for what is displayed at your site's main URL.
400 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
402 * If you set a static page for the front page of your site, this function will return
403 * true when viewing that page.
405 * Otherwise the same as @see is_home()
407 * @see WP_Query::is_front_page()
412 * @return bool True, if front of site.
414 function is_front_page() {
417 if ( ! isset( $wp_query ) ) {
418 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
422 return $wp_query->is_front_page();
426 * Is the query for the blog homepage?
428 * This is the page which shows the time based blog content of your site.
430 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
432 * If you set a static page for the front page of your site, this function will return
433 * true only on the page you set as the "Posts page".
435 * @see is_front_page()
437 * @see WP_Query::is_home()
441 * @return bool True if blog view homepage.
446 if ( ! isset( $wp_query ) ) {
447 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
451 return $wp_query->is_home();
455 * Is the query for an existing month archive?
457 * @see WP_Query::is_month()
463 function is_month() {
466 if ( ! isset( $wp_query ) ) {
467 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
471 return $wp_query->is_month();
475 * Is the query for an existing single page?
477 * If the $page parameter is specified, this function will additionally
478 * check if the query is for one of the pages specified.
483 * @see WP_Query::is_page()
487 * @param mixed $page Page ID, title, slug, or array of such.
490 function is_page( $page = '' ) {
493 if ( ! isset( $wp_query ) ) {
494 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
498 return $wp_query->is_page( $page );
502 * Is the query for paged result and not for the first page?
504 * @see WP_Query::is_paged()
510 function is_paged() {
513 if ( ! isset( $wp_query ) ) {
514 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
518 return $wp_query->is_paged();
522 * Is the query for a post or page preview?
524 * @see WP_Query::is_preview()
530 function is_preview() {
533 if ( ! isset( $wp_query ) ) {
534 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
538 return $wp_query->is_preview();
542 * Is the query for the robots file?
544 * @see WP_Query::is_robots()
550 function is_robots() {
553 if ( ! isset( $wp_query ) ) {
554 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
558 return $wp_query->is_robots();
562 * Is the query for a search?
564 * @see WP_Query::is_search()
570 function is_search() {
573 if ( ! isset( $wp_query ) ) {
574 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
578 return $wp_query->is_search();
582 * Is the query for an existing single post?
584 * Works for any post type, except attachments and pages
586 * If the $post parameter is specified, this function will additionally
587 * check if the query is for one of the Posts specified.
592 * @see WP_Query::is_single()
596 * @param mixed $post Post ID, title, slug, or array of such.
599 function is_single( $post = '' ) {
602 if ( ! isset( $wp_query ) ) {
603 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
607 return $wp_query->is_single( $post );
611 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
613 * If the $post_types parameter is specified, this function will additionally
614 * check if the query is for one of the Posts Types specified.
619 * @see WP_Query::is_singular()
623 * @param mixed $post_types Optional. Post Type or array of Post Types
626 function is_singular( $post_types = '' ) {
629 if ( ! isset( $wp_query ) ) {
630 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
634 return $wp_query->is_singular( $post_types );
638 * Is the query for a specific time?
640 * @see WP_Query::is_time()
649 if ( ! isset( $wp_query ) ) {
650 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
654 return $wp_query->is_time();
658 * Is the query for a trackback endpoint call?
660 * @see WP_Query::is_trackback()
666 function is_trackback() {
669 if ( ! isset( $wp_query ) ) {
670 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
674 return $wp_query->is_trackback();
678 * Is the query for an existing year archive?
680 * @see WP_Query::is_year()
689 if ( ! isset( $wp_query ) ) {
690 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
694 return $wp_query->is_year();
698 * Is the query a 404 (returns no results)?
700 * @see WP_Query::is_404()
709 if ( ! isset( $wp_query ) ) {
710 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
714 return $wp_query->is_404();
718 * Is the query the main query?
724 function is_main_query() {
725 if ( 'pre_get_posts' === current_filter() ) {
726 $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
727 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
728 _doing_it_wrong( __FUNCTION__, $message, '3.7' );
732 return $wp_query->is_main_query();
736 * The Loop. Post loop control.
740 * Whether current WordPress query has results to loop over.
742 * @see WP_Query::have_posts()
748 function have_posts() {
751 return $wp_query->have_posts();
755 * Whether the caller is in the Loop.
760 * @return bool True if caller is within loop, false if loop hasn't started or ended.
762 function in_the_loop() {
765 return $wp_query->in_the_loop;
769 * Rewind the loop posts.
771 * @see WP_Query::rewind_posts()
777 function rewind_posts() {
780 return $wp_query->rewind_posts();
784 * Iterate the post index in the loop.
786 * @see WP_Query::the_post()
790 function the_post() {
793 $wp_query->the_post();
801 * Whether there are comments to loop over.
803 * @see WP_Query::have_comments()
809 function have_comments() {
811 return $wp_query->have_comments();
815 * Iterate comment index in the comment loop.
817 * @see WP_Query::the_comment()
823 function the_comment() {
825 return $wp_query->the_comment();
833 * The WordPress Query class.
835 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
842 * Query vars set by the user
851 * Query vars, after parsing
857 var $query_vars = array();
860 * Taxonomy query, as passed to get_tax_sql()
864 * @var object WP_Tax_Query
869 * Metadata query container
873 * @var object WP_Meta_Query
875 var $meta_query = false;
878 * Date query container
882 * @var object WP_Date_Query
884 var $date_query = false;
887 * Holds the data for a single object that is queried.
889 * Holds the contents of a post, page, category, attachment.
898 * The ID of the queried object.
904 var $queried_object_id;
907 * Get post database query.
925 * The amount of posts for the current query.
934 * Index of the current item in the loop.
940 var $current_post = -1;
943 * Whether the loop has started and the caller is in the loop.
949 var $in_the_loop = false;
961 * The list of comments for current post.
970 * The amount of comments for the posts.
976 var $comment_count = 0;
979 * The index of the comment in the comment loop.
985 var $current_comment = -1;
988 * Current comment ID.
997 * The amount of found posts for the current query.
999 * If limit clause was not used, equals $post_count.
1005 var $found_posts = 0;
1008 * The amount of pages.
1014 var $max_num_pages = 0;
1017 * The amount of comment pages.
1023 var $max_num_comment_pages = 0;
1026 * Set if query is single post.
1032 var $is_single = false;
1035 * Set if query is preview of blog.
1041 var $is_preview = false;
1044 * Set if query returns a page.
1050 var $is_page = false;
1053 * Set if query is an archive list.
1059 var $is_archive = false;
1062 * Set if query is part of a date.
1068 var $is_date = false;
1071 * Set if query contains a year.
1077 var $is_year = false;
1080 * Set if query contains a month.
1086 var $is_month = false;
1089 * Set if query contains a day.
1095 var $is_day = false;
1098 * Set if query contains time.
1104 var $is_time = false;
1107 * Set if query contains an author.
1113 var $is_author = false;
1116 * Set if query contains category.
1122 var $is_category = false;
1125 * Set if query contains tag.
1131 var $is_tag = false;
1134 * Set if query contains taxonomy.
1140 var $is_tax = false;
1143 * Set if query was part of a search result.
1149 var $is_search = false;
1152 * Set if query is feed display.
1158 var $is_feed = false;
1161 * Set if query is comment feed display.
1167 var $is_comment_feed = false;
1170 * Set if query is trackback.
1176 var $is_trackback = false;
1179 * Set if query is blog homepage.
1185 var $is_home = false;
1188 * Set if query couldn't found anything.
1194 var $is_404 = false;
1197 * Set if query is within comments popup window.
1203 var $is_comments_popup = false;
1206 * Set if query is paged
1212 var $is_paged = false;
1215 * Set if query is part of administration page.
1221 var $is_admin = false;
1224 * Set if query is an attachment.
1230 var $is_attachment = false;
1233 * Set if is single, is a page, or is an attachment.
1239 var $is_singular = false;
1242 * Set if query is for robots.
1248 var $is_robots = false;
1251 * Set if query contains posts.
1253 * Basically, the homepage if the option isn't set for the static homepage.
1259 var $is_posts_page = false;
1262 * Set if query is for a post type archive.
1268 var $is_post_type_archive = false;
1271 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1272 * whether we have to re-parse because something has changed
1277 var $query_vars_hash = false;
1280 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1281 * via pre_get_posts hooks.
1286 var $query_vars_changed = true;
1289 * Set if post thumbnails are cached
1295 var $thumbnails_cached = false;
1298 * Cached list of search stopwords.
1306 * Resets query flags to false.
1308 * The query flags are what page info WordPress was able to figure out.
1313 function init_query_flags() {
1314 $this->is_single = false;
1315 $this->is_preview = false;
1316 $this->is_page = false;
1317 $this->is_archive = false;
1318 $this->is_date = false;
1319 $this->is_year = false;
1320 $this->is_month = false;
1321 $this->is_day = false;
1322 $this->is_time = false;
1323 $this->is_author = false;
1324 $this->is_category = false;
1325 $this->is_tag = false;
1326 $this->is_tax = false;
1327 $this->is_search = false;
1328 $this->is_feed = false;
1329 $this->is_comment_feed = false;
1330 $this->is_trackback = false;
1331 $this->is_home = false;
1332 $this->is_404 = false;
1333 $this->is_comments_popup = false;
1334 $this->is_paged = false;
1335 $this->is_admin = false;
1336 $this->is_attachment = false;
1337 $this->is_singular = false;
1338 $this->is_robots = false;
1339 $this->is_posts_page = false;
1340 $this->is_post_type_archive = false;
1344 * Initiates object properties and sets default values.
1350 unset($this->posts);
1351 unset($this->query);
1352 $this->query_vars = array();
1353 unset($this->queried_object);
1354 unset($this->queried_object_id);
1355 $this->post_count = 0;
1356 $this->current_post = -1;
1357 $this->in_the_loop = false;
1358 unset( $this->request );
1359 unset( $this->post );
1360 unset( $this->comments );
1361 unset( $this->comment );
1362 $this->comment_count = 0;
1363 $this->current_comment = -1;
1364 $this->found_posts = 0;
1365 $this->max_num_pages = 0;
1366 $this->max_num_comment_pages = 0;
1368 $this->init_query_flags();
1372 * Reparse the query vars.
1377 function parse_query_vars() {
1378 $this->parse_query();
1382 * Fills in the query variables, which do not exist within the parameter.
1387 * @param array $array Defined query variables.
1388 * @return array Complete query variables with undefined ones filled in empty.
1390 function fill_query_vars($array) {
1430 foreach ( $keys as $key ) {
1431 if ( !isset($array[$key]) )
1435 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1436 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
1437 'author__in', 'author__not_in' );
1439 foreach ( $array_keys as $key ) {
1440 if ( !isset($array[$key]) )
1441 $array[$key] = array();
1447 * Parse a query string and set query type booleans.
1452 * @param string|array $query Optional query.
1454 function parse_query( $query = '' ) {
1455 if ( ! empty( $query ) ) {
1457 $this->query = $this->query_vars = wp_parse_args( $query );
1458 } elseif ( ! isset( $this->query ) ) {
1459 $this->query = $this->query_vars;
1462 $this->query_vars = $this->fill_query_vars($this->query_vars);
1463 $qv = &$this->query_vars;
1464 $this->query_vars_changed = true;
1466 if ( ! empty($qv['robots']) )
1467 $this->is_robots = true;
1469 $qv['p'] = absint($qv['p']);
1470 $qv['page_id'] = absint($qv['page_id']);
1471 $qv['year'] = absint($qv['year']);
1472 $qv['monthnum'] = absint($qv['monthnum']);
1473 $qv['day'] = absint($qv['day']);
1474 $qv['w'] = absint($qv['w']);
1475 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
1476 $qv['paged'] = absint($qv['paged']);
1477 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1478 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
1479 $qv['pagename'] = trim( $qv['pagename'] );
1480 $qv['name'] = trim( $qv['name'] );
1481 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1482 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1483 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1484 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1486 // Fairly insane upper bound for search string lengths.
1487 if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
1490 // Compat. Map subpost to attachment.
1491 if ( '' != $qv['subpost'] )
1492 $qv['attachment'] = $qv['subpost'];
1493 if ( '' != $qv['subpost_id'] )
1494 $qv['attachment_id'] = $qv['subpost_id'];
1496 $qv['attachment_id'] = absint($qv['attachment_id']);
1498 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1499 $this->is_single = true;
1500 $this->is_attachment = true;
1501 } elseif ( '' != $qv['name'] ) {
1502 $this->is_single = true;
1503 } elseif ( $qv['p'] ) {
1504 $this->is_single = true;
1505 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1506 // If year, month, day, hour, minute, and second are set, a single
1507 // post is being queried.
1508 $this->is_single = true;
1509 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1510 $this->is_page = true;
1511 $this->is_single = false;
1513 // Look for archive queries. Dates, categories, authors, search, post type archives.
1515 if ( !empty($qv['s']) ) {
1516 $this->is_search = true;
1519 if ( '' !== $qv['second'] ) {
1520 $this->is_time = true;
1521 $this->is_date = true;
1524 if ( '' !== $qv['minute'] ) {
1525 $this->is_time = true;
1526 $this->is_date = true;
1529 if ( '' !== $qv['hour'] ) {
1530 $this->is_time = true;
1531 $this->is_date = true;
1535 if ( ! $this->is_date ) {
1536 $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
1537 if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
1538 $qv['error'] = '404';
1540 $this->is_day = true;
1541 $this->is_date = true;
1546 if ( $qv['monthnum'] ) {
1547 if ( ! $this->is_date ) {
1548 if ( 12 < $qv['monthnum'] ) {
1549 $qv['error'] = '404';
1551 $this->is_month = true;
1552 $this->is_date = true;
1557 if ( $qv['year'] ) {
1558 if ( ! $this->is_date ) {
1559 $this->is_year = true;
1560 $this->is_date = true;
1565 $this->is_date = true;
1566 if ( strlen($qv['m']) > 9 ) {
1567 $this->is_time = true;
1568 } else if ( strlen($qv['m']) > 7 ) {
1569 $this->is_day = true;
1570 } else if ( strlen($qv['m']) > 5 ) {
1571 $this->is_month = true;
1573 $this->is_year = true;
1577 if ( '' != $qv['w'] ) {
1578 $this->is_date = true;
1581 $this->query_vars_hash = false;
1582 $this->parse_tax_query( $qv );
1584 foreach ( $this->tax_query->queries as $tax_query ) {
1585 if ( 'NOT IN' != $tax_query['operator'] ) {
1586 switch ( $tax_query['taxonomy'] ) {
1588 $this->is_category = true;
1591 $this->is_tag = true;
1594 $this->is_tax = true;
1598 unset( $tax_query );
1600 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1601 $this->is_author = false;
1603 $this->is_author = true;
1606 if ( '' != $qv['author_name'] )
1607 $this->is_author = true;
1609 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1610 $post_type_obj = get_post_type_object( $qv['post_type'] );
1611 if ( ! empty( $post_type_obj->has_archive ) )
1612 $this->is_post_type_archive = true;
1615 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1616 $this->is_archive = true;
1619 if ( '' != $qv['feed'] )
1620 $this->is_feed = true;
1622 if ( '' != $qv['tb'] )
1623 $this->is_trackback = true;
1625 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1626 $this->is_paged = true;
1628 if ( '' != $qv['comments_popup'] )
1629 $this->is_comments_popup = true;
1631 // if we're previewing inside the write screen
1632 if ( '' != $qv['preview'] )
1633 $this->is_preview = true;
1636 $this->is_admin = true;
1638 if ( false !== strpos($qv['feed'], 'comments-') ) {
1639 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1640 $qv['withcomments'] = 1;
1643 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1645 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1646 $this->is_comment_feed = true;
1648 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 ) )
1649 $this->is_home = true;
1651 // Correct is_* for page_on_front and page_for_posts
1652 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1653 $_query = wp_parse_args($this->query);
1654 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1655 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1656 unset($_query['pagename']);
1657 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1658 $this->is_page = true;
1659 $this->is_home = false;
1660 $qv['page_id'] = get_option('page_on_front');
1661 // Correct <!--nextpage--> for page_on_front
1662 if ( !empty($qv['paged']) ) {
1663 $qv['page'] = $qv['paged'];
1664 unset($qv['paged']);
1669 if ( '' != $qv['pagename'] ) {
1670 $this->queried_object = get_page_by_path($qv['pagename']);
1671 if ( !empty($this->queried_object) )
1672 $this->queried_object_id = (int) $this->queried_object->ID;
1674 unset($this->queried_object);
1676 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1677 $this->is_page = false;
1678 $this->is_home = true;
1679 $this->is_posts_page = true;
1683 if ( $qv['page_id'] ) {
1684 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1685 $this->is_page = false;
1686 $this->is_home = true;
1687 $this->is_posts_page = true;
1691 if ( !empty($qv['post_type']) ) {
1692 if ( is_array($qv['post_type']) )
1693 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1695 $qv['post_type'] = sanitize_key($qv['post_type']);
1698 if ( ! empty( $qv['post_status'] ) ) {
1699 if ( is_array( $qv['post_status'] ) )
1700 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1702 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1705 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1706 $this->is_comment_feed = false;
1708 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1709 // Done correcting is_* for page_on_front and page_for_posts
1711 if ( '404' == $qv['error'] )
1714 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1715 $this->query_vars_changed = false;
1718 * Fires after the main query vars have been parsed.
1722 * @param WP_Query &$this The WP_Query instance (passed by reference).
1724 do_action_ref_array( 'parse_query', array( &$this ) );
1728 * Parses various taxonomy related query vars.
1733 * @param array &$q The query variables
1735 function parse_tax_query( &$q ) {
1736 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1737 $tax_query = $q['tax_query'];
1739 $tax_query = array();
1742 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1743 $tax_query[] = array(
1744 'taxonomy' => $q['taxonomy'],
1745 'terms' => array( $q['term'] ),
1750 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
1751 if ( 'post_tag' == $taxonomy )
1752 continue; // Handled further down in the $q['tag'] block
1754 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1755 $tax_query_defaults = array(
1756 'taxonomy' => $taxonomy,
1760 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1761 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1764 $term = $q[$t->query_var];
1766 if ( strpos($term, '+') !== false ) {
1767 $terms = preg_split( '/[+]+/', $term );
1768 foreach ( $terms as $term ) {
1769 $tax_query[] = array_merge( $tax_query_defaults, array(
1770 'terms' => array( $term )
1774 $tax_query[] = array_merge( $tax_query_defaults, array(
1775 'terms' => preg_split( '/[,]+/', $term )
1782 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
1783 $cat_in = $cat_not_in = array();
1785 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
1786 $cat_array = array_map( 'intval', $cat_array );
1787 $q['cat'] = implode( ',', $cat_array );
1789 foreach ( $cat_array as $cat ) {
1793 $cat_not_in[] = abs( $cat );
1796 if ( ! empty( $cat_in ) ) {
1797 $tax_query[] = array(
1798 'taxonomy' => 'category',
1800 'field' => 'term_id',
1801 'include_children' => true
1805 if ( ! empty( $cat_not_in ) ) {
1806 $tax_query[] = array(
1807 'taxonomy' => 'category',
1808 'terms' => $cat_not_in,
1809 'field' => 'term_id',
1810 'operator' => 'NOT IN',
1811 'include_children' => true
1814 unset( $cat_array, $cat_in, $cat_not_in );
1817 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1818 $q['category__and'] = (array) $q['category__and'];
1819 if ( ! isset( $q['category__in'] ) )
1820 $q['category__in'] = array();
1821 $q['category__in'][] = absint( reset( $q['category__and'] ) );
1822 unset( $q['category__and'] );
1825 if ( ! empty( $q['category__in'] ) ) {
1826 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1827 $tax_query[] = array(
1828 'taxonomy' => 'category',
1829 'terms' => $q['category__in'],
1830 'field' => 'term_id',
1831 'include_children' => false
1835 if ( ! empty($q['category__not_in']) ) {
1836 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1837 $tax_query[] = array(
1838 'taxonomy' => 'category',
1839 'terms' => $q['category__not_in'],
1840 'operator' => 'NOT IN',
1841 'include_children' => false
1845 if ( ! empty($q['category__and']) ) {
1846 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1847 $tax_query[] = array(
1848 'taxonomy' => 'category',
1849 'terms' => $q['category__and'],
1850 'field' => 'term_id',
1851 'operator' => 'AND',
1852 'include_children' => false
1857 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1858 if ( strpos($q['tag'], ',') !== false ) {
1859 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1860 foreach ( (array) $tags as $tag ) {
1861 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1862 $q['tag_slug__in'][] = $tag;
1864 } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1865 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1866 foreach ( (array) $tags as $tag ) {
1867 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1868 $q['tag_slug__and'][] = $tag;
1871 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1872 $q['tag_slug__in'][] = $q['tag'];
1876 if ( !empty($q['tag_id']) ) {
1877 $q['tag_id'] = absint( $q['tag_id'] );
1878 $tax_query[] = array(
1879 'taxonomy' => 'post_tag',
1880 'terms' => $q['tag_id']
1884 if ( !empty($q['tag__in']) ) {
1885 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1886 $tax_query[] = array(
1887 'taxonomy' => 'post_tag',
1888 'terms' => $q['tag__in']
1892 if ( !empty($q['tag__not_in']) ) {
1893 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1894 $tax_query[] = array(
1895 'taxonomy' => 'post_tag',
1896 'terms' => $q['tag__not_in'],
1897 'operator' => 'NOT IN'
1901 if ( !empty($q['tag__and']) ) {
1902 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1903 $tax_query[] = array(
1904 'taxonomy' => 'post_tag',
1905 'terms' => $q['tag__and'],
1910 if ( !empty($q['tag_slug__in']) ) {
1911 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1912 $tax_query[] = array(
1913 'taxonomy' => 'post_tag',
1914 'terms' => $q['tag_slug__in'],
1919 if ( !empty($q['tag_slug__and']) ) {
1920 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1921 $tax_query[] = array(
1922 'taxonomy' => 'post_tag',
1923 'terms' => $q['tag_slug__and'],
1929 $this->tax_query = new WP_Tax_Query( $tax_query );
1932 * Fires after taxonomy-related query vars have been parsed.
1936 * @param WP_Query $this The WP_Query instance.
1938 do_action( 'parse_tax_query', $this );
1942 * Generate SQL for the WHERE clause based on passed search terms.
1946 * @global wpdb $wpdb
1947 * @param array $q Query variables.
1948 * @return string WHERE clause.
1950 protected function parse_search( &$q ) {
1955 // added slashes screw with quote grouping when done early, so done later
1956 $q['s'] = stripslashes( $q['s'] );
1957 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1958 $q['s'] = urldecode( $q['s'] );
1959 // there are no line breaks in <input /> fields
1960 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1961 $q['search_terms_count'] = 1;
1962 if ( ! empty( $q['sentence'] ) ) {
1963 $q['search_terms'] = array( $q['s'] );
1965 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1966 $q['search_terms_count'] = count( $matches[0] );
1967 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1968 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1969 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1970 $q['search_terms'] = array( $q['s'] );
1972 $q['search_terms'] = array( $q['s'] );
1976 $n = ! empty( $q['exact'] ) ? '' : '%';
1978 $q['search_orderby_title'] = array();
1979 foreach ( $q['search_terms'] as $term ) {
1980 $term = like_escape( esc_sql( $term ) );
1982 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1984 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1985 $searchand = ' AND ';
1988 if ( ! empty( $search ) ) {
1989 $search = " AND ({$search}) ";
1990 if ( ! is_user_logged_in() )
1991 $search .= " AND ($wpdb->posts.post_password = '') ";
1998 * Check if the terms are suitable for searching.
2000 * Uses an array of stopwords (terms) that are excluded from the separate
2001 * term matching when searching for posts. The list of English stopwords is
2002 * the approximate search engines list, and is translatable.
2006 * @param array Terms to check.
2007 * @return array Terms that are not stopwords.
2009 protected function parse_search_terms( $terms ) {
2010 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
2013 $stopwords = $this->get_search_stopwords();
2015 foreach ( $terms as $term ) {
2016 // keep before/after spaces when term is for exact match
2017 if ( preg_match( '/^".+"$/', $term ) )
2018 $term = trim( $term, "\"'" );
2020 $term = trim( $term, "\"' " );
2022 // Avoid single A-Z.
2023 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
2026 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2036 * Retrieve stopwords used when parsing search terms.
2040 * @return array Stopwords.
2042 protected function get_search_stopwords() {
2043 if ( isset( $this->stopwords ) )
2044 return $this->stopwords;
2046 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2047 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2048 * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2050 $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
2051 'Comma-separated list of search stopwords in your language' ) );
2053 $stopwords = array();
2054 foreach( $words as $word ) {
2055 $word = trim( $word, "\r\n\t " );
2057 $stopwords[] = $word;
2061 * Filter stopwords used when parsing search terms.
2065 * @param array $stopwords Stopwords.
2067 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2068 return $this->stopwords;
2072 * Generate SQL for the ORDER BY condition based on passed search terms.
2074 * @global wpdb $wpdb
2075 * @param array $q Query variables.
2076 * @return string ORDER BY clause.
2078 protected function parse_search_order( &$q ) {
2081 $search_orderby = '';
2083 if ( $q['search_terms_count'] > 1 ) {
2084 $num_terms = count( $q['search_orderby_title'] );
2085 $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2087 $search_orderby = '(CASE ';
2088 // sentence match in 'post_title'
2089 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2091 // sanity limit, sort as sentence when more than 6 terms
2092 // (few searches are longer than 6 terms and most titles are not)
2093 if ( $num_terms < 7 ) {
2094 // all words in title
2095 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2096 // any word in title, not needed when $num_terms == 1
2097 if ( $num_terms > 1 )
2098 $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2101 // sentence match in 'post_content'
2102 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2103 $search_orderby .= 'ELSE 5 END)';
2105 // single word or sentence search
2106 $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2109 return $search_orderby;
2113 * Sets the 404 property and saves whether query is feed.
2118 function set_404() {
2119 $is_feed = $this->is_feed;
2121 $this->init_query_flags();
2122 $this->is_404 = true;
2124 $this->is_feed = $is_feed;
2128 * Retrieve query variable.
2133 * @param string $query_var Query variable key.
2134 * @param mixed $default Value to return if the query variable is not set. Default ''.
2137 function get( $query_var, $default = '' ) {
2138 if ( isset( $this->query_vars[ $query_var ] ) ) {
2139 return $this->query_vars[ $query_var ];
2146 * Set query variable.
2151 * @param string $query_var Query variable key.
2152 * @param mixed $value Query variable value.
2154 function set($query_var, $value) {
2155 $this->query_vars[$query_var] = $value;
2159 * Retrieve the posts based on query variables.
2161 * There are a few filters and actions that can be used to modify the post
2166 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2168 * @return array List of posts.
2170 function get_posts() {
2173 $this->parse_query();
2176 * Fires after the query variable object is created, but before the actual query is run.
2178 * Note: If using conditional tags, use the method versions within the passed instance
2179 * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
2180 * like is_main_query() test against the global $wp_query instance, not the passed one.
2184 * @param WP_Query &$this The WP_Query instance (passed by reference).
2186 do_action_ref_array( 'pre_get_posts', array( &$this ) );
2189 $q = &$this->query_vars;
2191 // Fill again in case pre_get_posts unset some vars.
2192 $q = $this->fill_query_vars($q);
2195 $this->meta_query = new WP_Meta_Query();
2196 $this->meta_query->parse_query_vars( $q );
2198 // Set a flag if a pre_get_posts hook changed the query vars.
2199 $hash = md5( serialize( $this->query_vars ) );
2200 if ( $hash != $this->query_vars_hash ) {
2201 $this->query_vars_changed = true;
2202 $this->query_vars_hash = $hash;
2206 // First let's clear some variables
2209 $whichmimetype = '';
2216 $post_status_join = false;
2219 if ( isset( $q['caller_get_posts'] ) ) {
2220 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2221 if ( !isset( $q['ignore_sticky_posts'] ) )
2222 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2225 if ( !isset( $q['ignore_sticky_posts'] ) )
2226 $q['ignore_sticky_posts'] = false;
2228 if ( !isset($q['suppress_filters']) )
2229 $q['suppress_filters'] = false;
2231 if ( !isset($q['cache_results']) ) {
2232 if ( wp_using_ext_object_cache() )
2233 $q['cache_results'] = false;
2235 $q['cache_results'] = true;
2238 if ( !isset($q['update_post_term_cache']) )
2239 $q['update_post_term_cache'] = true;
2241 if ( !isset($q['update_post_meta_cache']) )
2242 $q['update_post_meta_cache'] = true;
2244 if ( !isset($q['post_type']) ) {
2245 if ( $this->is_search )
2246 $q['post_type'] = 'any';
2248 $q['post_type'] = '';
2250 $post_type = $q['post_type'];
2251 if ( empty( $q['posts_per_page'] ) ) {
2252 $q['posts_per_page'] = get_option( 'posts_per_page' );
2254 if ( isset($q['showposts']) && $q['showposts'] ) {
2255 $q['showposts'] = (int) $q['showposts'];
2256 $q['posts_per_page'] = $q['showposts'];
2258 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2259 $q['posts_per_page'] = $q['posts_per_archive_page'];
2260 if ( !isset($q['nopaging']) ) {
2261 if ( $q['posts_per_page'] == -1 ) {
2262 $q['nopaging'] = true;
2264 $q['nopaging'] = false;
2267 if ( $this->is_feed ) {
2268 // This overrides posts_per_page.
2269 if ( ! empty( $q['posts_per_rss'] ) ) {
2270 $q['posts_per_page'] = $q['posts_per_rss'];
2272 $q['posts_per_page'] = get_option( 'posts_per_rss' );
2274 $q['nopaging'] = false;
2276 $q['posts_per_page'] = (int) $q['posts_per_page'];
2277 if ( $q['posts_per_page'] < -1 )
2278 $q['posts_per_page'] = abs($q['posts_per_page']);
2279 else if ( $q['posts_per_page'] == 0 )
2280 $q['posts_per_page'] = 1;
2282 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2283 $q['comments_per_page'] = get_option('comments_per_page');
2285 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2286 $this->is_page = true;
2287 $this->is_home = false;
2288 $q['page_id'] = get_option('page_on_front');
2291 if ( isset($q['page']) ) {
2292 $q['page'] = trim($q['page'], '/');
2293 $q['page'] = absint($q['page']);
2296 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2297 if ( isset($q['no_found_rows']) )
2298 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2300 $q['no_found_rows'] = false;
2302 switch ( $q['fields'] ) {
2304 $fields = "$wpdb->posts.ID";
2307 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2310 $fields = "$wpdb->posts.*";
2313 if ( '' !== $q['menu_order'] )
2314 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2316 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2318 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2319 if ( strlen($q['m']) > 5 )
2320 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2321 if ( strlen($q['m']) > 7 )
2322 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2323 if ( strlen($q['m']) > 9 )
2324 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2325 if ( strlen($q['m']) > 11 )
2326 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2327 if ( strlen($q['m']) > 13 )
2328 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2331 // Handle the other individual date parameters
2332 $date_parameters = array();
2334 if ( '' !== $q['hour'] )
2335 $date_parameters['hour'] = $q['hour'];
2337 if ( '' !== $q['minute'] )
2338 $date_parameters['minute'] = $q['minute'];
2340 if ( '' !== $q['second'] )
2341 $date_parameters['second'] = $q['second'];
2344 $date_parameters['year'] = $q['year'];
2346 if ( $q['monthnum'] )
2347 $date_parameters['monthnum'] = $q['monthnum'];
2350 $date_parameters['week'] = $q['w'];
2353 $date_parameters['day'] = $q['day'];
2355 if ( $date_parameters ) {
2356 $date_query = new WP_Date_Query( array( $date_parameters ) );
2357 $where .= $date_query->get_sql();
2359 unset( $date_parameters, $date_query );
2361 // Handle complex date queries
2362 if ( ! empty( $q['date_query'] ) ) {
2363 $this->date_query = new WP_Date_Query( $q['date_query'] );
2364 $where .= $this->date_query->get_sql();
2368 // If we've got a post_type AND it's not "any" post_type.
2369 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2370 foreach ( (array)$q['post_type'] as $_post_type ) {
2371 $ptype_obj = get_post_type_object($_post_type);
2372 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2375 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2376 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2377 $q['name'] = $q[ $ptype_obj->query_var ];
2379 // Hierarchical post_types will operate through the
2380 $q['pagename'] = $q[ $ptype_obj->query_var ];
2384 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2390 if ( '' != $q['name'] ) {
2391 $q['name'] = sanitize_title_for_query( $q['name'] );
2392 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2393 } elseif ( '' != $q['pagename'] ) {
2394 if ( isset($this->queried_object_id) ) {
2395 $reqpage = $this->queried_object_id;
2397 if ( 'page' != $q['post_type'] ) {
2398 foreach ( (array)$q['post_type'] as $_post_type ) {
2399 $ptype_obj = get_post_type_object($_post_type);
2400 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2403 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2409 $reqpage = get_page_by_path($q['pagename']);
2411 if ( !empty($reqpage) )
2412 $reqpage = $reqpage->ID;
2417 $page_for_posts = get_option('page_for_posts');
2418 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2419 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2420 $q['name'] = $q['pagename'];
2421 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2422 $reqpage_obj = get_post( $reqpage );
2423 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2424 $this->is_attachment = true;
2425 $post_type = $q['post_type'] = 'attachment';
2426 $this->is_page = true;
2427 $q['attachment_id'] = $reqpage;
2430 } elseif ( '' != $q['attachment'] ) {
2431 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2432 $q['name'] = $q['attachment'];
2433 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2437 if ( intval($q['comments_popup']) )
2438 $q['p'] = absint($q['comments_popup']);
2440 // If an attachment is requested by number, let it supersede any post number.
2441 if ( $q['attachment_id'] )
2442 $q['p'] = absint($q['attachment_id']);
2444 // If a post number is specified, load that post
2446 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2447 } elseif ( $q['post__in'] ) {
2448 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2449 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2450 } elseif ( $q['post__not_in'] ) {
2451 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2452 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2455 if ( is_numeric( $q['post_parent'] ) ) {
2456 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2457 } elseif ( $q['post_parent__in'] ) {
2458 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2459 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2460 } elseif ( $q['post_parent__not_in'] ) {
2461 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2462 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2465 if ( $q['page_id'] ) {
2466 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2467 $q['p'] = $q['page_id'];
2468 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2472 // If a search pattern is specified, load the posts that match.
2473 if ( ! empty( $q['s'] ) )
2474 $search = $this->parse_search( $q );
2477 * Filter the search SQL that is used in the WHERE clause of WP_Query.
2481 * @param string $search Search SQL for WHERE clause.
2482 * @param WP_Query $this The current WP_Query object.
2484 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2487 if ( !$this->is_singular ) {
2488 $this->parse_tax_query( $q );
2490 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2492 $join .= $clauses['join'];
2493 $where .= $clauses['where'];
2496 if ( $this->is_tax ) {
2497 if ( empty($post_type) ) {
2498 // Do a fully inclusive search for currently registered post types of queried taxonomies
2499 $post_type = array();
2500 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2501 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2502 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2503 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2508 elseif ( count( $post_type ) == 1 )
2509 $post_type = $post_type[0];
2511 $post_status_join = true;
2512 } elseif ( in_array('attachment', (array) $post_type) ) {
2513 $post_status_join = true;
2518 if ( !empty($this->tax_query->queries) ) {
2519 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2520 if ( !empty( $tax_query_in_and ) ) {
2521 if ( !isset( $q['taxonomy'] ) ) {
2522 foreach ( $tax_query_in_and as $a_tax_query ) {
2523 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2524 $q['taxonomy'] = $a_tax_query['taxonomy'];
2525 if ( 'slug' == $a_tax_query['field'] )
2526 $q['term'] = $a_tax_query['terms'][0];
2528 $q['term_id'] = $a_tax_query['terms'][0];
2535 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2536 if ( ! empty( $cat_query ) ) {
2537 $cat_query = reset( $cat_query );
2539 if ( ! empty( $cat_query['terms'][0] ) ) {
2540 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2542 $this->set( 'cat', $the_cat->term_id );
2543 $this->set( 'category_name', $the_cat->slug );
2548 unset( $cat_query );
2550 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2551 if ( ! empty( $tag_query ) ) {
2552 $tag_query = reset( $tag_query );
2554 if ( ! empty( $tag_query['terms'][0] ) ) {
2555 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2557 $this->set( 'tag_id', $the_tag->term_id );
2561 unset( $tag_query );
2565 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2566 $groupby = "{$wpdb->posts}.ID";
2569 // Author/user stuff
2571 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2572 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2573 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2574 foreach ( $authors as $author ) {
2575 $key = $author > 0 ? 'author__in' : 'author__not_in';
2576 $q[$key][] = abs( $author );
2578 $q['author'] = implode( ',', $authors );
2581 if ( ! empty( $q['author__not_in'] ) ) {
2582 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2583 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2584 } elseif ( ! empty( $q['author__in'] ) ) {
2585 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2586 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2589 // Author stuff for nice URLs
2591 if ( '' != $q['author_name'] ) {
2592 if ( strpos($q['author_name'], '/') !== false ) {
2593 $q['author_name'] = explode('/', $q['author_name']);
2594 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2595 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2597 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2600 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2601 $q['author'] = get_user_by('slug', $q['author_name']);
2603 $q['author'] = $q['author']->ID;
2604 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2607 // MIME-Type stuff for attachment browsing
2609 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2610 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2612 $where .= $search . $whichauthor . $whichmimetype;
2614 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2615 $q['order'] = 'DESC';
2618 if ( empty($q['orderby']) ) {
2619 $orderby = "$wpdb->posts.post_date " . $q['order'];
2620 } elseif ( 'none' == $q['orderby'] ) {
2622 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2623 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2624 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2625 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2627 // Used to filter values
2628 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2629 if ( !empty($q['meta_key']) ) {
2630 $allowed_keys[] = $q['meta_key'];
2631 $allowed_keys[] = 'meta_value';
2632 $allowed_keys[] = 'meta_value_num';
2634 $q['orderby'] = urldecode($q['orderby']);
2635 $q['orderby'] = addslashes_gpc($q['orderby']);
2637 $orderby_array = array();
2638 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2639 // Only allow certain values for safety
2640 if ( ! in_array($orderby, $allowed_keys) )
2643 switch ( $orderby ) {
2645 $orderby = "$wpdb->posts.menu_order";
2648 $orderby = "$wpdb->posts.ID";
2651 $orderby = 'RAND()';
2653 case $q['meta_key']:
2655 if ( isset( $q['meta_type'] ) ) {
2656 $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2657 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2659 $orderby = "$wpdb->postmeta.meta_value";
2662 case 'meta_value_num':
2663 $orderby = "$wpdb->postmeta.meta_value+0";
2665 case 'comment_count':
2666 $orderby = "$wpdb->posts.comment_count";
2669 $orderby = "$wpdb->posts.post_" . $orderby;
2672 $orderby_array[] = $orderby;
2674 $orderby = implode( ',', $orderby_array );
2676 if ( empty( $orderby ) )
2677 $orderby = "$wpdb->posts.post_date ".$q['order'];
2679 $orderby .= " {$q['order']}";
2682 // Order search results by relevance only when another "orderby" is not specified in the query.
2683 if ( ! empty( $q['s'] ) ) {
2684 $search_orderby = '';
2685 if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2686 $search_orderby = $this->parse_search_order( $q );
2689 * Filter the ORDER BY used when ordering search results.
2693 * @param string $search_orderby The ORDER BY clause.
2694 * @param WP_Query $this The current WP_Query instance.
2696 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2697 if ( $search_orderby )
2698 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2701 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2702 $post_type_cap = 'multiple_post_type';
2704 if ( is_array( $post_type ) )
2705 $post_type = reset( $post_type );
2706 $post_type_object = get_post_type_object( $post_type );
2707 if ( empty( $post_type_object ) )
2708 $post_type_cap = $post_type;
2711 if ( isset( $q['post_password'] ) ) {
2712 $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
2713 if ( empty( $q['perm'] ) ) {
2714 $q['perm'] = 'readable';
2716 } elseif ( isset( $q['has_password'] ) ) {
2717 $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
2720 if ( 'any' == $post_type ) {
2721 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2722 if ( empty( $in_search_post_types ) )
2723 $where .= ' AND 1=0 ';
2725 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2726 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2727 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2728 } elseif ( ! empty( $post_type ) ) {
2729 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2730 $post_type_object = get_post_type_object ( $post_type );
2731 } elseif ( $this->is_attachment ) {
2732 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2733 $post_type_object = get_post_type_object ( 'attachment' );
2734 } elseif ( $this->is_page ) {
2735 $where .= " AND $wpdb->posts.post_type = 'page'";
2736 $post_type_object = get_post_type_object ( 'page' );
2738 $where .= " AND $wpdb->posts.post_type = 'post'";
2739 $post_type_object = get_post_type_object ( 'post' );
2742 $edit_cap = 'edit_post';
2743 $read_cap = 'read_post';
2745 if ( ! empty( $post_type_object ) ) {
2746 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2747 $read_private_cap = $post_type_object->cap->read_private_posts;
2749 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2750 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2753 $user_id = get_current_user_id();
2755 if ( ! empty( $q['post_status'] ) ) {
2756 $statuswheres = array();
2757 $q_status = $q['post_status'];
2758 if ( ! is_array( $q_status ) )
2759 $q_status = explode(',', $q_status);
2760 $r_status = array();
2761 $p_status = array();
2762 $e_status = array();
2763 if ( in_array('any', $q_status) ) {
2764 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2765 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2767 foreach ( get_post_stati() as $status ) {
2768 if ( in_array( $status, $q_status ) ) {
2769 if ( 'private' == $status )
2770 $p_status[] = "$wpdb->posts.post_status = '$status'";
2772 $r_status[] = "$wpdb->posts.post_status = '$status'";
2777 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2778 $r_status = array_merge($r_status, $p_status);
2782 if ( !empty($e_status) ) {
2783 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2785 if ( !empty($r_status) ) {
2786 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2787 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2789 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2791 if ( !empty($p_status) ) {
2792 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2793 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2795 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2797 if ( $post_status_join ) {
2798 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2799 foreach ( $statuswheres as $index => $statuswhere )
2800 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2802 $where_status = implode( ' OR ', $statuswheres );
2803 if ( ! empty( $where_status ) ) {
2804 $where .= " AND ($where_status)";
2806 } elseif ( !$this->is_singular ) {
2807 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2809 // Add public states.
2810 $public_states = get_post_stati( array('public' => true) );
2811 foreach ( (array) $public_states as $state ) {
2812 if ( 'publish' == $state ) // Publish is hard-coded above.
2814 $where .= " OR $wpdb->posts.post_status = '$state'";
2817 if ( $this->is_admin ) {
2818 // Add protected states that should show in the admin all list.
2819 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2820 foreach ( (array) $admin_all_states as $state )
2821 $where .= " OR $wpdb->posts.post_status = '$state'";
2824 if ( is_user_logged_in() ) {
2825 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2826 $private_states = get_post_stati( array('private' => true) );
2827 foreach ( (array) $private_states as $state )
2828 $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'";
2834 if ( !empty( $this->meta_query->queries ) ) {
2835 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2836 $join .= $clauses['join'];
2837 $where .= $clauses['where'];
2841 * Apply filters on where and join prior to paging so that any
2842 * manipulations to them are reflected in the paging by day queries.
2844 if ( !$q['suppress_filters'] ) {
2846 * Filter the WHERE clause of the query.
2850 * @param string $where The WHERE clause of the query.
2851 * @param WP_Query &$this The WP_Query instance (passed by reference).
2853 $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) );
2856 * Filter the JOIN clause of the query.
2860 * @param string $where The JOIN clause of the query.
2861 * @param WP_Query &$this The WP_Query instance (passed by reference).
2863 $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) );
2867 if ( empty($q['nopaging']) && !$this->is_singular ) {
2868 $page = absint($q['paged']);
2872 if ( empty($q['offset']) ) {
2873 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2874 } else { // we're ignoring $page and using 'offset'
2875 $q['offset'] = absint($q['offset']);
2876 $pgstrt = $q['offset'] . ', ';
2878 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2882 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2883 if ( $this->is_archive || $this->is_search ) {
2884 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2885 $cwhere = "WHERE comment_approved = '1' $where";
2886 $cgroupby = "$wpdb->comments.comment_id";
2887 } else { // Other non singular e.g. front
2888 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2889 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2893 if ( !$q['suppress_filters'] ) {
2895 * Filter the JOIN clause of the comments feed query before sending.
2899 * @param string $cjoin The JOIN clause of the query.
2900 * @param WP_Query &$this The WP_Query instance (passed by reference).
2902 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
2905 * Filter the WHERE clause of the comments feed query before sending.
2909 * @param string $cwhere The WHERE clause of the query.
2910 * @param WP_Query &$this The WP_Query instance (passed by reference).
2912 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
2915 * Filter the GROUP BY clause of the comments feed query before sending.
2919 * @param string $cgroupby The GROUP BY clause of the query.
2920 * @param WP_Query &$this The WP_Query instance (passed by reference).
2922 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
2925 * Filter the ORDER BY clause of the comments feed query before sending.
2929 * @param string $corderby The ORDER BY clause of the query.
2930 * @param WP_Query &$this The WP_Query instance (passed by reference).
2932 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2935 * Filter the LIMIT clause of the comments feed query before sending.
2939 * @param string $climits The JOIN clause of the query.
2940 * @param WP_Query &$this The WP_Query instance (passed by reference).
2942 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2944 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2945 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2947 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2948 $this->comment_count = count($this->comments);
2950 $post_ids = array();
2952 foreach ( $this->comments as $comment )
2953 $post_ids[] = (int) $comment->comment_post_ID;
2955 $post_ids = join(',', $post_ids);
2958 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2963 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2966 * Apply post-paging filters on where and join. Only plugins that
2967 * manipulate paging queries should use these hooks.
2969 if ( !$q['suppress_filters'] ) {
2971 * Filter the WHERE clause of the query.
2973 * Specifically for manipulating paging queries.
2977 * @param string $where The WHERE clause of the query.
2978 * @param WP_Query &$this The WP_Query instance (passed by reference).
2980 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2983 * Filter the GROUP BY clause of the query.
2987 * @param string $groupby The GROUP BY clause of the query.
2988 * @param WP_Query &$this The WP_Query instance (passed by reference).
2990 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2993 * Filter the JOIN clause of the query.
2995 * Specifically for manipulating paging queries.
2999 * @param string $join The JOIN clause of the query.
3000 * @param WP_Query &$this The WP_Query instance (passed by reference).
3002 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
3005 * Filter the ORDER BY clause of the query.
3009 * @param string $orderby The ORDER BY clause of the query.
3010 * @param WP_Query &$this The WP_Query instance (passed by reference).
3012 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
3015 * Filter the DISTINCT clause of the query.
3019 * @param string $distinct The DISTINCT clause of the query.
3020 * @param WP_Query &$this The WP_Query instance (passed by reference).
3022 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
3025 * Filter the LIMIT clause of the query.
3029 * @param string $limits The LIMIT clause of the query.
3030 * @param WP_Query &$this The WP_Query instance (passed by reference).
3032 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
3035 * Filter the SELECT clause of the query.
3039 * @param string $fields The SELECT clause of the query.
3040 * @param WP_Query &$this The WP_Query instance (passed by reference).
3042 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
3045 * Filter all query clauses at once, for convenience.
3047 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
3048 * fields (SELECT), and LIMITS clauses.
3052 * @param array $clauses The list of clauses for the query.
3053 * @param WP_Query &$this The WP_Query instance (passed by reference).
3055 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
3057 foreach ( $pieces as $piece ) {
3058 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
3063 * Fires to announce the query's current selection parameters.
3065 * For use by caching plugins.
3069 * @param string $selection The assembled selection query.
3071 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
3074 * Filter again for the benefit of caching plugins.
3075 * Regular plugins should use the hooks above.
3077 if ( !$q['suppress_filters'] ) {
3079 * Filter the WHERE clause of the query.
3081 * For use by caching plugins.
3085 * @param string $where The WHERE clause of the query.
3086 * @param WP_Query &$this The WP_Query instance (passed by reference).
3088 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
3091 * Filter the GROUP BY clause of the query.
3093 * For use by caching plugins.
3097 * @param string $groupby The GROUP BY clause of the query.
3098 * @param WP_Query &$this The WP_Query instance (passed by reference).
3100 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
3103 * Filter the JOIN clause of the query.
3105 * For use by caching plugins.
3109 * @param string $join The JOIN clause of the query.
3110 * @param WP_Query &$this The WP_Query instance (passed by reference).
3112 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
3115 * Filter the ORDER BY clause of the query.
3117 * For use by caching plugins.
3121 * @param string $orderby The ORDER BY clause of the query.
3122 * @param WP_Query &$this The WP_Query instance (passed by reference).
3124 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
3127 * Filter the DISTINCT clause of the query.
3129 * For use by caching plugins.
3133 * @param string $distinct The DISTINCT clause of the query.
3134 * @param WP_Query &$this The WP_Query instance (passed by reference).
3136 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
3139 * Filter the SELECT clause of the query.
3141 * For use by caching plugins.
3145 * @param string $fields The SELECT clause of the query.
3146 * @param WP_Query &$this The WP_Query instance (passed by reference).
3148 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
3151 * Filter the LIMIT clause of the query.
3153 * For use by caching plugins.
3157 * @param string $limits The LIMIT clause of the query.
3158 * @param WP_Query &$this The WP_Query instance (passed by reference).
3160 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
3163 * Filter all query clauses at once, for convenience.
3165 * For use by caching plugins.
3167 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
3168 * fields (SELECT), and LIMITS clauses.
3172 * @param array $pieces The pieces of the query.
3173 * @param WP_Query &$this The WP_Query instance (passed by reference).
3175 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
3176 foreach ( $pieces as $piece )
3177 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
3180 if ( ! empty($groupby) )
3181 $groupby = 'GROUP BY ' . $groupby;
3182 if ( !empty( $orderby ) )
3183 $orderby = 'ORDER BY ' . $orderby;
3186 if ( !$q['no_found_rows'] && !empty($limits) )
3187 $found_rows = 'SQL_CALC_FOUND_ROWS';
3189 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
3191 if ( !$q['suppress_filters'] ) {
3193 * Filter the completed SQL query before sending.
3197 * @param array $request The complete SQL query.
3198 * @param WP_Query &$this The WP_Query instance (passed by reference).
3200 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
3203 if ( 'ids' == $q['fields'] ) {
3204 $this->posts = $wpdb->get_col( $this->request );
3205 $this->post_count = count( $this->posts );
3206 $this->set_found_posts( $q, $limits );
3208 return array_map( 'intval', $this->posts );
3211 if ( 'id=>parent' == $q['fields'] ) {
3212 $this->posts = $wpdb->get_results( $this->request );
3213 $this->post_count = count( $this->posts );
3214 $this->set_found_posts( $q, $limits );
3217 foreach ( $this->posts as $post ) {
3218 $r[ (int) $post->ID ] = (int) $post->post_parent;
3223 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
3226 * Filter whether to split the query.
3228 * Splitting the query will cause it to fetch just the IDs of the found posts
3229 * (and then individually fetch each post by ID), rather than fetching every
3230 * complete row at once. One massive result vs. many small results.
3234 * @param bool $split_the_query Whether or not to split the query.
3235 * @param WP_Query $this The WP_Query instance.
3237 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
3239 if ( $split_the_query ) {
3240 // First get the IDs and then fill in the objects
3242 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
3245 * Filter the Post IDs SQL request before sending.
3249 * @param string $request The post ID request.
3250 * @param WP_Query $this The WP_Query instance.
3252 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
3254 $ids = $wpdb->get_col( $this->request );
3257 $this->posts = $ids;
3258 $this->set_found_posts( $q, $limits );
3259 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
3261 $this->posts = array();
3264 $this->posts = $wpdb->get_results( $this->request );
3265 $this->set_found_posts( $q, $limits );
3268 // Convert to WP_Post objects
3270 $this->posts = array_map( 'get_post', $this->posts );
3272 if ( ! $q['suppress_filters'] ) {
3274 * Filter the raw post results array, prior to status checks.
3278 * @param array $posts The post results array.
3279 * @param WP_Query &$this The WP_Query instance (passed by reference).
3281 $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
3284 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
3285 /** This filter is documented in wp-includes/query.php */
3286 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
3288 /** This filter is documented in wp-includes/query.php */
3289 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
3291 /** This filter is documented in wp-includes/query.php */
3292 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
3293 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
3295 /** This filter is documented in wp-includes/query.php */
3296 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
3297 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
3299 /** This filter is documented in wp-includes/query.php */
3300 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
3302 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
3303 $this->comments = $wpdb->get_results($comments_request);
3304 $this->comment_count = count($this->comments);
3307 // Check post status to determine if post should be displayed.
3308 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
3309 $status = get_post_status($this->posts[0]);
3310 $post_status_obj = get_post_status_object($status);
3311 //$type = get_post_type($this->posts[0]);
3312 if ( !$post_status_obj->public ) {
3313 if ( ! is_user_logged_in() ) {
3314 // User must be logged in to view unpublished posts.
3315 $this->posts = array();
3317 if ( $post_status_obj->protected ) {
3318 // User must have edit permissions on the draft to preview.
3319 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
3320 $this->posts = array();
3322 $this->is_preview = true;
3323 if ( 'future' != $status )
3324 $this->posts[0]->post_date = current_time('mysql');
3326 } elseif ( $post_status_obj->private ) {
3327 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
3328 $this->posts = array();
3330 $this->posts = array();
3335 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
3337 * Filter the single post for preview mode.
3341 * @param WP_Post $post_preview The Post object.
3342 * @param WP_Query &$this The WP_Query instance (passed by reference).
3344 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
3348 // Put sticky posts at the top of the posts array
3349 $sticky_posts = get_option('sticky_posts');
3350 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
3351 $num_posts = count($this->posts);
3353 // Loop over posts and relocate stickies to the front.
3354 for ( $i = 0; $i < $num_posts; $i++ ) {
3355 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
3356 $sticky_post = $this->posts[$i];
3357 // Remove sticky from current position
3358 array_splice($this->posts, $i, 1);
3359 // Move to front, after other stickies
3360 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
3361 // Increment the sticky offset. The next sticky will be placed at this offset.
3363 // Remove post from sticky posts array
3364 $offset = array_search($sticky_post->ID, $sticky_posts);
3365 unset( $sticky_posts[$offset] );
3369 // If any posts have been excluded specifically, Ignore those that are sticky.
3370 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3371 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3373 // Fetch sticky posts that weren't in the query results
3374 if ( !empty($sticky_posts) ) {
3375 $stickies = get_posts( array(
3376 'post__in' => $sticky_posts,
3377 'post_type' => $post_type,
3378 'post_status' => 'publish',
3382 foreach ( $stickies as $sticky_post ) {
3383 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3389 if ( ! $q['suppress_filters'] ) {
3391 * Filter the array of retrieved posts after they've been fetched and
3392 * internally processed.
3396 * @param array $posts The array of retrieved posts.
3397 * @param WP_Query &$this The WP_Query instance (passed by reference).
3399 $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) );
3402 // Ensure that any posts added/modified via one of the filters above are
3403 // of the type WP_Post and are filtered.
3404 if ( $this->posts ) {
3405 $this->post_count = count( $this->posts );
3407 $this->posts = array_map( 'get_post', $this->posts );
3409 if ( $q['cache_results'] )
3410 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3412 $this->post = reset( $this->posts );
3414 $this->post_count = 0;
3415 $this->posts = array();
3418 return $this->posts;
3422 * Set up the amount of found posts and the number of pages (if limit clause was used)
3423 * for the current query.
3428 function set_found_posts( $q, $limits ) {
3431 // Bail if posts is an empty array. Continue if posts is an empty string,
3432 // null, or false to accommodate caching plugins that fill posts later.
3433 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3436 if ( ! empty( $limits ) ) {
3438 * Filter the query to run for retrieving the found posts.
3442 * @param string $found_posts The query to run to find the found posts.
3443 * @param WP_Query &$this The WP_Query instance (passed by reference).
3445 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3447 $this->found_posts = count( $this->posts );
3451 * Filter the number of found posts for the query.
3455 * @param int $found_posts The number of posts found.
3456 * @param WP_Query &$this The WP_Query instance (passed by reference).
3458 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3460 if ( ! empty( $limits ) )
3461 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3465 * Set up the next post and iterate current post index.
3470 * @return WP_Post Next post.
3472 function next_post() {
3474 $this->current_post++;
3476 $this->post = $this->posts[$this->current_post];
3481 * Sets up the current post.
3483 * Retrieves the next post, sets up the post, sets the 'in the loop'
3489 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3491 function the_post() {
3493 $this->in_the_loop = true;
3495 if ( $this->current_post == -1 ) // loop has just started
3497 * Fires once the loop is started.
3501 * @param WP_Query &$this The WP_Query instance (passed by reference).
3503 do_action_ref_array( 'loop_start', array( &$this ) );
3505 $post = $this->next_post();
3506 setup_postdata($post);
3510 * Whether there are more posts available in the loop.
3512 * Calls action 'loop_end', when the loop is complete.
3516 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3518 * @return bool True if posts are available, false if end of loop.
3520 function have_posts() {
3521 if ( $this->current_post + 1 < $this->post_count ) {
3523 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3525 * Fires once the loop has ended.
3529 * @param WP_Query &$this The WP_Query instance (passed by reference).
3531 do_action_ref_array( 'loop_end', array( &$this ) );
3532 // Do some cleaning up after the loop
3533 $this->rewind_posts();
3536 $this->in_the_loop = false;
3541 * Rewind the posts and reset post index.
3546 function rewind_posts() {
3547 $this->current_post = -1;
3548 if ( $this->post_count > 0 ) {
3549 $this->post = $this->posts[0];
3554 * Iterate current comment index and return comment object.
3559 * @return object Comment object.
3561 function next_comment() {
3562 $this->current_comment++;
3564 $this->comment = $this->comments[$this->current_comment];
3565 return $this->comment;
3569 * Sets up the current comment.
3573 * @global object $comment Current comment.
3574 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3576 function the_comment() {
3579 $comment = $this->next_comment();
3581 if ( $this->current_comment == 0 ) {
3583 * Fires once the comment loop is started.
3587 do_action( 'comment_loop_start' );
3592 * Whether there are more comments available.
3594 * Automatically rewinds comments when finished.
3599 * @return bool True, if more comments. False, if no more posts.
3601 function have_comments() {
3602 if ( $this->current_comment + 1 < $this->comment_count ) {
3604 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3605 $this->rewind_comments();
3612 * Rewind the comments, resets the comment index and comment to first.
3617 function rewind_comments() {
3618 $this->current_comment = -1;
3619 if ( $this->comment_count > 0 ) {
3620 $this->comment = $this->comments[0];
3625 * Sets up the WordPress query by parsing query string.
3630 * @param string $query URL query string.
3631 * @return array List of posts.
3633 function query( $query ) {
3635 $this->query = $this->query_vars = wp_parse_args( $query );
3636 return $this->get_posts();
3640 * Retrieve queried object.
3642 * If queried object is not set, then the queried object will be set from
3643 * the category, tag, taxonomy, posts page, single post, page, or author
3644 * query variable. After it is set up, it will be returned.
3651 function get_queried_object() {
3652 if ( isset($this->queried_object) )
3653 return $this->queried_object;
3655 $this->queried_object = null;
3656 $this->queried_object_id = 0;
3658 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3659 if ( $this->is_category ) {
3660 if ( $this->get( 'cat' ) ) {
3661 $term = get_term( $this->get( 'cat' ), 'category' );
3662 } elseif ( $this->get( 'category_name' ) ) {
3663 $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
3665 } elseif ( $this->is_tag ) {
3666 if ( $this->get( 'tag_id' ) ) {
3667 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
3668 } elseif ( $this->get( 'tag' ) ) {
3669 $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
3672 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3673 $query = reset( $tax_query_in_and );
3675 if ( $query['terms'] ) {
3676 if ( 'term_id' == $query['field'] ) {
3677 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3679 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3684 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3685 $this->queried_object = $term;
3686 $this->queried_object_id = (int) $term->term_id;
3688 if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
3689 _make_cat_compat( $this->queried_object );
3691 } elseif ( $this->is_post_type_archive ) {
3692 $post_type = $this->get( 'post_type' );
3693 if ( is_array( $post_type ) )
3694 $post_type = reset( $post_type );
3695 $this->queried_object = get_post_type_object( $post_type );
3696 } elseif ( $this->is_posts_page ) {
3697 $page_for_posts = get_option('page_for_posts');
3698 $this->queried_object = get_post( $page_for_posts );
3699 $this->queried_object_id = (int) $this->queried_object->ID;
3700 } elseif ( $this->is_singular && !is_null($this->post) ) {
3701 $this->queried_object = $this->post;
3702 $this->queried_object_id = (int) $this->post->ID;
3703 } elseif ( $this->is_author ) {
3704 $this->queried_object_id = (int) $this->get('author');
3705 $this->queried_object = get_userdata( $this->queried_object_id );
3708 return $this->queried_object;
3712 * Retrieve ID of the current queried object.
3719 function get_queried_object_id() {
3720 $this->get_queried_object();
3722 if ( isset($this->queried_object_id) ) {
3723 return $this->queried_object_id;
3732 * Sets up the WordPress query, if parameter is not empty.
3737 * @param string $query URL query string.
3740 function __construct($query = '') {
3741 if ( ! empty($query) ) {
3742 $this->query($query);
3747 * Is the query for an existing archive page?
3749 * Month, Year, Category, Author, Post Type archive...
3755 function is_archive() {
3756 return (bool) $this->is_archive;
3760 * Is the query for an existing post type archive page?
3764 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3767 function is_post_type_archive( $post_types = '' ) {
3768 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3769 return (bool) $this->is_post_type_archive;
3771 $post_type = $this->get( 'post_type' );
3772 if ( is_array( $post_type ) )
3773 $post_type = reset( $post_type );
3774 $post_type_object = get_post_type_object( $post_type );
3776 return in_array( $post_type_object->name, (array) $post_types );
3780 * Is the query for an existing attachment page?
3784 * @param mixed $attachment Attachment ID, title, slug, or array of such.
3787 function is_attachment( $attachment = '' ) {
3788 if ( ! $this->is_attachment ) {
3792 if ( empty( $attachment ) ) {
3796 $attachment = (array) $attachment;
3798 $post_obj = $this->get_queried_object();
3800 if ( in_array( $post_obj->ID, $attachment ) ) {
3802 } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
3804 } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
3811 * Is the query for an existing author archive page?
3813 * If the $author parameter is specified, this function will additionally
3814 * check if the query is for one of the authors specified.
3818 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3821 function is_author( $author = '' ) {
3822 if ( !$this->is_author )
3825 if ( empty($author) )
3828 $author_obj = $this->get_queried_object();
3830 $author = (array) $author;
3832 if ( in_array( $author_obj->ID, $author ) )
3834 elseif ( in_array( $author_obj->nickname, $author ) )
3836 elseif ( in_array( $author_obj->user_nicename, $author ) )
3843 * Is the query for an existing category archive page?
3845 * If the $category parameter is specified, this function will additionally
3846 * check if the query is for one of the categories specified.
3850 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3853 function is_category( $category = '' ) {
3854 if ( !$this->is_category )
3857 if ( empty($category) )
3860 $cat_obj = $this->get_queried_object();
3862 $category = (array) $category;
3864 if ( in_array( $cat_obj->term_id, $category ) )
3866 elseif ( in_array( $cat_obj->name, $category ) )
3868 elseif ( in_array( $cat_obj->slug, $category ) )
3875 * Is the query for an existing tag archive page?
3877 * If the $tag parameter is specified, this function will additionally
3878 * check if the query is for one of the tags specified.
3882 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3885 function is_tag( $tag = '' ) {
3886 if ( ! $this->is_tag )
3889 if ( empty( $tag ) )
3892 $tag_obj = $this->get_queried_object();
3894 $tag = (array) $tag;
3896 if ( in_array( $tag_obj->term_id, $tag ) )
3898 elseif ( in_array( $tag_obj->name, $tag ) )
3900 elseif ( in_array( $tag_obj->slug, $tag ) )
3907 * Is the query for an existing taxonomy archive page?
3909 * If the $taxonomy parameter is specified, this function will additionally
3910 * check if the query is for that specific $taxonomy.
3912 * If the $term parameter is specified in addition to the $taxonomy parameter,
3913 * this function will additionally check if the query is for one of the terms
3918 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3919 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3922 function is_tax( $taxonomy = '', $term = '' ) {
3923 global $wp_taxonomies;
3925 if ( !$this->is_tax )
3928 if ( empty( $taxonomy ) )
3931 $queried_object = $this->get_queried_object();
3932 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3933 $term_array = (array) $term;
3935 // Check that the taxonomy matches.
3936 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3939 // Only a Taxonomy provided.
3940 if ( empty( $term ) )
3943 return isset( $queried_object->term_id ) &&
3944 count( array_intersect(
3945 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3951 * Whether the current URL is within the comments popup window.
3957 function is_comments_popup() {
3958 return (bool) $this->is_comments_popup;
3962 * Is the query for an existing date archive?
3968 function is_date() {
3969 return (bool) $this->is_date;
3973 * Is the query for an existing day archive?
3980 return (bool) $this->is_day;
3984 * Is the query for a feed?
3988 * @param string|array $feeds Optional feed types to check.
3991 function is_feed( $feeds = '' ) {
3992 if ( empty( $feeds ) || ! $this->is_feed )
3993 return (bool) $this->is_feed;
3994 $qv = $this->get( 'feed' );
3995 if ( 'feed' == $qv )
3996 $qv = get_default_feed();
3997 return in_array( $qv, (array) $feeds );
4001 * Is the query for a comments feed?
4007 function is_comment_feed() {
4008 return (bool) $this->is_comment_feed;
4012 * Is the query for the front page of the site?
4014 * This is for what is displayed at your site's main URL.
4016 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
4018 * If you set a static page for the front page of your site, this function will return
4019 * true when viewing that page.
4021 * Otherwise the same as @see WP_Query::is_home()
4025 * @uses get_option()
4027 * @return bool True, if front of site.
4029 function is_front_page() {
4031 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
4033 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
4040 * Is the query for the blog homepage?
4042 * This is the page which shows the time based blog content of your site.
4044 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
4046 * If you set a static page for the front page of your site, this function will return
4047 * true only on the page you set as the "Posts page".
4049 * @see WP_Query::is_front_page()
4053 * @return bool True if blog view homepage.
4055 function is_home() {
4056 return (bool) $this->is_home;
4060 * Is the query for an existing month archive?
4066 function is_month() {
4067 return (bool) $this->is_month;
4071 * Is the query for an existing single page?
4073 * If the $page parameter is specified, this function will additionally
4074 * check if the query is for one of the pages specified.
4076 * @see WP_Query::is_single()
4077 * @see WP_Query::is_singular()
4081 * @param mixed $page Page ID, title, slug, or array of such.
4084 function is_page( $page = '' ) {
4085 if ( !$this->is_page )
4088 if ( empty( $page ) )
4091 $page_obj = $this->get_queried_object();
4093 $page = (array) $page;
4095 if ( in_array( $page_obj->ID, $page ) )
4097 elseif ( in_array( $page_obj->post_title, $page ) )
4099 else if ( in_array( $page_obj->post_name, $page ) )
4106 * Is the query for paged result and not for the first page?
4112 function is_paged() {
4113 return (bool) $this->is_paged;
4117 * Is the query for a post or page preview?
4123 function is_preview() {
4124 return (bool) $this->is_preview;
4128 * Is the query for the robots file?
4134 function is_robots() {
4135 return (bool) $this->is_robots;
4139 * Is the query for a search?
4145 function is_search() {
4146 return (bool) $this->is_search;
4150 * Is the query for an existing single post?
4152 * Works for any post type, except attachments and pages
4154 * If the $post parameter is specified, this function will additionally
4155 * check if the query is for one of the Posts specified.
4157 * @see WP_Query::is_page()
4158 * @see WP_Query::is_singular()
4162 * @param mixed $post Post ID, title, slug, or array of such.
4165 function is_single( $post = '' ) {
4166 if ( !$this->is_single )
4172 $post_obj = $this->get_queried_object();
4174 $post = (array) $post;
4176 if ( in_array( $post_obj->ID, $post ) )
4178 elseif ( in_array( $post_obj->post_title, $post ) )
4180 elseif ( in_array( $post_obj->post_name, $post ) )
4187 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
4189 * If the $post_types parameter is specified, this function will additionally
4190 * check if the query is for one of the Posts Types specified.
4192 * @see WP_Query::is_page()
4193 * @see WP_Query::is_single()
4197 * @param mixed $post_types Optional. Post Type or array of Post Types
4200 function is_singular( $post_types = '' ) {
4201 if ( empty( $post_types ) || !$this->is_singular )
4202 return (bool) $this->is_singular;
4204 $post_obj = $this->get_queried_object();
4206 return in_array( $post_obj->post_type, (array) $post_types );
4210 * Is the query for a specific time?
4216 function is_time() {
4217 return (bool) $this->is_time;
4221 * Is the query for a trackback endpoint call?
4227 function is_trackback() {
4228 return (bool) $this->is_trackback;
4232 * Is the query for an existing year archive?
4238 function is_year() {
4239 return (bool) $this->is_year;
4243 * Is the query a 404 (returns no results)?
4250 return (bool) $this->is_404;
4254 * Is the query the main query?
4260 function is_main_query() {
4261 global $wp_the_query;
4262 return $wp_the_query === $this;
4266 * After looping through a nested query, this function
4267 * restores the $post global to the current post in this query.
4273 function reset_postdata() {
4274 if ( ! empty( $this->post ) ) {
4275 $GLOBALS['post'] = $this->post;
4276 setup_postdata( $this->post );
4282 * Redirect old slugs to the correct permalink.
4284 * Attempts to find the current slug from the past slugs.
4290 * @return null If no link is found, null is returned.
4292 function wp_old_slug_redirect() {
4294 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
4297 // Guess the current post_type based on the query vars.
4298 if ( get_query_var('post_type') )
4299 $post_type = get_query_var('post_type');
4300 elseif ( !empty($wp_query->query_vars['pagename']) )
4301 $post_type = 'page';
4303 $post_type = 'post';
4305 if ( is_array( $post_type ) ) {
4306 if ( count( $post_type ) > 1 )
4308 $post_type = array_shift( $post_type );
4311 // Do not attempt redirect for hierarchical post types
4312 if ( is_post_type_hierarchical( $post_type ) )
4315 $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']);
4317 // if year, monthnum, or day have been specified, make our query more precise
4318 // just in case there are multiple identical _wp_old_slug values
4319 if ( '' != $wp_query->query_vars['year'] )
4320 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
4321 if ( '' != $wp_query->query_vars['monthnum'] )
4322 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
4323 if ( '' != $wp_query->query_vars['day'] )
4324 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
4326 $id = (int) $wpdb->get_var($query);
4331 $link = get_permalink($id);
4336 wp_redirect( $link, 301 ); // Permanent redirect
4342 * Set up global post data.
4346 * @param object $post Post data.
4347 * @uses do_action_ref_array() Calls 'the_post'
4348 * @return bool True when finished.
4350 function setup_postdata( $post ) {
4351 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
4353 $id = (int) $post->ID;
4355 $authordata = get_userdata($post->post_author);
4357 $currentday = mysql2date('d.m.y', $post->post_date, false);
4358 $currentmonth = mysql2date('m', $post->post_date, false);
4361 $page = get_query_var('page');
4364 if ( is_single() || is_page() || is_feed() )
4366 $content = $post->post_content;
4367 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
4370 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
4371 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
4372 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
4373 // Ignore nextpage at the beginning of the content.
4374 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
4375 $content = substr( $content, 15 );
4376 $pages = explode('<!--nextpage-->', $content);
4377 $numpages = count($pages);
4378 if ( $numpages > 1 )
4381 $pages = array( $post->post_content );
4385 * Fires once the post data has been setup.
4389 * @param WP_Post &$post The Post object (passed by reference).
4391 do_action_ref_array( 'the_post', array( &$post ) );