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.
24 function get_query_var($var) {
27 return $wp_query->get($var);
31 * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
33 * @uses WP_Query::get_queried_object
40 function get_queried_object() {
42 return $wp_query->get_queried_object();
46 * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
48 * @uses WP_Query::get_queried_object_id()
55 function get_queried_object_id() {
57 return $wp_query->get_queried_object_id();
63 * @see WP_Query::set()
67 * @param string $var Query variable key.
71 function set_query_var($var, $value) {
74 return $wp_query->set($var, $value);
78 * Set up The Loop with query parameters.
80 * This will override the current WordPress Loop and shouldn't be used more than
81 * once. This must not be used within the WordPress Loop.
86 * @param string $query
87 * @return array List of posts
89 function query_posts($query) {
90 $GLOBALS['wp_query'] = new WP_Query();
91 return $GLOBALS['wp_query']->query($query);
95 * Destroy the previous query and set up a new query.
97 * This should be used after {@link query_posts()} and before another {@link
98 * query_posts()}. This will remove obscure bugs that occur when the previous
99 * wp_query object is not destroyed properly before another is set up.
104 function wp_reset_query() {
105 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
110 * After looping through a separate query, this function restores
111 * the $post global to the current post in the main query.
116 function wp_reset_postdata() {
119 if ( isset( $wp_query ) ) {
120 $wp_query->reset_postdata();
129 * Is the query for an existing archive page?
131 * Month, Year, Category, Author, Post Type archive...
133 * @see WP_Query::is_archive()
139 function is_archive() {
142 if ( ! isset( $wp_query ) ) {
143 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
147 return $wp_query->is_archive();
151 * Is the query for an existing post type archive page?
153 * @see WP_Query::is_post_type_archive()
157 * @param mixed $post_types Optional. Post type or array of posts types to check against.
160 function is_post_type_archive( $post_types = '' ) {
163 if ( ! isset( $wp_query ) ) {
164 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
168 return $wp_query->is_post_type_archive( $post_types );
172 * Is the query for an existing attachment page?
174 * @see WP_Query::is_attachment()
180 function is_attachment() {
183 if ( ! isset( $wp_query ) ) {
184 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
188 return $wp_query->is_attachment();
192 * Is the query for an existing author archive page?
194 * If the $author parameter is specified, this function will additionally
195 * check if the query is for one of the authors specified.
197 * @see WP_Query::is_author()
201 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
204 function is_author( $author = '' ) {
207 if ( ! isset( $wp_query ) ) {
208 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
212 return $wp_query->is_author( $author );
216 * Is the query for an existing category archive page?
218 * If the $category parameter is specified, this function will additionally
219 * check if the query is for one of the categories specified.
221 * @see WP_Query::is_category()
225 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
228 function is_category( $category = '' ) {
231 if ( ! isset( $wp_query ) ) {
232 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
236 return $wp_query->is_category( $category );
240 * Is the query for an existing tag archive page?
242 * If the $tag parameter is specified, this function will additionally
243 * check if the query is for one of the tags specified.
245 * @see WP_Query::is_tag()
249 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
252 function is_tag( $tag = '' ) {
255 if ( ! isset( $wp_query ) ) {
256 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
260 return $wp_query->is_tag( $tag );
264 * Is the query for an existing taxonomy archive page?
266 * If the $taxonomy parameter is specified, this function will additionally
267 * check if the query is for that specific $taxonomy.
269 * If the $term parameter is specified in addition to the $taxonomy parameter,
270 * this function will additionally check if the query is for one of the terms
273 * @see WP_Query::is_tax()
277 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
278 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
281 function is_tax( $taxonomy = '', $term = '' ) {
284 if ( ! isset( $wp_query ) ) {
285 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
289 return $wp_query->is_tax( $taxonomy, $term );
293 * Whether the current URL is within the comments popup window.
295 * @see WP_Query::is_comments_popup()
301 function is_comments_popup() {
304 if ( ! isset( $wp_query ) ) {
305 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
309 return $wp_query->is_comments_popup();
313 * Is the query for an existing date archive?
315 * @see WP_Query::is_date()
324 if ( ! isset( $wp_query ) ) {
325 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
329 return $wp_query->is_date();
333 * Is the query for an existing day archive?
335 * @see WP_Query::is_day()
344 if ( ! isset( $wp_query ) ) {
345 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
349 return $wp_query->is_day();
353 * Is the query for a feed?
355 * @see WP_Query::is_feed()
359 * @param string|array $feeds Optional feed types to check.
362 function is_feed( $feeds = '' ) {
365 if ( ! isset( $wp_query ) ) {
366 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
370 return $wp_query->is_feed( $feeds );
374 * Is the query for a comments feed?
376 * @see WP_Query::is_comments_feed()
382 function is_comment_feed() {
385 if ( ! isset( $wp_query ) ) {
386 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
390 return $wp_query->is_comment_feed();
394 * Is the query for the front page of the site?
396 * This is for what is displayed at your site's main URL.
398 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
400 * If you set a static page for the front page of your site, this function will return
401 * true when viewing that page.
403 * Otherwise the same as @see is_home()
405 * @see WP_Query::is_front_page()
410 * @return bool True, if front of site.
412 function is_front_page() {
415 if ( ! isset( $wp_query ) ) {
416 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
420 return $wp_query->is_front_page();
424 * Is the query for the blog homepage?
426 * This is the page which shows the time based blog content of your site.
428 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
430 * If you set a static page for the front page of your site, this function will return
431 * true only on the page you set as the "Posts page".
433 * @see is_front_page()
435 * @see WP_Query::is_home()
439 * @return bool True if blog view homepage.
444 if ( ! isset( $wp_query ) ) {
445 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
449 return $wp_query->is_home();
453 * Is the query for an existing month archive?
455 * @see WP_Query::is_month()
461 function is_month() {
464 if ( ! isset( $wp_query ) ) {
465 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
469 return $wp_query->is_month();
473 * Is the query for an existing single page?
475 * If the $page parameter is specified, this function will additionally
476 * check if the query is for one of the pages specified.
481 * @see WP_Query::is_page()
485 * @param mixed $page Page ID, title, slug, or array of such.
488 function is_page( $page = '' ) {
491 if ( ! isset( $wp_query ) ) {
492 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
496 return $wp_query->is_page( $page );
500 * Is the query for paged result and not for the first page?
502 * @see WP_Query::is_paged()
508 function is_paged() {
511 if ( ! isset( $wp_query ) ) {
512 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
516 return $wp_query->is_paged();
520 * Is the query for a post or page preview?
522 * @see WP_Query::is_preview()
528 function is_preview() {
531 if ( ! isset( $wp_query ) ) {
532 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
536 return $wp_query->is_preview();
540 * Is the query for the robots file?
542 * @see WP_Query::is_robots()
548 function is_robots() {
551 if ( ! isset( $wp_query ) ) {
552 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
556 return $wp_query->is_robots();
560 * Is the query for a search?
562 * @see WP_Query::is_search()
568 function is_search() {
571 if ( ! isset( $wp_query ) ) {
572 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
576 return $wp_query->is_search();
580 * Is the query for an existing single post?
582 * Works for any post type, except attachments and pages
584 * If the $post parameter is specified, this function will additionally
585 * check if the query is for one of the Posts specified.
590 * @see WP_Query::is_single()
594 * @param mixed $post Post ID, title, slug, or array of such.
597 function is_single( $post = '' ) {
600 if ( ! isset( $wp_query ) ) {
601 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
605 return $wp_query->is_single( $post );
609 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
611 * If the $post_types parameter is specified, this function will additionally
612 * check if the query is for one of the Posts Types specified.
617 * @see WP_Query::is_singular()
621 * @param mixed $post_types Optional. Post Type or array of Post Types
624 function is_singular( $post_types = '' ) {
627 if ( ! isset( $wp_query ) ) {
628 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
632 return $wp_query->is_singular( $post_types );
636 * Is the query for a specific time?
638 * @see WP_Query::is_time()
647 if ( ! isset( $wp_query ) ) {
648 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
652 return $wp_query->is_time();
656 * Is the query for a trackback endpoint call?
658 * @see WP_Query::is_trackback()
664 function is_trackback() {
667 if ( ! isset( $wp_query ) ) {
668 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
672 return $wp_query->is_trackback();
676 * Is the query for an existing year archive?
678 * @see WP_Query::is_year()
687 if ( ! isset( $wp_query ) ) {
688 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
692 return $wp_query->is_year();
696 * Is the query a 404 (returns no results)?
698 * @see WP_Query::is_404()
707 if ( ! isset( $wp_query ) ) {
708 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
712 return $wp_query->is_404();
716 * Is the query the main query?
722 function is_main_query() {
723 if ( 'pre_get_posts' === current_filter() ) {
724 $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
725 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
726 _doing_it_wrong( __FUNCTION__, $message, '3.7' );
730 return $wp_query->is_main_query();
734 * The Loop. Post loop control.
738 * Whether current WordPress query has results to loop over.
740 * @see WP_Query::have_posts()
746 function have_posts() {
749 return $wp_query->have_posts();
753 * Whether the caller is in the Loop.
758 * @return bool True if caller is within loop, false if loop hasn't started or ended.
760 function in_the_loop() {
763 return $wp_query->in_the_loop;
767 * Rewind the loop posts.
769 * @see WP_Query::rewind_posts()
775 function rewind_posts() {
778 return $wp_query->rewind_posts();
782 * Iterate the post index in the loop.
784 * @see WP_Query::the_post()
788 function the_post() {
791 $wp_query->the_post();
799 * Whether there are comments to loop over.
801 * @see WP_Query::have_comments()
807 function have_comments() {
809 return $wp_query->have_comments();
813 * Iterate comment index in the comment loop.
815 * @see WP_Query::the_comment()
821 function the_comment() {
823 return $wp_query->the_comment();
831 * The WordPress Query class.
833 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
840 * Query vars set by the user
849 * Query vars, after parsing
855 var $query_vars = array();
858 * Taxonomy query, as passed to get_tax_sql()
862 * @var object WP_Tax_Query
867 * Metadata query container
871 * @var object WP_Meta_Query
873 var $meta_query = false;
876 * Date query container
880 * @var object WP_Date_Query
882 var $date_query = false;
885 * Holds the data for a single object that is queried.
887 * Holds the contents of a post, page, category, attachment.
896 * The ID of the queried object.
902 var $queried_object_id;
905 * Get post database query.
923 * The amount of posts for the current query.
932 * Index of the current item in the loop.
938 var $current_post = -1;
941 * Whether the loop has started and the caller is in the loop.
947 var $in_the_loop = false;
959 * The list of comments for current post.
968 * The amount of comments for the posts.
974 var $comment_count = 0;
977 * The index of the comment in the comment loop.
983 var $current_comment = -1;
986 * Current comment ID.
995 * The amount of found posts for the current query.
997 * If limit clause was not used, equals $post_count.
1003 var $found_posts = 0;
1006 * The amount of pages.
1012 var $max_num_pages = 0;
1015 * The amount of comment pages.
1021 var $max_num_comment_pages = 0;
1024 * Set if query is single post.
1030 var $is_single = false;
1033 * Set if query is preview of blog.
1039 var $is_preview = false;
1042 * Set if query returns a page.
1048 var $is_page = false;
1051 * Set if query is an archive list.
1057 var $is_archive = false;
1060 * Set if query is part of a date.
1066 var $is_date = false;
1069 * Set if query contains a year.
1075 var $is_year = false;
1078 * Set if query contains a month.
1084 var $is_month = false;
1087 * Set if query contains a day.
1093 var $is_day = false;
1096 * Set if query contains time.
1102 var $is_time = false;
1105 * Set if query contains an author.
1111 var $is_author = false;
1114 * Set if query contains category.
1120 var $is_category = false;
1123 * Set if query contains tag.
1129 var $is_tag = false;
1132 * Set if query contains taxonomy.
1138 var $is_tax = false;
1141 * Set if query was part of a search result.
1147 var $is_search = false;
1150 * Set if query is feed display.
1156 var $is_feed = false;
1159 * Set if query is comment feed display.
1165 var $is_comment_feed = false;
1168 * Set if query is trackback.
1174 var $is_trackback = false;
1177 * Set if query is blog homepage.
1183 var $is_home = false;
1186 * Set if query couldn't found anything.
1192 var $is_404 = false;
1195 * Set if query is within comments popup window.
1201 var $is_comments_popup = false;
1204 * Set if query is paged
1210 var $is_paged = false;
1213 * Set if query is part of administration page.
1219 var $is_admin = false;
1222 * Set if query is an attachment.
1228 var $is_attachment = false;
1231 * Set if is single, is a page, or is an attachment.
1237 var $is_singular = false;
1240 * Set if query is for robots.
1246 var $is_robots = false;
1249 * Set if query contains posts.
1251 * Basically, the homepage if the option isn't set for the static homepage.
1257 var $is_posts_page = false;
1260 * Set if query is for a post type archive.
1266 var $is_post_type_archive = false;
1269 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1270 * whether we have to re-parse because something has changed
1275 var $query_vars_hash = false;
1278 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1279 * via pre_get_posts hooks.
1284 var $query_vars_changed = true;
1287 * Set if post thumbnails are cached
1293 var $thumbnails_cached = false;
1296 * Cached list of search stopwords.
1304 * Resets query flags to false.
1306 * The query flags are what page info WordPress was able to figure out.
1311 function init_query_flags() {
1312 $this->is_single = false;
1313 $this->is_preview = false;
1314 $this->is_page = false;
1315 $this->is_archive = false;
1316 $this->is_date = false;
1317 $this->is_year = false;
1318 $this->is_month = false;
1319 $this->is_day = false;
1320 $this->is_time = false;
1321 $this->is_author = false;
1322 $this->is_category = false;
1323 $this->is_tag = false;
1324 $this->is_tax = false;
1325 $this->is_search = false;
1326 $this->is_feed = false;
1327 $this->is_comment_feed = false;
1328 $this->is_trackback = false;
1329 $this->is_home = false;
1330 $this->is_404 = false;
1331 $this->is_comments_popup = false;
1332 $this->is_paged = false;
1333 $this->is_admin = false;
1334 $this->is_attachment = false;
1335 $this->is_singular = false;
1336 $this->is_robots = false;
1337 $this->is_posts_page = false;
1338 $this->is_post_type_archive = false;
1342 * Initiates object properties and sets default values.
1348 unset($this->posts);
1349 unset($this->query);
1350 $this->query_vars = array();
1351 unset($this->queried_object);
1352 unset($this->queried_object_id);
1353 $this->post_count = 0;
1354 $this->current_post = -1;
1355 $this->in_the_loop = false;
1356 unset( $this->request );
1357 unset( $this->post );
1358 unset( $this->comments );
1359 unset( $this->comment );
1360 $this->comment_count = 0;
1361 $this->current_comment = -1;
1362 $this->found_posts = 0;
1363 $this->max_num_pages = 0;
1364 $this->max_num_comment_pages = 0;
1366 $this->init_query_flags();
1370 * Reparse the query vars.
1375 function parse_query_vars() {
1376 $this->parse_query();
1380 * Fills in the query variables, which do not exist within the parameter.
1385 * @param array $array Defined query variables.
1386 * @return array Complete query variables with undefined ones filled in empty.
1388 function fill_query_vars($array) {
1428 foreach ( $keys as $key ) {
1429 if ( !isset($array[$key]) )
1433 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1434 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
1435 'author__in', 'author__not_in' );
1437 foreach ( $array_keys as $key ) {
1438 if ( !isset($array[$key]) )
1439 $array[$key] = array();
1445 * Parse a query string and set query type booleans.
1450 * @param string|array $query Optional query.
1452 function parse_query( $query = '' ) {
1453 if ( ! empty( $query ) ) {
1455 $this->query = $this->query_vars = wp_parse_args( $query );
1456 } elseif ( ! isset( $this->query ) ) {
1457 $this->query = $this->query_vars;
1460 $this->query_vars = $this->fill_query_vars($this->query_vars);
1461 $qv = &$this->query_vars;
1462 $this->query_vars_changed = true;
1464 if ( ! empty($qv['robots']) )
1465 $this->is_robots = true;
1467 $qv['p'] = absint($qv['p']);
1468 $qv['page_id'] = absint($qv['page_id']);
1469 $qv['year'] = absint($qv['year']);
1470 $qv['monthnum'] = absint($qv['monthnum']);
1471 $qv['day'] = absint($qv['day']);
1472 $qv['w'] = absint($qv['w']);
1473 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
1474 $qv['paged'] = absint($qv['paged']);
1475 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1476 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
1477 $qv['pagename'] = trim( $qv['pagename'] );
1478 $qv['name'] = trim( $qv['name'] );
1479 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1480 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1481 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1482 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1484 // Fairly insane upper bound for search string lengths.
1485 if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
1488 // Compat. Map subpost to attachment.
1489 if ( '' != $qv['subpost'] )
1490 $qv['attachment'] = $qv['subpost'];
1491 if ( '' != $qv['subpost_id'] )
1492 $qv['attachment_id'] = $qv['subpost_id'];
1494 $qv['attachment_id'] = absint($qv['attachment_id']);
1496 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1497 $this->is_single = true;
1498 $this->is_attachment = true;
1499 } elseif ( '' != $qv['name'] ) {
1500 $this->is_single = true;
1501 } elseif ( $qv['p'] ) {
1502 $this->is_single = true;
1503 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1504 // If year, month, day, hour, minute, and second are set, a single
1505 // post is being queried.
1506 $this->is_single = true;
1507 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1508 $this->is_page = true;
1509 $this->is_single = false;
1511 // Look for archive queries. Dates, categories, authors, search, post type archives.
1513 if ( !empty($qv['s']) ) {
1514 $this->is_search = true;
1517 if ( '' !== $qv['second'] ) {
1518 $this->is_time = true;
1519 $this->is_date = true;
1522 if ( '' !== $qv['minute'] ) {
1523 $this->is_time = true;
1524 $this->is_date = true;
1527 if ( '' !== $qv['hour'] ) {
1528 $this->is_time = true;
1529 $this->is_date = true;
1533 if ( ! $this->is_date ) {
1534 $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
1535 if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
1536 $qv['error'] = '404';
1538 $this->is_day = true;
1539 $this->is_date = true;
1544 if ( $qv['monthnum'] ) {
1545 if ( ! $this->is_date ) {
1546 if ( 12 < $qv['monthnum'] ) {
1547 $qv['error'] = '404';
1549 $this->is_month = true;
1550 $this->is_date = true;
1555 if ( $qv['year'] ) {
1556 if ( ! $this->is_date ) {
1557 $this->is_year = true;
1558 $this->is_date = true;
1563 $this->is_date = true;
1564 if ( strlen($qv['m']) > 9 ) {
1565 $this->is_time = true;
1566 } else if ( strlen($qv['m']) > 7 ) {
1567 $this->is_day = true;
1568 } else if ( strlen($qv['m']) > 5 ) {
1569 $this->is_month = true;
1571 $this->is_year = true;
1575 if ( '' != $qv['w'] ) {
1576 $this->is_date = true;
1579 $this->query_vars_hash = false;
1580 $this->parse_tax_query( $qv );
1582 foreach ( $this->tax_query->queries as $tax_query ) {
1583 if ( 'NOT IN' != $tax_query['operator'] ) {
1584 switch ( $tax_query['taxonomy'] ) {
1586 $this->is_category = true;
1589 $this->is_tag = true;
1592 $this->is_tax = true;
1596 unset( $tax_query );
1598 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1599 $this->is_author = false;
1601 $this->is_author = true;
1604 if ( '' != $qv['author_name'] )
1605 $this->is_author = true;
1607 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1608 $post_type_obj = get_post_type_object( $qv['post_type'] );
1609 if ( ! empty( $post_type_obj->has_archive ) )
1610 $this->is_post_type_archive = true;
1613 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1614 $this->is_archive = true;
1617 if ( '' != $qv['feed'] )
1618 $this->is_feed = true;
1620 if ( '' != $qv['tb'] )
1621 $this->is_trackback = true;
1623 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1624 $this->is_paged = true;
1626 if ( '' != $qv['comments_popup'] )
1627 $this->is_comments_popup = true;
1629 // if we're previewing inside the write screen
1630 if ( '' != $qv['preview'] )
1631 $this->is_preview = true;
1634 $this->is_admin = true;
1636 if ( false !== strpos($qv['feed'], 'comments-') ) {
1637 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1638 $qv['withcomments'] = 1;
1641 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1643 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1644 $this->is_comment_feed = true;
1646 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 ) )
1647 $this->is_home = true;
1649 // Correct is_* for page_on_front and page_for_posts
1650 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1651 $_query = wp_parse_args($this->query);
1652 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1653 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1654 unset($_query['pagename']);
1655 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1656 $this->is_page = true;
1657 $this->is_home = false;
1658 $qv['page_id'] = get_option('page_on_front');
1659 // Correct <!--nextpage--> for page_on_front
1660 if ( !empty($qv['paged']) ) {
1661 $qv['page'] = $qv['paged'];
1662 unset($qv['paged']);
1667 if ( '' != $qv['pagename'] ) {
1668 $this->queried_object = get_page_by_path($qv['pagename']);
1669 if ( !empty($this->queried_object) )
1670 $this->queried_object_id = (int) $this->queried_object->ID;
1672 unset($this->queried_object);
1674 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1675 $this->is_page = false;
1676 $this->is_home = true;
1677 $this->is_posts_page = true;
1681 if ( $qv['page_id'] ) {
1682 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1683 $this->is_page = false;
1684 $this->is_home = true;
1685 $this->is_posts_page = true;
1689 if ( !empty($qv['post_type']) ) {
1690 if ( is_array($qv['post_type']) )
1691 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1693 $qv['post_type'] = sanitize_key($qv['post_type']);
1696 if ( ! empty( $qv['post_status'] ) ) {
1697 if ( is_array( $qv['post_status'] ) )
1698 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1700 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1703 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1704 $this->is_comment_feed = false;
1706 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1707 // Done correcting is_* for page_on_front and page_for_posts
1709 if ( '404' == $qv['error'] )
1712 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1713 $this->query_vars_changed = false;
1715 do_action_ref_array('parse_query', array(&$this));
1719 * Parses various taxonomy related query vars.
1724 * @param array &$q The query variables
1726 function parse_tax_query( &$q ) {
1727 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1728 $tax_query = $q['tax_query'];
1730 $tax_query = array();
1733 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1734 $tax_query[] = array(
1735 'taxonomy' => $q['taxonomy'],
1736 'terms' => array( $q['term'] ),
1741 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
1742 if ( 'post_tag' == $taxonomy )
1743 continue; // Handled further down in the $q['tag'] block
1745 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1746 $tax_query_defaults = array(
1747 'taxonomy' => $taxonomy,
1751 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1752 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1755 $term = $q[$t->query_var];
1757 if ( strpos($term, '+') !== false ) {
1758 $terms = preg_split( '/[+]+/', $term );
1759 foreach ( $terms as $term ) {
1760 $tax_query[] = array_merge( $tax_query_defaults, array(
1761 'terms' => array( $term )
1765 $tax_query[] = array_merge( $tax_query_defaults, array(
1766 'terms' => preg_split( '/[,]+/', $term )
1773 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
1774 $cat_in = $cat_not_in = array();
1776 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
1777 $cat_array = array_map( 'intval', $cat_array );
1778 $q['cat'] = implode( ',', $cat_array );
1780 foreach ( $cat_array as $cat ) {
1784 $cat_not_in[] = abs( $cat );
1787 if ( ! empty( $cat_in ) ) {
1788 $tax_query[] = array(
1789 'taxonomy' => 'category',
1791 'field' => 'term_id',
1792 'include_children' => true
1796 if ( ! empty( $cat_not_in ) ) {
1797 $tax_query[] = array(
1798 'taxonomy' => 'category',
1799 'terms' => $cat_not_in,
1800 'field' => 'term_id',
1801 'operator' => 'NOT IN',
1802 'include_children' => true
1805 unset( $cat_array, $cat_in, $cat_not_in );
1808 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1809 $q['category__and'] = (array) $q['category__and'];
1810 if ( ! isset( $q['category__in'] ) )
1811 $q['category__in'] = array();
1812 $q['category__in'][] = absint( reset( $q['category__and'] ) );
1813 unset( $q['category__and'] );
1816 if ( ! empty( $q['category__in'] ) ) {
1817 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1818 $tax_query[] = array(
1819 'taxonomy' => 'category',
1820 'terms' => $q['category__in'],
1821 'field' => 'term_id',
1822 'include_children' => false
1826 if ( ! empty($q['category__not_in']) ) {
1827 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1828 $tax_query[] = array(
1829 'taxonomy' => 'category',
1830 'terms' => $q['category__not_in'],
1831 'operator' => 'NOT IN',
1832 'include_children' => false
1836 if ( ! empty($q['category__and']) ) {
1837 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1838 $tax_query[] = array(
1839 'taxonomy' => 'category',
1840 'terms' => $q['category__and'],
1841 'field' => 'term_id',
1842 'operator' => 'AND',
1843 'include_children' => false
1848 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1849 if ( strpos($q['tag'], ',') !== false ) {
1850 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1851 foreach ( (array) $tags as $tag ) {
1852 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1853 $q['tag_slug__in'][] = $tag;
1855 } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1856 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1857 foreach ( (array) $tags as $tag ) {
1858 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1859 $q['tag_slug__and'][] = $tag;
1862 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1863 $q['tag_slug__in'][] = $q['tag'];
1867 if ( !empty($q['tag_id']) ) {
1868 $q['tag_id'] = absint( $q['tag_id'] );
1869 $tax_query[] = array(
1870 'taxonomy' => 'post_tag',
1871 'terms' => $q['tag_id']
1875 if ( !empty($q['tag__in']) ) {
1876 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1877 $tax_query[] = array(
1878 'taxonomy' => 'post_tag',
1879 'terms' => $q['tag__in']
1883 if ( !empty($q['tag__not_in']) ) {
1884 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1885 $tax_query[] = array(
1886 'taxonomy' => 'post_tag',
1887 'terms' => $q['tag__not_in'],
1888 'operator' => 'NOT IN'
1892 if ( !empty($q['tag__and']) ) {
1893 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1894 $tax_query[] = array(
1895 'taxonomy' => 'post_tag',
1896 'terms' => $q['tag__and'],
1901 if ( !empty($q['tag_slug__in']) ) {
1902 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1903 $tax_query[] = array(
1904 'taxonomy' => 'post_tag',
1905 'terms' => $q['tag_slug__in'],
1910 if ( !empty($q['tag_slug__and']) ) {
1911 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1912 $tax_query[] = array(
1913 'taxonomy' => 'post_tag',
1914 'terms' => $q['tag_slug__and'],
1920 $this->tax_query = new WP_Tax_Query( $tax_query );
1922 do_action( 'parse_tax_query', $this );
1926 * Generate SQL for the WHERE clause based on passed search terms.
1930 * @global wpdb $wpdb
1931 * @param array $q Query variables.
1932 * @return string WHERE clause.
1934 protected function parse_search( &$q ) {
1939 // added slashes screw with quote grouping when done early, so done later
1940 $q['s'] = stripslashes( $q['s'] );
1941 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1942 $q['s'] = urldecode( $q['s'] );
1943 // there are no line breaks in <input /> fields
1944 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1945 $q['search_terms_count'] = 1;
1946 if ( ! empty( $q['sentence'] ) ) {
1947 $q['search_terms'] = array( $q['s'] );
1949 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1950 $q['search_terms_count'] = count( $matches[0] );
1951 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1952 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1953 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1954 $q['search_terms'] = array( $q['s'] );
1956 $q['search_terms'] = array( $q['s'] );
1960 $n = ! empty( $q['exact'] ) ? '' : '%';
1962 $q['search_orderby_title'] = array();
1963 foreach ( $q['search_terms'] as $term ) {
1964 $term = like_escape( esc_sql( $term ) );
1966 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1968 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1969 $searchand = ' AND ';
1972 if ( ! empty( $search ) ) {
1973 $search = " AND ({$search}) ";
1974 if ( ! is_user_logged_in() )
1975 $search .= " AND ($wpdb->posts.post_password = '') ";
1982 * Check if the terms are suitable for searching.
1984 * Uses an array of stopwords (terms) that are excluded from the separate
1985 * term matching when searching for posts. The list of English stopwords is
1986 * the approximate search engines list, and is translatable.
1990 * @param array Terms to check.
1991 * @return array Terms that are not stopwords.
1993 protected function parse_search_terms( $terms ) {
1994 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
1997 $stopwords = $this->get_search_stopwords();
1999 foreach ( $terms as $term ) {
2000 // keep before/after spaces when term is for exact match
2001 if ( preg_match( '/^".+"$/', $term ) )
2002 $term = trim( $term, "\"'" );
2004 $term = trim( $term, "\"' " );
2006 // Avoid single A-Z.
2007 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
2010 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2020 * Retrieve stopwords used when parsing search terms.
2024 * @return array Stopwords.
2026 protected function get_search_stopwords() {
2027 if ( isset( $this->stopwords ) )
2028 return $this->stopwords;
2030 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2031 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2032 * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2034 $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',
2035 'Comma-separated list of search stopwords in your language' ) );
2037 $stopwords = array();
2038 foreach( $words as $word ) {
2039 $word = trim( $word, "\r\n\t " );
2041 $stopwords[] = $word;
2045 * Filter stopwords used when parsing search terms.
2049 * @param array $stopwords Stopwords.
2051 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2052 return $this->stopwords;
2056 * Generate SQL for the ORDER BY condition based on passed search terms.
2058 * @global wpdb $wpdb
2059 * @param array $q Query variables.
2060 * @return string ORDER BY clause.
2062 protected function parse_search_order( &$q ) {
2065 $search_orderby = '';
2067 if ( $q['search_terms_count'] > 1 ) {
2068 $num_terms = count( $q['search_orderby_title'] );
2069 $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2071 $search_orderby = '(CASE ';
2072 // sentence match in 'post_title'
2073 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2075 // sanity limit, sort as sentence when more than 6 terms
2076 // (few searches are longer than 6 terms and most titles are not)
2077 if ( $num_terms < 7 ) {
2078 // all words in title
2079 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2080 // any word in title, not needed when $num_terms == 1
2081 if ( $num_terms > 1 )
2082 $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2085 // sentence match in 'post_content'
2086 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2087 $search_orderby .= 'ELSE 5 END)';
2089 // single word or sentence search
2090 $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2093 return $search_orderby;
2097 * Sets the 404 property and saves whether query is feed.
2102 function set_404() {
2103 $is_feed = $this->is_feed;
2105 $this->init_query_flags();
2106 $this->is_404 = true;
2108 $this->is_feed = $is_feed;
2112 * Retrieve query variable.
2117 * @param string $query_var Query variable key.
2120 function get($query_var) {
2121 if ( isset($this->query_vars[$query_var]) )
2122 return $this->query_vars[$query_var];
2128 * Set query variable.
2133 * @param string $query_var Query variable key.
2134 * @param mixed $value Query variable value.
2136 function set($query_var, $value) {
2137 $this->query_vars[$query_var] = $value;
2141 * Retrieve the posts based on query variables.
2143 * There are a few filters and actions that can be used to modify the post
2148 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2150 * @return array List of posts.
2152 function get_posts() {
2155 $this->parse_query();
2157 do_action_ref_array('pre_get_posts', array(&$this));
2160 $q = &$this->query_vars;
2162 // Fill again in case pre_get_posts unset some vars.
2163 $q = $this->fill_query_vars($q);
2166 $this->meta_query = new WP_Meta_Query();
2167 $this->meta_query->parse_query_vars( $q );
2169 // Set a flag if a pre_get_posts hook changed the query vars.
2170 $hash = md5( serialize( $this->query_vars ) );
2171 if ( $hash != $this->query_vars_hash ) {
2172 $this->query_vars_changed = true;
2173 $this->query_vars_hash = $hash;
2177 // First let's clear some variables
2180 $whichmimetype = '';
2187 $post_status_join = false;
2190 if ( isset( $q['caller_get_posts'] ) ) {
2191 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2192 if ( !isset( $q['ignore_sticky_posts'] ) )
2193 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2196 if ( !isset( $q['ignore_sticky_posts'] ) )
2197 $q['ignore_sticky_posts'] = false;
2199 if ( !isset($q['suppress_filters']) )
2200 $q['suppress_filters'] = false;
2202 if ( !isset($q['cache_results']) ) {
2203 if ( wp_using_ext_object_cache() )
2204 $q['cache_results'] = false;
2206 $q['cache_results'] = true;
2209 if ( !isset($q['update_post_term_cache']) )
2210 $q['update_post_term_cache'] = true;
2212 if ( !isset($q['update_post_meta_cache']) )
2213 $q['update_post_meta_cache'] = true;
2215 if ( !isset($q['post_type']) ) {
2216 if ( $this->is_search )
2217 $q['post_type'] = 'any';
2219 $q['post_type'] = '';
2221 $post_type = $q['post_type'];
2222 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
2223 $q['posts_per_page'] = get_option('posts_per_page');
2224 if ( isset($q['showposts']) && $q['showposts'] ) {
2225 $q['showposts'] = (int) $q['showposts'];
2226 $q['posts_per_page'] = $q['showposts'];
2228 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2229 $q['posts_per_page'] = $q['posts_per_archive_page'];
2230 if ( !isset($q['nopaging']) ) {
2231 if ( $q['posts_per_page'] == -1 ) {
2232 $q['nopaging'] = true;
2234 $q['nopaging'] = false;
2237 if ( $this->is_feed ) {
2238 $q['posts_per_page'] = get_option('posts_per_rss');
2239 $q['nopaging'] = false;
2241 $q['posts_per_page'] = (int) $q['posts_per_page'];
2242 if ( $q['posts_per_page'] < -1 )
2243 $q['posts_per_page'] = abs($q['posts_per_page']);
2244 else if ( $q['posts_per_page'] == 0 )
2245 $q['posts_per_page'] = 1;
2247 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2248 $q['comments_per_page'] = get_option('comments_per_page');
2250 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2251 $this->is_page = true;
2252 $this->is_home = false;
2253 $q['page_id'] = get_option('page_on_front');
2256 if ( isset($q['page']) ) {
2257 $q['page'] = trim($q['page'], '/');
2258 $q['page'] = absint($q['page']);
2261 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2262 if ( isset($q['no_found_rows']) )
2263 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2265 $q['no_found_rows'] = false;
2267 switch ( $q['fields'] ) {
2269 $fields = "$wpdb->posts.ID";
2272 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2275 $fields = "$wpdb->posts.*";
2278 if ( '' !== $q['menu_order'] )
2279 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2281 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2283 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2284 if ( strlen($q['m']) > 5 )
2285 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2286 if ( strlen($q['m']) > 7 )
2287 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2288 if ( strlen($q['m']) > 9 )
2289 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2290 if ( strlen($q['m']) > 11 )
2291 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2292 if ( strlen($q['m']) > 13 )
2293 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2296 // Handle the other individual date parameters
2297 $date_parameters = array();
2299 if ( '' !== $q['hour'] )
2300 $date_parameters['hour'] = $q['hour'];
2302 if ( '' !== $q['minute'] )
2303 $date_parameters['minute'] = $q['minute'];
2305 if ( '' !== $q['second'] )
2306 $date_parameters['second'] = $q['second'];
2309 $date_parameters['year'] = $q['year'];
2311 if ( $q['monthnum'] )
2312 $date_parameters['monthnum'] = $q['monthnum'];
2315 $date_parameters['week'] = $q['w'];
2318 $date_parameters['day'] = $q['day'];
2320 if ( $date_parameters ) {
2321 $date_query = new WP_Date_Query( array( $date_parameters ) );
2322 $where .= $date_query->get_sql();
2324 unset( $date_parameters, $date_query );
2326 // Handle complex date queries
2327 if ( ! empty( $q['date_query'] ) ) {
2328 $this->date_query = new WP_Date_Query( $q['date_query'] );
2329 $where .= $this->date_query->get_sql();
2333 // If we've got a post_type AND it's not "any" post_type.
2334 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2335 foreach ( (array)$q['post_type'] as $_post_type ) {
2336 $ptype_obj = get_post_type_object($_post_type);
2337 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2340 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2341 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2342 $q['name'] = $q[ $ptype_obj->query_var ];
2344 // Hierarchical post_types will operate through the
2345 $q['pagename'] = $q[ $ptype_obj->query_var ];
2349 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2355 if ( '' != $q['name'] ) {
2356 $q['name'] = sanitize_title_for_query( $q['name'] );
2357 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2358 } elseif ( '' != $q['pagename'] ) {
2359 if ( isset($this->queried_object_id) ) {
2360 $reqpage = $this->queried_object_id;
2362 if ( 'page' != $q['post_type'] ) {
2363 foreach ( (array)$q['post_type'] as $_post_type ) {
2364 $ptype_obj = get_post_type_object($_post_type);
2365 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2368 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2374 $reqpage = get_page_by_path($q['pagename']);
2376 if ( !empty($reqpage) )
2377 $reqpage = $reqpage->ID;
2382 $page_for_posts = get_option('page_for_posts');
2383 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2384 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2385 $q['name'] = $q['pagename'];
2386 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2387 $reqpage_obj = get_post( $reqpage );
2388 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2389 $this->is_attachment = true;
2390 $post_type = $q['post_type'] = 'attachment';
2391 $this->is_page = true;
2392 $q['attachment_id'] = $reqpage;
2395 } elseif ( '' != $q['attachment'] ) {
2396 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2397 $q['name'] = $q['attachment'];
2398 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2402 if ( intval($q['comments_popup']) )
2403 $q['p'] = absint($q['comments_popup']);
2405 // If an attachment is requested by number, let it supersede any post number.
2406 if ( $q['attachment_id'] )
2407 $q['p'] = absint($q['attachment_id']);
2409 // If a post number is specified, load that post
2411 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2412 } elseif ( $q['post__in'] ) {
2413 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2414 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2415 } elseif ( $q['post__not_in'] ) {
2416 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2417 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2420 if ( is_numeric( $q['post_parent'] ) ) {
2421 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2422 } elseif ( $q['post_parent__in'] ) {
2423 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2424 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2425 } elseif ( $q['post_parent__not_in'] ) {
2426 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2427 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2430 if ( $q['page_id'] ) {
2431 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2432 $q['p'] = $q['page_id'];
2433 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2437 // If a search pattern is specified, load the posts that match.
2438 if ( ! empty( $q['s'] ) )
2439 $search = $this->parse_search( $q );
2442 * Filter the search SQL that is used in the WHERE clause of WP_Query.
2446 * @param string $search Search SQL for WHERE clause.
2447 * @param WP_Query $this The current WP_Query object.
2449 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2452 if ( !$this->is_singular ) {
2453 $this->parse_tax_query( $q );
2455 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2457 $join .= $clauses['join'];
2458 $where .= $clauses['where'];
2461 if ( $this->is_tax ) {
2462 if ( empty($post_type) ) {
2463 // Do a fully inclusive search for currently registered post types of queried taxonomies
2464 $post_type = array();
2465 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2466 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2467 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2468 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2473 elseif ( count( $post_type ) == 1 )
2474 $post_type = $post_type[0];
2476 $post_status_join = true;
2477 } elseif ( in_array('attachment', (array) $post_type) ) {
2478 $post_status_join = true;
2483 if ( !empty($this->tax_query->queries) ) {
2484 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2485 if ( !empty( $tax_query_in_and ) ) {
2486 if ( !isset( $q['taxonomy'] ) ) {
2487 foreach ( $tax_query_in_and as $a_tax_query ) {
2488 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2489 $q['taxonomy'] = $a_tax_query['taxonomy'];
2490 if ( 'slug' == $a_tax_query['field'] )
2491 $q['term'] = $a_tax_query['terms'][0];
2493 $q['term_id'] = $a_tax_query['terms'][0];
2500 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2501 if ( ! empty( $cat_query ) ) {
2502 $cat_query = reset( $cat_query );
2504 if ( ! empty( $cat_query['terms'][0] ) ) {
2505 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2507 $this->set( 'cat', $the_cat->term_id );
2508 $this->set( 'category_name', $the_cat->slug );
2513 unset( $cat_query );
2515 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2516 if ( ! empty( $tag_query ) ) {
2517 $tag_query = reset( $tag_query );
2519 if ( ! empty( $tag_query['terms'][0] ) ) {
2520 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2522 $this->set( 'tag_id', $the_tag->term_id );
2526 unset( $tag_query );
2530 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2531 $groupby = "{$wpdb->posts}.ID";
2534 // Author/user stuff
2536 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2537 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2538 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2539 foreach ( $authors as $author ) {
2540 $key = $author > 0 ? 'author__in' : 'author__not_in';
2541 $q[$key][] = abs( $author );
2543 $q['author'] = implode( ',', $authors );
2546 if ( ! empty( $q['author__not_in'] ) ) {
2547 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2548 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2549 } elseif ( ! empty( $q['author__in'] ) ) {
2550 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2551 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2554 // Author stuff for nice URLs
2556 if ( '' != $q['author_name'] ) {
2557 if ( strpos($q['author_name'], '/') !== false ) {
2558 $q['author_name'] = explode('/', $q['author_name']);
2559 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2560 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2562 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2565 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2566 $q['author'] = get_user_by('slug', $q['author_name']);
2568 $q['author'] = $q['author']->ID;
2569 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2572 // MIME-Type stuff for attachment browsing
2574 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2575 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2577 $where .= $search . $whichauthor . $whichmimetype;
2579 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2580 $q['order'] = 'DESC';
2583 if ( empty($q['orderby']) ) {
2584 $orderby = "$wpdb->posts.post_date " . $q['order'];
2585 } elseif ( 'none' == $q['orderby'] ) {
2587 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2588 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2589 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2590 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2592 // Used to filter values
2593 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2594 if ( !empty($q['meta_key']) ) {
2595 $allowed_keys[] = $q['meta_key'];
2596 $allowed_keys[] = 'meta_value';
2597 $allowed_keys[] = 'meta_value_num';
2599 $q['orderby'] = urldecode($q['orderby']);
2600 $q['orderby'] = addslashes_gpc($q['orderby']);
2602 $orderby_array = array();
2603 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2604 // Only allow certain values for safety
2605 if ( ! in_array($orderby, $allowed_keys) )
2608 switch ( $orderby ) {
2610 $orderby = "$wpdb->posts.menu_order";
2613 $orderby = "$wpdb->posts.ID";
2616 $orderby = 'RAND()';
2618 case $q['meta_key']:
2620 if ( isset( $q['meta_type'] ) ) {
2621 $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2622 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2624 $orderby = "$wpdb->postmeta.meta_value";
2627 case 'meta_value_num':
2628 $orderby = "$wpdb->postmeta.meta_value+0";
2630 case 'comment_count':
2631 $orderby = "$wpdb->posts.comment_count";
2634 $orderby = "$wpdb->posts.post_" . $orderby;
2637 $orderby_array[] = $orderby;
2639 $orderby = implode( ',', $orderby_array );
2641 if ( empty( $orderby ) )
2642 $orderby = "$wpdb->posts.post_date ".$q['order'];
2644 $orderby .= " {$q['order']}";
2647 // Order search results by relevance only when another "orderby" is not specified in the query.
2648 if ( ! empty( $q['s'] ) ) {
2649 $search_orderby = '';
2650 if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2651 $search_orderby = $this->parse_search_order( $q );
2654 * Filter the ORDER BY used when ordering search results.
2658 * @param string $search_orderby The ORDER BY clause.
2659 * @param WP_Query $this The current WP_Query instance.
2661 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2662 if ( $search_orderby )
2663 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2666 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2667 $post_type_cap = 'multiple_post_type';
2669 if ( is_array( $post_type ) )
2670 $post_type = reset( $post_type );
2671 $post_type_object = get_post_type_object( $post_type );
2672 if ( empty( $post_type_object ) )
2673 $post_type_cap = $post_type;
2676 if ( 'any' == $post_type ) {
2677 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2678 if ( empty( $in_search_post_types ) )
2679 $where .= ' AND 1=0 ';
2681 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2682 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2683 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2684 } elseif ( ! empty( $post_type ) ) {
2685 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2686 $post_type_object = get_post_type_object ( $post_type );
2687 } elseif ( $this->is_attachment ) {
2688 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2689 $post_type_object = get_post_type_object ( 'attachment' );
2690 } elseif ( $this->is_page ) {
2691 $where .= " AND $wpdb->posts.post_type = 'page'";
2692 $post_type_object = get_post_type_object ( 'page' );
2694 $where .= " AND $wpdb->posts.post_type = 'post'";
2695 $post_type_object = get_post_type_object ( 'post' );
2698 $edit_cap = 'edit_post';
2699 $read_cap = 'read_post';
2701 if ( ! empty( $post_type_object ) ) {
2702 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2703 $read_private_cap = $post_type_object->cap->read_private_posts;
2705 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2706 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2709 $user_id = get_current_user_id();
2711 if ( ! empty( $q['post_status'] ) ) {
2712 $statuswheres = array();
2713 $q_status = $q['post_status'];
2714 if ( ! is_array( $q_status ) )
2715 $q_status = explode(',', $q_status);
2716 $r_status = array();
2717 $p_status = array();
2718 $e_status = array();
2719 if ( in_array('any', $q_status) ) {
2720 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2721 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2723 foreach ( get_post_stati() as $status ) {
2724 if ( in_array( $status, $q_status ) ) {
2725 if ( 'private' == $status )
2726 $p_status[] = "$wpdb->posts.post_status = '$status'";
2728 $r_status[] = "$wpdb->posts.post_status = '$status'";
2733 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2734 $r_status = array_merge($r_status, $p_status);
2738 if ( !empty($e_status) ) {
2739 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2741 if ( !empty($r_status) ) {
2742 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2743 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2745 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2747 if ( !empty($p_status) ) {
2748 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2749 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2751 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2753 if ( $post_status_join ) {
2754 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2755 foreach ( $statuswheres as $index => $statuswhere )
2756 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2758 foreach ( $statuswheres as $statuswhere )
2759 $where .= " AND $statuswhere";
2760 } elseif ( !$this->is_singular ) {
2761 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2763 // Add public states.
2764 $public_states = get_post_stati( array('public' => true) );
2765 foreach ( (array) $public_states as $state ) {
2766 if ( 'publish' == $state ) // Publish is hard-coded above.
2768 $where .= " OR $wpdb->posts.post_status = '$state'";
2771 if ( $this->is_admin ) {
2772 // Add protected states that should show in the admin all list.
2773 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2774 foreach ( (array) $admin_all_states as $state )
2775 $where .= " OR $wpdb->posts.post_status = '$state'";
2778 if ( is_user_logged_in() ) {
2779 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2780 $private_states = get_post_stati( array('private' => true) );
2781 foreach ( (array) $private_states as $state )
2782 $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'";
2788 if ( !empty( $this->meta_query->queries ) ) {
2789 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2790 $join .= $clauses['join'];
2791 $where .= $clauses['where'];
2794 // Apply filters on where and join prior to paging so that any
2795 // manipulations to them are reflected in the paging by day queries.
2796 if ( !$q['suppress_filters'] ) {
2797 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2798 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2802 if ( empty($q['nopaging']) && !$this->is_singular ) {
2803 $page = absint($q['paged']);
2807 if ( empty($q['offset']) ) {
2808 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2809 } else { // we're ignoring $page and using 'offset'
2810 $q['offset'] = absint($q['offset']);
2811 $pgstrt = $q['offset'] . ', ';
2813 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2817 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2818 if ( $this->is_archive || $this->is_search ) {
2819 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2820 $cwhere = "WHERE comment_approved = '1' $where";
2821 $cgroupby = "$wpdb->comments.comment_id";
2822 } else { // Other non singular e.g. front
2823 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2824 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2828 if ( !$q['suppress_filters'] ) {
2829 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2830 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2831 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2832 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2833 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2835 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2836 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2838 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2839 $this->comment_count = count($this->comments);
2841 $post_ids = array();
2843 foreach ( $this->comments as $comment )
2844 $post_ids[] = (int) $comment->comment_post_ID;
2846 $post_ids = join(',', $post_ids);
2849 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2854 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2856 // Apply post-paging filters on where and join. Only plugins that
2857 // manipulate paging queries should use these hooks.
2858 if ( !$q['suppress_filters'] ) {
2859 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2860 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2861 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2862 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2863 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2864 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2865 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2867 // Filter all clauses at once, for convenience
2868 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2869 foreach ( $pieces as $piece )
2870 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2873 // Announce current selection parameters. For use by caching plugins.
2874 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2876 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2877 if ( !$q['suppress_filters'] ) {
2878 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2879 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2880 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2881 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2882 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2883 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2884 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2886 // Filter all clauses at once, for convenience
2887 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2888 foreach ( $pieces as $piece )
2889 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2892 if ( ! empty($groupby) )
2893 $groupby = 'GROUP BY ' . $groupby;
2894 if ( !empty( $orderby ) )
2895 $orderby = 'ORDER BY ' . $orderby;
2898 if ( !$q['no_found_rows'] && !empty($limits) )
2899 $found_rows = 'SQL_CALC_FOUND_ROWS';
2901 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2903 if ( !$q['suppress_filters'] ) {
2904 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2907 if ( 'ids' == $q['fields'] ) {
2908 $this->posts = $wpdb->get_col( $this->request );
2909 $this->post_count = count( $this->posts );
2910 $this->set_found_posts( $q, $limits );
2912 return $this->posts;
2915 if ( 'id=>parent' == $q['fields'] ) {
2916 $this->posts = $wpdb->get_results( $this->request );
2917 $this->post_count = count( $this->posts );
2918 $this->set_found_posts( $q, $limits );
2921 foreach ( $this->posts as $post )
2922 $r[ $post->ID ] = $post->post_parent;
2927 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2928 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2930 if ( $split_the_query ) {
2931 // First get the IDs and then fill in the objects
2933 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2935 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2937 $ids = $wpdb->get_col( $this->request );
2940 $this->posts = $ids;
2941 $this->set_found_posts( $q, $limits );
2942 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2944 $this->posts = array();
2947 $this->posts = $wpdb->get_results( $this->request );
2948 $this->set_found_posts( $q, $limits );
2951 // Convert to WP_Post objects
2953 $this->posts = array_map( 'get_post', $this->posts );
2955 // Raw results filter. Prior to status checks.
2956 if ( !$q['suppress_filters'] )
2957 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2959 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2960 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2961 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2962 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2963 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2964 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2965 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2966 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2967 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2968 $this->comments = $wpdb->get_results($comments_request);
2969 $this->comment_count = count($this->comments);
2972 // Check post status to determine if post should be displayed.
2973 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2974 $status = get_post_status($this->posts[0]);
2975 $post_status_obj = get_post_status_object($status);
2976 //$type = get_post_type($this->posts[0]);
2977 if ( !$post_status_obj->public ) {
2978 if ( ! is_user_logged_in() ) {
2979 // User must be logged in to view unpublished posts.
2980 $this->posts = array();
2982 if ( $post_status_obj->protected ) {
2983 // User must have edit permissions on the draft to preview.
2984 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2985 $this->posts = array();
2987 $this->is_preview = true;
2988 if ( 'future' != $status )
2989 $this->posts[0]->post_date = current_time('mysql');
2991 } elseif ( $post_status_obj->private ) {
2992 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2993 $this->posts = array();
2995 $this->posts = array();
3000 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
3001 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
3004 // Put sticky posts at the top of the posts array
3005 $sticky_posts = get_option('sticky_posts');
3006 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
3007 $num_posts = count($this->posts);
3009 // Loop over posts and relocate stickies to the front.
3010 for ( $i = 0; $i < $num_posts; $i++ ) {
3011 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
3012 $sticky_post = $this->posts[$i];
3013 // Remove sticky from current position
3014 array_splice($this->posts, $i, 1);
3015 // Move to front, after other stickies
3016 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
3017 // Increment the sticky offset. The next sticky will be placed at this offset.
3019 // Remove post from sticky posts array
3020 $offset = array_search($sticky_post->ID, $sticky_posts);
3021 unset( $sticky_posts[$offset] );
3025 // If any posts have been excluded specifically, Ignore those that are sticky.
3026 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3027 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3029 // Fetch sticky posts that weren't in the query results
3030 if ( !empty($sticky_posts) ) {
3031 $stickies = get_posts( array(
3032 'post__in' => $sticky_posts,
3033 'post_type' => $post_type,
3034 'post_status' => 'publish',
3038 foreach ( $stickies as $sticky_post ) {
3039 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3045 if ( !$q['suppress_filters'] )
3046 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
3048 // Ensure that any posts added/modified via one of the filters above are
3049 // of the type WP_Post and are filtered.
3050 if ( $this->posts ) {
3051 $this->post_count = count( $this->posts );
3053 $this->posts = array_map( 'get_post', $this->posts );
3055 if ( $q['cache_results'] )
3056 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3058 $this->post = reset( $this->posts );
3060 $this->post_count = 0;
3061 $this->posts = array();
3064 return $this->posts;
3068 * Set up the amount of found posts and the number of pages (if limit clause was used)
3069 * for the current query.
3074 function set_found_posts( $q, $limits ) {
3077 // Bail if posts is an empty array. Continue if posts is an empty string,
3078 // null, or false to accommodate caching plugins that fill posts later.
3079 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3082 if ( ! empty( $limits ) )
3083 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3085 $this->found_posts = count( $this->posts );
3087 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3089 if ( ! empty( $limits ) )
3090 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3094 * Set up the next post and iterate current post index.
3099 * @return WP_Post Next post.
3101 function next_post() {
3103 $this->current_post++;
3105 $this->post = $this->posts[$this->current_post];
3110 * Sets up the current post.
3112 * Retrieves the next post, sets up the post, sets the 'in the loop'
3118 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3120 function the_post() {
3122 $this->in_the_loop = true;
3124 if ( $this->current_post == -1 ) // loop has just started
3125 do_action_ref_array('loop_start', array(&$this));
3127 $post = $this->next_post();
3128 setup_postdata($post);
3132 * Whether there are more posts available in the loop.
3134 * Calls action 'loop_end', when the loop is complete.
3138 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3140 * @return bool True if posts are available, false if end of loop.
3142 function have_posts() {
3143 if ( $this->current_post + 1 < $this->post_count ) {
3145 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3146 do_action_ref_array('loop_end', array(&$this));
3147 // Do some cleaning up after the loop
3148 $this->rewind_posts();
3151 $this->in_the_loop = false;
3156 * Rewind the posts and reset post index.
3161 function rewind_posts() {
3162 $this->current_post = -1;
3163 if ( $this->post_count > 0 ) {
3164 $this->post = $this->posts[0];
3169 * Iterate current comment index and return comment object.
3174 * @return object Comment object.
3176 function next_comment() {
3177 $this->current_comment++;
3179 $this->comment = $this->comments[$this->current_comment];
3180 return $this->comment;
3184 * Sets up the current comment.
3188 * @global object $comment Current comment.
3189 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3191 function the_comment() {
3194 $comment = $this->next_comment();
3196 if ( $this->current_comment == 0 ) {
3197 do_action('comment_loop_start');
3202 * Whether there are more comments available.
3204 * Automatically rewinds comments when finished.
3209 * @return bool True, if more comments. False, if no more posts.
3211 function have_comments() {
3212 if ( $this->current_comment + 1 < $this->comment_count ) {
3214 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3215 $this->rewind_comments();
3222 * Rewind the comments, resets the comment index and comment to first.
3227 function rewind_comments() {
3228 $this->current_comment = -1;
3229 if ( $this->comment_count > 0 ) {
3230 $this->comment = $this->comments[0];
3235 * Sets up the WordPress query by parsing query string.
3240 * @param string $query URL query string.
3241 * @return array List of posts.
3243 function query( $query ) {
3245 $this->query = $this->query_vars = wp_parse_args( $query );
3246 return $this->get_posts();
3250 * Retrieve queried object.
3252 * If queried object is not set, then the queried object will be set from
3253 * the category, tag, taxonomy, posts page, single post, page, or author
3254 * query variable. After it is set up, it will be returned.
3261 function get_queried_object() {
3262 if ( isset($this->queried_object) )
3263 return $this->queried_object;
3265 $this->queried_object = null;
3266 $this->queried_object_id = 0;
3268 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3269 if ( $this->is_category ) {
3270 if ( $this->get( 'cat' ) ) {
3271 $term = get_term( $this->get( 'cat' ), 'category' );
3272 } elseif ( $this->get( 'category_name' ) ) {
3273 $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
3275 } elseif ( $this->is_tag ) {
3276 if ( $this->get( 'tag_id' ) ) {
3277 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
3278 } elseif ( $this->get( 'tag' ) ) {
3279 $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
3282 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3283 $query = reset( $tax_query_in_and );
3285 if ( $query['terms'] ) {
3286 if ( 'term_id' == $query['field'] ) {
3287 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3289 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3294 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3295 $this->queried_object = $term;
3296 $this->queried_object_id = (int) $term->term_id;
3298 if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
3299 _make_cat_compat( $this->queried_object );
3301 } elseif ( $this->is_post_type_archive ) {
3302 $post_type = $this->get( 'post_type' );
3303 if ( is_array( $post_type ) )
3304 $post_type = reset( $post_type );
3305 $this->queried_object = get_post_type_object( $post_type );
3306 } elseif ( $this->is_posts_page ) {
3307 $page_for_posts = get_option('page_for_posts');
3308 $this->queried_object = get_post( $page_for_posts );
3309 $this->queried_object_id = (int) $this->queried_object->ID;
3310 } elseif ( $this->is_singular && !is_null($this->post) ) {
3311 $this->queried_object = $this->post;
3312 $this->queried_object_id = (int) $this->post->ID;
3313 } elseif ( $this->is_author ) {
3314 $this->queried_object_id = (int) $this->get('author');
3315 $this->queried_object = get_userdata( $this->queried_object_id );
3318 return $this->queried_object;
3322 * Retrieve ID of the current queried object.
3329 function get_queried_object_id() {
3330 $this->get_queried_object();
3332 if ( isset($this->queried_object_id) ) {
3333 return $this->queried_object_id;
3342 * Sets up the WordPress query, if parameter is not empty.
3347 * @param string $query URL query string.
3350 function __construct($query = '') {
3351 if ( ! empty($query) ) {
3352 $this->query($query);
3357 * Is the query for an existing archive page?
3359 * Month, Year, Category, Author, Post Type archive...
3365 function is_archive() {
3366 return (bool) $this->is_archive;
3370 * Is the query for an existing post type archive page?
3374 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3377 function is_post_type_archive( $post_types = '' ) {
3378 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3379 return (bool) $this->is_post_type_archive;
3381 $post_type = $this->get( 'post_type' );
3382 if ( is_array( $post_type ) )
3383 $post_type = reset( $post_type );
3384 $post_type_object = get_post_type_object( $post_type );
3386 return in_array( $post_type_object->name, (array) $post_types );
3390 * Is the query for an existing attachment page?
3396 function is_attachment() {
3397 return (bool) $this->is_attachment;
3401 * Is the query for an existing author archive page?
3403 * If the $author parameter is specified, this function will additionally
3404 * check if the query is for one of the authors specified.
3408 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3411 function is_author( $author = '' ) {
3412 if ( !$this->is_author )
3415 if ( empty($author) )
3418 $author_obj = $this->get_queried_object();
3420 $author = (array) $author;
3422 if ( in_array( $author_obj->ID, $author ) )
3424 elseif ( in_array( $author_obj->nickname, $author ) )
3426 elseif ( in_array( $author_obj->user_nicename, $author ) )
3433 * Is the query for an existing category archive page?
3435 * If the $category parameter is specified, this function will additionally
3436 * check if the query is for one of the categories specified.
3440 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3443 function is_category( $category = '' ) {
3444 if ( !$this->is_category )
3447 if ( empty($category) )
3450 $cat_obj = $this->get_queried_object();
3452 $category = (array) $category;
3454 if ( in_array( $cat_obj->term_id, $category ) )
3456 elseif ( in_array( $cat_obj->name, $category ) )
3458 elseif ( in_array( $cat_obj->slug, $category ) )
3465 * Is the query for an existing tag archive page?
3467 * If the $tag parameter is specified, this function will additionally
3468 * check if the query is for one of the tags specified.
3472 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3475 function is_tag( $tag = '' ) {
3476 if ( ! $this->is_tag )
3479 if ( empty( $tag ) )
3482 $tag_obj = $this->get_queried_object();
3484 $tag = (array) $tag;
3486 if ( in_array( $tag_obj->term_id, $tag ) )
3488 elseif ( in_array( $tag_obj->name, $tag ) )
3490 elseif ( in_array( $tag_obj->slug, $tag ) )
3497 * Is the query for an existing taxonomy archive page?
3499 * If the $taxonomy parameter is specified, this function will additionally
3500 * check if the query is for that specific $taxonomy.
3502 * If the $term parameter is specified in addition to the $taxonomy parameter,
3503 * this function will additionally check if the query is for one of the terms
3508 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3509 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3512 function is_tax( $taxonomy = '', $term = '' ) {
3513 global $wp_taxonomies;
3515 if ( !$this->is_tax )
3518 if ( empty( $taxonomy ) )
3521 $queried_object = $this->get_queried_object();
3522 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3523 $term_array = (array) $term;
3525 // Check that the taxonomy matches.
3526 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3529 // Only a Taxonomy provided.
3530 if ( empty( $term ) )
3533 return isset( $queried_object->term_id ) &&
3534 count( array_intersect(
3535 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3541 * Whether the current URL is within the comments popup window.
3547 function is_comments_popup() {
3548 return (bool) $this->is_comments_popup;
3552 * Is the query for an existing date archive?
3558 function is_date() {
3559 return (bool) $this->is_date;
3563 * Is the query for an existing day archive?
3570 return (bool) $this->is_day;
3574 * Is the query for a feed?
3578 * @param string|array $feeds Optional feed types to check.
3581 function is_feed( $feeds = '' ) {
3582 if ( empty( $feeds ) || ! $this->is_feed )
3583 return (bool) $this->is_feed;
3584 $qv = $this->get( 'feed' );
3585 if ( 'feed' == $qv )
3586 $qv = get_default_feed();
3587 return in_array( $qv, (array) $feeds );
3591 * Is the query for a comments feed?
3597 function is_comment_feed() {
3598 return (bool) $this->is_comment_feed;
3602 * Is the query for the front page of the site?
3604 * This is for what is displayed at your site's main URL.
3606 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3608 * If you set a static page for the front page of your site, this function will return
3609 * true when viewing that page.
3611 * Otherwise the same as @see WP_Query::is_home()
3615 * @uses get_option()
3617 * @return bool True, if front of site.
3619 function is_front_page() {
3621 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3623 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3630 * Is the query for the blog homepage?
3632 * This is the page which shows the time based blog content of your site.
3634 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3636 * If you set a static page for the front page of your site, this function will return
3637 * true only on the page you set as the "Posts page".
3639 * @see WP_Query::is_front_page()
3643 * @return bool True if blog view homepage.
3645 function is_home() {
3646 return (bool) $this->is_home;
3650 * Is the query for an existing month archive?
3656 function is_month() {
3657 return (bool) $this->is_month;
3661 * Is the query for an existing single page?
3663 * If the $page parameter is specified, this function will additionally
3664 * check if the query is for one of the pages specified.
3666 * @see WP_Query::is_single()
3667 * @see WP_Query::is_singular()
3671 * @param mixed $page Page ID, title, slug, or array of such.
3674 function is_page( $page = '' ) {
3675 if ( !$this->is_page )
3678 if ( empty( $page ) )
3681 $page_obj = $this->get_queried_object();
3683 $page = (array) $page;
3685 if ( in_array( $page_obj->ID, $page ) )
3687 elseif ( in_array( $page_obj->post_title, $page ) )
3689 else if ( in_array( $page_obj->post_name, $page ) )
3696 * Is the query for paged result and not for the first page?
3702 function is_paged() {
3703 return (bool) $this->is_paged;
3707 * Is the query for a post or page preview?
3713 function is_preview() {
3714 return (bool) $this->is_preview;
3718 * Is the query for the robots file?
3724 function is_robots() {
3725 return (bool) $this->is_robots;
3729 * Is the query for a search?
3735 function is_search() {
3736 return (bool) $this->is_search;
3740 * Is the query for an existing single post?
3742 * Works for any post type, except attachments and pages
3744 * If the $post parameter is specified, this function will additionally
3745 * check if the query is for one of the Posts specified.
3747 * @see WP_Query::is_page()
3748 * @see WP_Query::is_singular()
3752 * @param mixed $post Post ID, title, slug, or array of such.
3755 function is_single( $post = '' ) {
3756 if ( !$this->is_single )
3762 $post_obj = $this->get_queried_object();
3764 $post = (array) $post;
3766 if ( in_array( $post_obj->ID, $post ) )
3768 elseif ( in_array( $post_obj->post_title, $post ) )
3770 elseif ( in_array( $post_obj->post_name, $post ) )
3777 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
3779 * If the $post_types parameter is specified, this function will additionally
3780 * check if the query is for one of the Posts Types specified.
3782 * @see WP_Query::is_page()
3783 * @see WP_Query::is_single()
3787 * @param mixed $post_types Optional. Post Type or array of Post Types
3790 function is_singular( $post_types = '' ) {
3791 if ( empty( $post_types ) || !$this->is_singular )
3792 return (bool) $this->is_singular;
3794 $post_obj = $this->get_queried_object();
3796 return in_array( $post_obj->post_type, (array) $post_types );
3800 * Is the query for a specific time?
3806 function is_time() {
3807 return (bool) $this->is_time;
3811 * Is the query for a trackback endpoint call?
3817 function is_trackback() {
3818 return (bool) $this->is_trackback;
3822 * Is the query for an existing year archive?
3828 function is_year() {
3829 return (bool) $this->is_year;
3833 * Is the query a 404 (returns no results)?
3840 return (bool) $this->is_404;
3844 * Is the query the main query?
3850 function is_main_query() {
3851 global $wp_the_query;
3852 return $wp_the_query === $this;
3856 * After looping through a nested query, this function
3857 * restores the $post global to the current post in this query.
3863 function reset_postdata() {
3864 if ( ! empty( $this->post ) ) {
3865 $GLOBALS['post'] = $this->post;
3866 setup_postdata( $this->post );
3872 * Redirect old slugs to the correct permalink.
3874 * Attempts to find the current slug from the past slugs.
3880 * @return null If no link is found, null is returned.
3882 function wp_old_slug_redirect() {
3884 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3887 // Guess the current post_type based on the query vars.
3888 if ( get_query_var('post_type') )
3889 $post_type = get_query_var('post_type');
3890 elseif ( !empty($wp_query->query_vars['pagename']) )
3891 $post_type = 'page';
3893 $post_type = 'post';
3895 if ( is_array( $post_type ) ) {
3896 if ( count( $post_type ) > 1 )
3898 $post_type = array_shift( $post_type );
3901 // Do not attempt redirect for hierarchical post types
3902 if ( is_post_type_hierarchical( $post_type ) )
3905 $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']);
3907 // if year, monthnum, or day have been specified, make our query more precise
3908 // just in case there are multiple identical _wp_old_slug values
3909 if ( '' != $wp_query->query_vars['year'] )
3910 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3911 if ( '' != $wp_query->query_vars['monthnum'] )
3912 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3913 if ( '' != $wp_query->query_vars['day'] )
3914 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3916 $id = (int) $wpdb->get_var($query);
3921 $link = get_permalink($id);
3926 wp_redirect( $link, 301 ); // Permanent redirect
3932 * Set up global post data.
3936 * @param object $post Post data.
3937 * @uses do_action_ref_array() Calls 'the_post'
3938 * @return bool True when finished.
3940 function setup_postdata( $post ) {
3941 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3943 $id = (int) $post->ID;
3945 $authordata = get_userdata($post->post_author);
3947 $currentday = mysql2date('d.m.y', $post->post_date, false);
3948 $currentmonth = mysql2date('m', $post->post_date, false);
3951 $page = get_query_var('page');
3954 if ( is_single() || is_page() || is_feed() )
3956 $content = $post->post_content;
3957 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
3960 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
3961 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
3962 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
3963 // Ignore nextpage at the beginning of the content.
3964 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
3965 $content = substr( $content, 15 );
3966 $pages = explode('<!--nextpage-->', $content);
3967 $numpages = count($pages);
3968 if ( $numpages > 1 )
3971 $pages = array( $post->post_content );
3974 do_action_ref_array('the_post', array(&$post));