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() {
118 $wp_query->reset_postdata();
126 * Is the query for an existing archive page?
128 * Month, Year, Category, Author, Post Type archive...
130 * @see WP_Query::is_archive()
136 function is_archive() {
139 if ( ! isset( $wp_query ) ) {
140 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
144 return $wp_query->is_archive();
148 * Is the query for an existing post type archive page?
150 * @see WP_Query::is_post_type_archive()
154 * @param mixed $post_types Optional. Post type or array of posts types to check against.
157 function is_post_type_archive( $post_types = '' ) {
160 if ( ! isset( $wp_query ) ) {
161 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
165 return $wp_query->is_post_type_archive( $post_types );
169 * Is the query for an existing attachment page?
171 * @see WP_Query::is_attachment()
177 function is_attachment() {
180 if ( ! isset( $wp_query ) ) {
181 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
185 return $wp_query->is_attachment();
189 * Is the query for an existing author archive page?
191 * If the $author parameter is specified, this function will additionally
192 * check if the query is for one of the authors specified.
194 * @see WP_Query::is_author()
198 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
201 function is_author( $author = '' ) {
204 if ( ! isset( $wp_query ) ) {
205 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
209 return $wp_query->is_author( $author );
213 * Is the query for an existing category archive page?
215 * If the $category parameter is specified, this function will additionally
216 * check if the query is for one of the categories specified.
218 * @see WP_Query::is_category()
222 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
225 function is_category( $category = '' ) {
228 if ( ! isset( $wp_query ) ) {
229 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
233 return $wp_query->is_category( $category );
237 * Is the query for an existing tag archive page?
239 * If the $tag parameter is specified, this function will additionally
240 * check if the query is for one of the tags specified.
242 * @see WP_Query::is_tag()
246 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
249 function is_tag( $tag = '' ) {
252 if ( ! isset( $wp_query ) ) {
253 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
257 return $wp_query->is_tag( $tag );
261 * Is the query for an existing taxonomy archive page?
263 * If the $taxonomy parameter is specified, this function will additionally
264 * check if the query is for that specific $taxonomy.
266 * If the $term parameter is specified in addition to the $taxonomy parameter,
267 * this function will additionally check if the query is for one of the terms
270 * @see WP_Query::is_tax()
274 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
275 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
278 function is_tax( $taxonomy = '', $term = '' ) {
281 if ( ! isset( $wp_query ) ) {
282 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
286 return $wp_query->is_tax( $taxonomy, $term );
290 * Whether the current URL is within the comments popup window.
292 * @see WP_Query::is_comments_popup()
298 function is_comments_popup() {
301 if ( ! isset( $wp_query ) ) {
302 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
306 return $wp_query->is_comments_popup();
310 * Is the query for an existing date archive?
312 * @see WP_Query::is_date()
321 if ( ! isset( $wp_query ) ) {
322 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
326 return $wp_query->is_date();
330 * Is the query for an existing day archive?
332 * @see WP_Query::is_day()
341 if ( ! isset( $wp_query ) ) {
342 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
346 return $wp_query->is_day();
350 * Is the query for a feed?
352 * @see WP_Query::is_feed()
356 * @param string|array $feeds Optional feed types to check.
359 function is_feed( $feeds = '' ) {
362 if ( ! isset( $wp_query ) ) {
363 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
367 return $wp_query->is_feed( $feeds );
371 * Is the query for a comments feed?
373 * @see WP_Query::is_comments_feed()
379 function is_comment_feed() {
382 if ( ! isset( $wp_query ) ) {
383 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
387 return $wp_query->is_comment_feed();
391 * Is the query for the front page of the site?
393 * This is for what is displayed at your site's main URL.
395 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
397 * If you set a static page for the front page of your site, this function will return
398 * true when viewing that page.
400 * Otherwise the same as @see is_home()
402 * @see WP_Query::is_front_page()
407 * @return bool True, if front of site.
409 function is_front_page() {
412 if ( ! isset( $wp_query ) ) {
413 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
417 return $wp_query->is_front_page();
421 * Is the query for the blog homepage?
423 * This is the page which shows the time based blog content of your site.
425 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
427 * If you set a static page for the front page of your site, this function will return
428 * true only on the page you set as the "Posts page".
430 * @see is_front_page()
432 * @see WP_Query::is_home()
436 * @return bool True if blog view homepage.
441 if ( ! isset( $wp_query ) ) {
442 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
446 return $wp_query->is_home();
450 * Is the query for an existing month archive?
452 * @see WP_Query::is_month()
458 function is_month() {
461 if ( ! isset( $wp_query ) ) {
462 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
466 return $wp_query->is_month();
470 * Is the query for an existing single page?
472 * If the $page parameter is specified, this function will additionally
473 * check if the query is for one of the pages specified.
478 * @see WP_Query::is_page()
482 * @param mixed $page Page ID, title, slug, or array of such.
485 function is_page( $page = '' ) {
488 if ( ! isset( $wp_query ) ) {
489 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
493 return $wp_query->is_page( $page );
497 * Is the query for paged result and not for the first page?
499 * @see WP_Query::is_paged()
505 function is_paged() {
508 if ( ! isset( $wp_query ) ) {
509 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
513 return $wp_query->is_paged();
517 * Is the query for a post or page preview?
519 * @see WP_Query::is_preview()
525 function is_preview() {
528 if ( ! isset( $wp_query ) ) {
529 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
533 return $wp_query->is_preview();
537 * Is the query for the robots file?
539 * @see WP_Query::is_robots()
545 function is_robots() {
548 if ( ! isset( $wp_query ) ) {
549 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
553 return $wp_query->is_robots();
557 * Is the query for a search?
559 * @see WP_Query::is_search()
565 function is_search() {
568 if ( ! isset( $wp_query ) ) {
569 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
573 return $wp_query->is_search();
577 * Is the query for an existing single post?
579 * Works for any post type, except attachments and pages
581 * If the $post parameter is specified, this function will additionally
582 * check if the query is for one of the Posts specified.
587 * @see WP_Query::is_single()
591 * @param mixed $post Post ID, title, slug, or array of such.
594 function is_single( $post = '' ) {
597 if ( ! isset( $wp_query ) ) {
598 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
602 return $wp_query->is_single( $post );
606 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
608 * If the $post_types parameter is specified, this function will additionally
609 * check if the query is for one of the Posts Types specified.
614 * @see WP_Query::is_singular()
618 * @param mixed $post_types Optional. Post Type or array of Post Types
621 function is_singular( $post_types = '' ) {
624 if ( ! isset( $wp_query ) ) {
625 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
629 return $wp_query->is_singular( $post_types );
633 * Is the query for a specific time?
635 * @see WP_Query::is_time()
644 if ( ! isset( $wp_query ) ) {
645 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
649 return $wp_query->is_time();
653 * Is the query for a trackback endpoint call?
655 * @see WP_Query::is_trackback()
661 function is_trackback() {
664 if ( ! isset( $wp_query ) ) {
665 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
669 return $wp_query->is_trackback();
673 * Is the query for an existing year archive?
675 * @see WP_Query::is_year()
684 if ( ! isset( $wp_query ) ) {
685 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
689 return $wp_query->is_year();
693 * Is the query a 404 (returns no results)?
695 * @see WP_Query::is_404()
704 if ( ! isset( $wp_query ) ) {
705 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
709 return $wp_query->is_404();
713 * Is the query the main query?
719 function is_main_query() {
720 if ( 'pre_get_posts' === current_filter() ) {
721 $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
722 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
723 _doing_it_wrong( __FUNCTION__, $message, '3.7' );
727 return $wp_query->is_main_query();
731 * The Loop. Post loop control.
735 * Whether current WordPress query has results to loop over.
737 * @see WP_Query::have_posts()
743 function have_posts() {
746 return $wp_query->have_posts();
750 * Whether the caller is in the Loop.
755 * @return bool True if caller is within loop, false if loop hasn't started or ended.
757 function in_the_loop() {
760 return $wp_query->in_the_loop;
764 * Rewind the loop posts.
766 * @see WP_Query::rewind_posts()
772 function rewind_posts() {
775 return $wp_query->rewind_posts();
779 * Iterate the post index in the loop.
781 * @see WP_Query::the_post()
785 function the_post() {
788 $wp_query->the_post();
796 * Whether there are comments to loop over.
798 * @see WP_Query::have_comments()
804 function have_comments() {
806 return $wp_query->have_comments();
810 * Iterate comment index in the comment loop.
812 * @see WP_Query::the_comment()
818 function the_comment() {
820 return $wp_query->the_comment();
828 * The WordPress Query class.
830 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
837 * Query vars set by the user
846 * Query vars, after parsing
852 var $query_vars = array();
855 * Taxonomy query, as passed to get_tax_sql()
859 * @var object WP_Tax_Query
864 * Metadata query container
868 * @var object WP_Meta_Query
870 var $meta_query = false;
873 * Date query container
877 * @var object WP_Date_Query
879 var $date_query = false;
882 * Holds the data for a single object that is queried.
884 * Holds the contents of a post, page, category, attachment.
893 * The ID of the queried object.
899 var $queried_object_id;
902 * Get post database query.
920 * The amount of posts for the current query.
929 * Index of the current item in the loop.
935 var $current_post = -1;
938 * Whether the loop has started and the caller is in the loop.
944 var $in_the_loop = false;
956 * The list of comments for current post.
965 * The amount of comments for the posts.
971 var $comment_count = 0;
974 * The index of the comment in the comment loop.
980 var $current_comment = -1;
983 * Current comment ID.
992 * The amount of found posts for the current query.
994 * If limit clause was not used, equals $post_count.
1000 var $found_posts = 0;
1003 * The amount of pages.
1009 var $max_num_pages = 0;
1012 * The amount of comment pages.
1018 var $max_num_comment_pages = 0;
1021 * Set if query is single post.
1027 var $is_single = false;
1030 * Set if query is preview of blog.
1036 var $is_preview = false;
1039 * Set if query returns a page.
1045 var $is_page = false;
1048 * Set if query is an archive list.
1054 var $is_archive = false;
1057 * Set if query is part of a date.
1063 var $is_date = false;
1066 * Set if query contains a year.
1072 var $is_year = false;
1075 * Set if query contains a month.
1081 var $is_month = false;
1084 * Set if query contains a day.
1090 var $is_day = false;
1093 * Set if query contains time.
1099 var $is_time = false;
1102 * Set if query contains an author.
1108 var $is_author = false;
1111 * Set if query contains category.
1117 var $is_category = false;
1120 * Set if query contains tag.
1126 var $is_tag = false;
1129 * Set if query contains taxonomy.
1135 var $is_tax = false;
1138 * Set if query was part of a search result.
1144 var $is_search = false;
1147 * Set if query is feed display.
1153 var $is_feed = false;
1156 * Set if query is comment feed display.
1162 var $is_comment_feed = false;
1165 * Set if query is trackback.
1171 var $is_trackback = false;
1174 * Set if query is blog homepage.
1180 var $is_home = false;
1183 * Set if query couldn't found anything.
1189 var $is_404 = false;
1192 * Set if query is within comments popup window.
1198 var $is_comments_popup = false;
1201 * Set if query is paged
1207 var $is_paged = false;
1210 * Set if query is part of administration page.
1216 var $is_admin = false;
1219 * Set if query is an attachment.
1225 var $is_attachment = false;
1228 * Set if is single, is a page, or is an attachment.
1234 var $is_singular = false;
1237 * Set if query is for robots.
1243 var $is_robots = false;
1246 * Set if query contains posts.
1248 * Basically, the homepage if the option isn't set for the static homepage.
1254 var $is_posts_page = false;
1257 * Set if query is for a post type archive.
1263 var $is_post_type_archive = false;
1266 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1267 * whether we have to re-parse because something has changed
1272 var $query_vars_hash = false;
1275 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1276 * via pre_get_posts hooks.
1281 var $query_vars_changed = true;
1284 * Set if post thumbnails are cached
1290 var $thumbnails_cached = false;
1293 * Cached list of search stopwords.
1301 * Resets query flags to false.
1303 * The query flags are what page info WordPress was able to figure out.
1308 function init_query_flags() {
1309 $this->is_single = false;
1310 $this->is_preview = false;
1311 $this->is_page = false;
1312 $this->is_archive = false;
1313 $this->is_date = false;
1314 $this->is_year = false;
1315 $this->is_month = false;
1316 $this->is_day = false;
1317 $this->is_time = false;
1318 $this->is_author = false;
1319 $this->is_category = false;
1320 $this->is_tag = false;
1321 $this->is_tax = false;
1322 $this->is_search = false;
1323 $this->is_feed = false;
1324 $this->is_comment_feed = false;
1325 $this->is_trackback = false;
1326 $this->is_home = false;
1327 $this->is_404 = false;
1328 $this->is_comments_popup = false;
1329 $this->is_paged = false;
1330 $this->is_admin = false;
1331 $this->is_attachment = false;
1332 $this->is_singular = false;
1333 $this->is_robots = false;
1334 $this->is_posts_page = false;
1335 $this->is_post_type_archive = false;
1339 * Initiates object properties and sets default values.
1345 unset($this->posts);
1346 unset($this->query);
1347 $this->query_vars = array();
1348 unset($this->queried_object);
1349 unset($this->queried_object_id);
1350 $this->post_count = 0;
1351 $this->current_post = -1;
1352 $this->in_the_loop = false;
1353 unset( $this->request );
1354 unset( $this->post );
1355 unset( $this->comments );
1356 unset( $this->comment );
1357 $this->comment_count = 0;
1358 $this->current_comment = -1;
1359 $this->found_posts = 0;
1360 $this->max_num_pages = 0;
1361 $this->max_num_comment_pages = 0;
1363 $this->init_query_flags();
1367 * Reparse the query vars.
1372 function parse_query_vars() {
1373 $this->parse_query();
1377 * Fills in the query variables, which do not exist within the parameter.
1382 * @param array $array Defined query variables.
1383 * @return array Complete query variables with undefined ones filled in empty.
1385 function fill_query_vars($array) {
1425 foreach ( $keys as $key ) {
1426 if ( !isset($array[$key]) )
1430 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1431 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
1432 'author__in', 'author__not_in' );
1434 foreach ( $array_keys as $key ) {
1435 if ( !isset($array[$key]) )
1436 $array[$key] = array();
1442 * Parse a query string and set query type booleans.
1447 * @param string|array $query Optional query.
1449 function parse_query( $query = '' ) {
1450 if ( ! empty( $query ) ) {
1452 $this->query = $this->query_vars = wp_parse_args( $query );
1453 } elseif ( ! isset( $this->query ) ) {
1454 $this->query = $this->query_vars;
1457 $this->query_vars = $this->fill_query_vars($this->query_vars);
1458 $qv = &$this->query_vars;
1459 $this->query_vars_changed = true;
1461 if ( ! empty($qv['robots']) )
1462 $this->is_robots = true;
1464 $qv['p'] = absint($qv['p']);
1465 $qv['page_id'] = absint($qv['page_id']);
1466 $qv['year'] = absint($qv['year']);
1467 $qv['monthnum'] = absint($qv['monthnum']);
1468 $qv['day'] = absint($qv['day']);
1469 $qv['w'] = absint($qv['w']);
1470 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
1471 $qv['paged'] = absint($qv['paged']);
1472 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1473 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
1474 $qv['pagename'] = trim( $qv['pagename'] );
1475 $qv['name'] = trim( $qv['name'] );
1476 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1477 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1478 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1479 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1481 // Fairly insane upper bound for search string lengths.
1482 if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
1485 // Compat. Map subpost to attachment.
1486 if ( '' != $qv['subpost'] )
1487 $qv['attachment'] = $qv['subpost'];
1488 if ( '' != $qv['subpost_id'] )
1489 $qv['attachment_id'] = $qv['subpost_id'];
1491 $qv['attachment_id'] = absint($qv['attachment_id']);
1493 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1494 $this->is_single = true;
1495 $this->is_attachment = true;
1496 } elseif ( '' != $qv['name'] ) {
1497 $this->is_single = true;
1498 } elseif ( $qv['p'] ) {
1499 $this->is_single = true;
1500 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1501 // If year, month, day, hour, minute, and second are set, a single
1502 // post is being queried.
1503 $this->is_single = true;
1504 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1505 $this->is_page = true;
1506 $this->is_single = false;
1508 // Look for archive queries. Dates, categories, authors, search, post type archives.
1510 if ( !empty($qv['s']) ) {
1511 $this->is_search = true;
1514 if ( '' !== $qv['second'] ) {
1515 $this->is_time = true;
1516 $this->is_date = true;
1519 if ( '' !== $qv['minute'] ) {
1520 $this->is_time = true;
1521 $this->is_date = true;
1524 if ( '' !== $qv['hour'] ) {
1525 $this->is_time = true;
1526 $this->is_date = true;
1530 if ( ! $this->is_date ) {
1531 $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
1532 if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
1533 $qv['error'] = '404';
1535 $this->is_day = true;
1536 $this->is_date = true;
1541 if ( $qv['monthnum'] ) {
1542 if ( ! $this->is_date ) {
1543 if ( 12 < $qv['monthnum'] ) {
1544 $qv['error'] = '404';
1546 $this->is_month = true;
1547 $this->is_date = true;
1552 if ( $qv['year'] ) {
1553 if ( ! $this->is_date ) {
1554 $this->is_year = true;
1555 $this->is_date = true;
1560 $this->is_date = true;
1561 if ( strlen($qv['m']) > 9 ) {
1562 $this->is_time = true;
1563 } else if ( strlen($qv['m']) > 7 ) {
1564 $this->is_day = true;
1565 } else if ( strlen($qv['m']) > 5 ) {
1566 $this->is_month = true;
1568 $this->is_year = true;
1572 if ( '' != $qv['w'] ) {
1573 $this->is_date = true;
1576 $this->query_vars_hash = false;
1577 $this->parse_tax_query( $qv );
1579 foreach ( $this->tax_query->queries as $tax_query ) {
1580 if ( 'NOT IN' != $tax_query['operator'] ) {
1581 switch ( $tax_query['taxonomy'] ) {
1583 $this->is_category = true;
1586 $this->is_tag = true;
1589 $this->is_tax = true;
1593 unset( $tax_query );
1595 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1596 $this->is_author = false;
1598 $this->is_author = true;
1601 if ( '' != $qv['author_name'] )
1602 $this->is_author = true;
1604 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1605 $post_type_obj = get_post_type_object( $qv['post_type'] );
1606 if ( ! empty( $post_type_obj->has_archive ) )
1607 $this->is_post_type_archive = true;
1610 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1611 $this->is_archive = true;
1614 if ( '' != $qv['feed'] )
1615 $this->is_feed = true;
1617 if ( '' != $qv['tb'] )
1618 $this->is_trackback = true;
1620 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1621 $this->is_paged = true;
1623 if ( '' != $qv['comments_popup'] )
1624 $this->is_comments_popup = true;
1626 // if we're previewing inside the write screen
1627 if ( '' != $qv['preview'] )
1628 $this->is_preview = true;
1631 $this->is_admin = true;
1633 if ( false !== strpos($qv['feed'], 'comments-') ) {
1634 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1635 $qv['withcomments'] = 1;
1638 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1640 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1641 $this->is_comment_feed = true;
1643 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 ) )
1644 $this->is_home = true;
1646 // Correct is_* for page_on_front and page_for_posts
1647 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1648 $_query = wp_parse_args($this->query);
1649 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1650 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1651 unset($_query['pagename']);
1652 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1653 $this->is_page = true;
1654 $this->is_home = false;
1655 $qv['page_id'] = get_option('page_on_front');
1656 // Correct <!--nextpage--> for page_on_front
1657 if ( !empty($qv['paged']) ) {
1658 $qv['page'] = $qv['paged'];
1659 unset($qv['paged']);
1664 if ( '' != $qv['pagename'] ) {
1665 $this->queried_object = get_page_by_path($qv['pagename']);
1666 if ( !empty($this->queried_object) )
1667 $this->queried_object_id = (int) $this->queried_object->ID;
1669 unset($this->queried_object);
1671 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1672 $this->is_page = false;
1673 $this->is_home = true;
1674 $this->is_posts_page = true;
1678 if ( $qv['page_id'] ) {
1679 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1680 $this->is_page = false;
1681 $this->is_home = true;
1682 $this->is_posts_page = true;
1686 if ( !empty($qv['post_type']) ) {
1687 if ( is_array($qv['post_type']) )
1688 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1690 $qv['post_type'] = sanitize_key($qv['post_type']);
1693 if ( ! empty( $qv['post_status'] ) ) {
1694 if ( is_array( $qv['post_status'] ) )
1695 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1697 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1700 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1701 $this->is_comment_feed = false;
1703 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1704 // Done correcting is_* for page_on_front and page_for_posts
1706 if ( '404' == $qv['error'] )
1709 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1710 $this->query_vars_changed = false;
1712 do_action_ref_array('parse_query', array(&$this));
1716 * Parses various taxonomy related query vars.
1721 * @param array &$q The query variables
1723 function parse_tax_query( &$q ) {
1724 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1725 $tax_query = $q['tax_query'];
1727 $tax_query = array();
1730 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1731 $tax_query[] = array(
1732 'taxonomy' => $q['taxonomy'],
1733 'terms' => array( $q['term'] ),
1738 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
1739 if ( 'post_tag' == $taxonomy )
1740 continue; // Handled further down in the $q['tag'] block
1742 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1743 $tax_query_defaults = array(
1744 'taxonomy' => $taxonomy,
1748 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1749 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1752 $term = $q[$t->query_var];
1754 if ( strpos($term, '+') !== false ) {
1755 $terms = preg_split( '/[+]+/', $term );
1756 foreach ( $terms as $term ) {
1757 $tax_query[] = array_merge( $tax_query_defaults, array(
1758 'terms' => array( $term )
1762 $tax_query[] = array_merge( $tax_query_defaults, array(
1763 'terms' => preg_split( '/[,]+/', $term )
1770 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
1771 $cat_in = $cat_not_in = array();
1773 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
1774 $cat_array = array_map( 'intval', $cat_array );
1775 $q['cat'] = implode( ',', $cat_array );
1777 foreach ( $cat_array as $cat ) {
1781 $cat_not_in[] = abs( $cat );
1784 if ( ! empty( $cat_in ) ) {
1785 $tax_query[] = array(
1786 'taxonomy' => 'category',
1788 'field' => 'term_id',
1789 'include_children' => true
1793 if ( ! empty( $cat_not_in ) ) {
1794 $tax_query[] = array(
1795 'taxonomy' => 'category',
1796 'terms' => $cat_not_in,
1797 'field' => 'term_id',
1798 'operator' => 'NOT IN',
1799 'include_children' => true
1802 unset( $cat_array, $cat_in, $cat_not_in );
1805 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1806 $q['category__and'] = (array) $q['category__and'];
1807 if ( ! isset( $q['category__in'] ) )
1808 $q['category__in'] = array();
1809 $q['category__in'][] = absint( reset( $q['category__and'] ) );
1810 unset( $q['category__and'] );
1813 if ( ! empty( $q['category__in'] ) ) {
1814 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1815 $tax_query[] = array(
1816 'taxonomy' => 'category',
1817 'terms' => $q['category__in'],
1818 'field' => 'term_id',
1819 'include_children' => false
1823 if ( ! empty($q['category__not_in']) ) {
1824 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1825 $tax_query[] = array(
1826 'taxonomy' => 'category',
1827 'terms' => $q['category__not_in'],
1828 'operator' => 'NOT IN',
1829 'include_children' => false
1833 if ( ! empty($q['category__and']) ) {
1834 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1835 $tax_query[] = array(
1836 'taxonomy' => 'category',
1837 'terms' => $q['category__and'],
1838 'field' => 'term_id',
1839 'operator' => 'AND',
1840 'include_children' => false
1845 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1846 if ( strpos($q['tag'], ',') !== false ) {
1847 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1848 foreach ( (array) $tags as $tag ) {
1849 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1850 $q['tag_slug__in'][] = $tag;
1852 } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1853 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1854 foreach ( (array) $tags as $tag ) {
1855 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1856 $q['tag_slug__and'][] = $tag;
1859 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1860 $q['tag_slug__in'][] = $q['tag'];
1864 if ( !empty($q['tag_id']) ) {
1865 $q['tag_id'] = absint( $q['tag_id'] );
1866 $tax_query[] = array(
1867 'taxonomy' => 'post_tag',
1868 'terms' => $q['tag_id']
1872 if ( !empty($q['tag__in']) ) {
1873 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1874 $tax_query[] = array(
1875 'taxonomy' => 'post_tag',
1876 'terms' => $q['tag__in']
1880 if ( !empty($q['tag__not_in']) ) {
1881 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1882 $tax_query[] = array(
1883 'taxonomy' => 'post_tag',
1884 'terms' => $q['tag__not_in'],
1885 'operator' => 'NOT IN'
1889 if ( !empty($q['tag__and']) ) {
1890 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1891 $tax_query[] = array(
1892 'taxonomy' => 'post_tag',
1893 'terms' => $q['tag__and'],
1898 if ( !empty($q['tag_slug__in']) ) {
1899 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1900 $tax_query[] = array(
1901 'taxonomy' => 'post_tag',
1902 'terms' => $q['tag_slug__in'],
1907 if ( !empty($q['tag_slug__and']) ) {
1908 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1909 $tax_query[] = array(
1910 'taxonomy' => 'post_tag',
1911 'terms' => $q['tag_slug__and'],
1917 $this->tax_query = new WP_Tax_Query( $tax_query );
1919 do_action( 'parse_tax_query', $this );
1923 * Generate SQL for the WHERE clause based on passed search terms.
1927 * @global wpdb $wpdb
1928 * @param array $q Query variables.
1929 * @return string WHERE clause.
1931 protected function parse_search( &$q ) {
1936 // added slashes screw with quote grouping when done early, so done later
1937 $q['s'] = stripslashes( $q['s'] );
1938 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1939 $q['s'] = urldecode( $q['s'] );
1940 // there are no line breaks in <input /> fields
1941 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1942 $q['search_terms_count'] = 1;
1943 if ( ! empty( $q['sentence'] ) ) {
1944 $q['search_terms'] = array( $q['s'] );
1946 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1947 $q['search_terms_count'] = count( $matches[0] );
1948 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1949 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1950 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1951 $q['search_terms'] = array( $q['s'] );
1953 $q['search_terms'] = array( $q['s'] );
1957 $n = ! empty( $q['exact'] ) ? '' : '%';
1959 $q['search_orderby_title'] = array();
1960 foreach ( $q['search_terms'] as $term ) {
1961 $term = like_escape( esc_sql( $term ) );
1963 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1965 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1966 $searchand = ' AND ';
1969 if ( ! empty( $search ) ) {
1970 $search = " AND ({$search}) ";
1971 if ( ! is_user_logged_in() )
1972 $search .= " AND ($wpdb->posts.post_password = '') ";
1979 * Check if the terms are suitable for searching.
1981 * Uses an array of stopwords (terms) that are excluded from the separate
1982 * term matching when searching for posts. The list of English stopwords is
1983 * the approximate search engines list, and is translatable.
1987 * @param array Terms to check.
1988 * @return array Terms that are not stopwords.
1990 protected function parse_search_terms( $terms ) {
1991 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
1994 $stopwords = $this->get_search_stopwords();
1996 foreach ( $terms as $term ) {
1997 // keep before/after spaces when term is for exact match
1998 if ( preg_match( '/^".+"$/', $term ) )
1999 $term = trim( $term, "\"'" );
2001 $term = trim( $term, "\"' " );
2003 // Avoid single A-Z.
2004 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
2007 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2017 * Retrieve stopwords used when parsing search terms.
2021 * @return array Stopwords.
2023 protected function get_search_stopwords() {
2024 if ( isset( $this->stopwords ) )
2025 return $this->stopwords;
2027 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2028 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2029 * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2031 $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',
2032 'Comma-separated list of search stopwords in your language' ) );
2034 $stopwords = array();
2035 foreach( $words as $word ) {
2036 $word = trim( $word, "\r\n\t " );
2038 $stopwords[] = $word;
2042 * Filter stopwords used when parsing search terms.
2046 * @param array $stopwords Stopwords.
2048 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2049 return $this->stopwords;
2053 * Generate SQL for the ORDER BY condition based on passed search terms.
2055 * @global wpdb $wpdb
2056 * @param array $q Query variables.
2057 * @return string ORDER BY clause.
2059 protected function parse_search_order( &$q ) {
2062 $search_orderby = '';
2064 if ( $q['search_terms_count'] > 1 ) {
2065 $num_terms = count( $q['search_orderby_title'] );
2066 $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2068 $search_orderby = '(CASE ';
2069 // sentence match in 'post_title'
2070 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2072 // sanity limit, sort as sentence when more than 6 terms
2073 // (few searches are longer than 6 terms and most titles are not)
2074 if ( $num_terms < 7 ) {
2075 // all words in title
2076 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2077 // any word in title, not needed when $num_terms == 1
2078 if ( $num_terms > 1 )
2079 $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2082 // sentence match in 'post_content'
2083 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2084 $search_orderby .= 'ELSE 5 END)';
2086 // single word or sentence search
2087 $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2090 return $search_orderby;
2094 * Sets the 404 property and saves whether query is feed.
2099 function set_404() {
2100 $is_feed = $this->is_feed;
2102 $this->init_query_flags();
2103 $this->is_404 = true;
2105 $this->is_feed = $is_feed;
2109 * Retrieve query variable.
2114 * @param string $query_var Query variable key.
2117 function get($query_var) {
2118 if ( isset($this->query_vars[$query_var]) )
2119 return $this->query_vars[$query_var];
2125 * Set query variable.
2130 * @param string $query_var Query variable key.
2131 * @param mixed $value Query variable value.
2133 function set($query_var, $value) {
2134 $this->query_vars[$query_var] = $value;
2138 * Retrieve the posts based on query variables.
2140 * There are a few filters and actions that can be used to modify the post
2145 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2147 * @return array List of posts.
2149 function get_posts() {
2152 $this->parse_query();
2154 do_action_ref_array('pre_get_posts', array(&$this));
2157 $q = &$this->query_vars;
2159 // Fill again in case pre_get_posts unset some vars.
2160 $q = $this->fill_query_vars($q);
2163 $this->meta_query = new WP_Meta_Query();
2164 $this->meta_query->parse_query_vars( $q );
2166 // Set a flag if a pre_get_posts hook changed the query vars.
2167 $hash = md5( serialize( $this->query_vars ) );
2168 if ( $hash != $this->query_vars_hash ) {
2169 $this->query_vars_changed = true;
2170 $this->query_vars_hash = $hash;
2174 // First let's clear some variables
2177 $whichmimetype = '';
2184 $post_status_join = false;
2187 if ( isset( $q['caller_get_posts'] ) ) {
2188 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2189 if ( !isset( $q['ignore_sticky_posts'] ) )
2190 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2193 if ( !isset( $q['ignore_sticky_posts'] ) )
2194 $q['ignore_sticky_posts'] = false;
2196 if ( !isset($q['suppress_filters']) )
2197 $q['suppress_filters'] = false;
2199 if ( !isset($q['cache_results']) ) {
2200 if ( wp_using_ext_object_cache() )
2201 $q['cache_results'] = false;
2203 $q['cache_results'] = true;
2206 if ( !isset($q['update_post_term_cache']) )
2207 $q['update_post_term_cache'] = true;
2209 if ( !isset($q['update_post_meta_cache']) )
2210 $q['update_post_meta_cache'] = true;
2212 if ( !isset($q['post_type']) ) {
2213 if ( $this->is_search )
2214 $q['post_type'] = 'any';
2216 $q['post_type'] = '';
2218 $post_type = $q['post_type'];
2219 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
2220 $q['posts_per_page'] = get_option('posts_per_page');
2221 if ( isset($q['showposts']) && $q['showposts'] ) {
2222 $q['showposts'] = (int) $q['showposts'];
2223 $q['posts_per_page'] = $q['showposts'];
2225 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2226 $q['posts_per_page'] = $q['posts_per_archive_page'];
2227 if ( !isset($q['nopaging']) ) {
2228 if ( $q['posts_per_page'] == -1 ) {
2229 $q['nopaging'] = true;
2231 $q['nopaging'] = false;
2234 if ( $this->is_feed ) {
2235 $q['posts_per_page'] = get_option('posts_per_rss');
2236 $q['nopaging'] = false;
2238 $q['posts_per_page'] = (int) $q['posts_per_page'];
2239 if ( $q['posts_per_page'] < -1 )
2240 $q['posts_per_page'] = abs($q['posts_per_page']);
2241 else if ( $q['posts_per_page'] == 0 )
2242 $q['posts_per_page'] = 1;
2244 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2245 $q['comments_per_page'] = get_option('comments_per_page');
2247 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2248 $this->is_page = true;
2249 $this->is_home = false;
2250 $q['page_id'] = get_option('page_on_front');
2253 if ( isset($q['page']) ) {
2254 $q['page'] = trim($q['page'], '/');
2255 $q['page'] = absint($q['page']);
2258 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2259 if ( isset($q['no_found_rows']) )
2260 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2262 $q['no_found_rows'] = false;
2264 switch ( $q['fields'] ) {
2266 $fields = "$wpdb->posts.ID";
2269 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2272 $fields = "$wpdb->posts.*";
2275 if ( '' !== $q['menu_order'] )
2276 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2278 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2280 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2281 if ( strlen($q['m']) > 5 )
2282 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2283 if ( strlen($q['m']) > 7 )
2284 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2285 if ( strlen($q['m']) > 9 )
2286 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2287 if ( strlen($q['m']) > 11 )
2288 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2289 if ( strlen($q['m']) > 13 )
2290 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2293 // Handle the other individual date parameters
2294 $date_parameters = array();
2296 if ( '' !== $q['hour'] )
2297 $date_parameters['hour'] = $q['hour'];
2299 if ( '' !== $q['minute'] )
2300 $date_parameters['minute'] = $q['minute'];
2302 if ( '' !== $q['second'] )
2303 $date_parameters['second'] = $q['second'];
2306 $date_parameters['year'] = $q['year'];
2308 if ( $q['monthnum'] )
2309 $date_parameters['monthnum'] = $q['monthnum'];
2312 $date_parameters['week'] = $q['w'];
2315 $date_parameters['day'] = $q['day'];
2317 if ( $date_parameters ) {
2318 $date_query = new WP_Date_Query( array( $date_parameters ) );
2319 $where .= $date_query->get_sql();
2321 unset( $date_parameters, $date_query );
2323 // Handle complex date queries
2324 if ( ! empty( $q['date_query'] ) ) {
2325 $this->date_query = new WP_Date_Query( $q['date_query'] );
2326 $where .= $this->date_query->get_sql();
2330 // If we've got a post_type AND it's not "any" post_type.
2331 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2332 foreach ( (array)$q['post_type'] as $_post_type ) {
2333 $ptype_obj = get_post_type_object($_post_type);
2334 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2337 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2338 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2339 $q['name'] = $q[ $ptype_obj->query_var ];
2341 // Hierarchical post_types will operate through the
2342 $q['pagename'] = $q[ $ptype_obj->query_var ];
2346 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2352 if ( '' != $q['name'] ) {
2353 $q['name'] = sanitize_title_for_query( $q['name'] );
2354 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2355 } elseif ( '' != $q['pagename'] ) {
2356 if ( isset($this->queried_object_id) ) {
2357 $reqpage = $this->queried_object_id;
2359 if ( 'page' != $q['post_type'] ) {
2360 foreach ( (array)$q['post_type'] as $_post_type ) {
2361 $ptype_obj = get_post_type_object($_post_type);
2362 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2365 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2371 $reqpage = get_page_by_path($q['pagename']);
2373 if ( !empty($reqpage) )
2374 $reqpage = $reqpage->ID;
2379 $page_for_posts = get_option('page_for_posts');
2380 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2381 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2382 $q['name'] = $q['pagename'];
2383 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2384 $reqpage_obj = get_post( $reqpage );
2385 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2386 $this->is_attachment = true;
2387 $post_type = $q['post_type'] = 'attachment';
2388 $this->is_page = true;
2389 $q['attachment_id'] = $reqpage;
2392 } elseif ( '' != $q['attachment'] ) {
2393 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2394 $q['name'] = $q['attachment'];
2395 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2399 if ( intval($q['comments_popup']) )
2400 $q['p'] = absint($q['comments_popup']);
2402 // If an attachment is requested by number, let it supersede any post number.
2403 if ( $q['attachment_id'] )
2404 $q['p'] = absint($q['attachment_id']);
2406 // If a post number is specified, load that post
2408 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2409 } elseif ( $q['post__in'] ) {
2410 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2411 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2412 } elseif ( $q['post__not_in'] ) {
2413 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2414 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2417 if ( is_numeric( $q['post_parent'] ) ) {
2418 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2419 } elseif ( $q['post_parent__in'] ) {
2420 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2421 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2422 } elseif ( $q['post_parent__not_in'] ) {
2423 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2424 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2427 if ( $q['page_id'] ) {
2428 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2429 $q['p'] = $q['page_id'];
2430 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2434 // If a search pattern is specified, load the posts that match.
2435 if ( ! empty( $q['s'] ) )
2436 $search = $this->parse_search( $q );
2439 * Filter the search SQL that is used in the WHERE clause of WP_Query.
2443 * @param string $search Search SQL for WHERE clause.
2444 * @param WP_Query $this The current WP_Query object.
2446 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2449 if ( !$this->is_singular ) {
2450 $this->parse_tax_query( $q );
2452 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2454 $join .= $clauses['join'];
2455 $where .= $clauses['where'];
2458 if ( $this->is_tax ) {
2459 if ( empty($post_type) ) {
2460 // Do a fully inclusive search for currently registered post types of queried taxonomies
2461 $post_type = array();
2462 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2463 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2464 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2465 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2470 elseif ( count( $post_type ) == 1 )
2471 $post_type = $post_type[0];
2473 $post_status_join = true;
2474 } elseif ( in_array('attachment', (array) $post_type) ) {
2475 $post_status_join = true;
2480 if ( !empty($this->tax_query->queries) ) {
2481 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2482 if ( !empty( $tax_query_in_and ) ) {
2483 if ( !isset( $q['taxonomy'] ) ) {
2484 foreach ( $tax_query_in_and as $a_tax_query ) {
2485 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2486 $q['taxonomy'] = $a_tax_query['taxonomy'];
2487 if ( 'slug' == $a_tax_query['field'] )
2488 $q['term'] = $a_tax_query['terms'][0];
2490 $q['term_id'] = $a_tax_query['terms'][0];
2497 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2498 if ( ! empty( $cat_query ) ) {
2499 $cat_query = reset( $cat_query );
2501 if ( ! empty( $cat_query['terms'][0] ) ) {
2502 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2504 $this->set( 'cat', $the_cat->term_id );
2505 $this->set( 'category_name', $the_cat->slug );
2510 unset( $cat_query );
2512 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2513 if ( ! empty( $tag_query ) ) {
2514 $tag_query = reset( $tag_query );
2516 if ( ! empty( $tag_query['terms'][0] ) ) {
2517 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2519 $this->set( 'tag_id', $the_tag->term_id );
2523 unset( $tag_query );
2527 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2528 $groupby = "{$wpdb->posts}.ID";
2531 // Author/user stuff
2533 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2534 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2535 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2536 foreach ( $authors as $author ) {
2537 $key = $author > 0 ? 'author__in' : 'author__not_in';
2538 $q[$key][] = abs( $author );
2540 $q['author'] = implode( ',', $authors );
2543 if ( ! empty( $q['author__not_in'] ) ) {
2544 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2545 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2546 } elseif ( ! empty( $q['author__in'] ) ) {
2547 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2548 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2551 // Author stuff for nice URLs
2553 if ( '' != $q['author_name'] ) {
2554 if ( strpos($q['author_name'], '/') !== false ) {
2555 $q['author_name'] = explode('/', $q['author_name']);
2556 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2557 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2559 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2562 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2563 $q['author'] = get_user_by('slug', $q['author_name']);
2565 $q['author'] = $q['author']->ID;
2566 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2569 // MIME-Type stuff for attachment browsing
2571 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2572 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2574 $where .= $search . $whichauthor . $whichmimetype;
2576 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2577 $q['order'] = 'DESC';
2580 if ( empty($q['orderby']) ) {
2581 $orderby = "$wpdb->posts.post_date " . $q['order'];
2582 } elseif ( 'none' == $q['orderby'] ) {
2584 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2585 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2586 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2587 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2589 // Used to filter values
2590 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2591 if ( !empty($q['meta_key']) ) {
2592 $allowed_keys[] = $q['meta_key'];
2593 $allowed_keys[] = 'meta_value';
2594 $allowed_keys[] = 'meta_value_num';
2596 $q['orderby'] = urldecode($q['orderby']);
2597 $q['orderby'] = addslashes_gpc($q['orderby']);
2599 $orderby_array = array();
2600 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2601 // Only allow certain values for safety
2602 if ( ! in_array($orderby, $allowed_keys) )
2605 switch ( $orderby ) {
2607 $orderby = "$wpdb->posts.menu_order";
2610 $orderby = "$wpdb->posts.ID";
2613 $orderby = 'RAND()';
2615 case $q['meta_key']:
2617 if ( isset( $q['meta_type'] ) ) {
2618 $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2619 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2621 $orderby = "$wpdb->postmeta.meta_value";
2624 case 'meta_value_num':
2625 $orderby = "$wpdb->postmeta.meta_value+0";
2627 case 'comment_count':
2628 $orderby = "$wpdb->posts.comment_count";
2631 $orderby = "$wpdb->posts.post_" . $orderby;
2634 $orderby_array[] = $orderby;
2636 $orderby = implode( ',', $orderby_array );
2638 if ( empty( $orderby ) )
2639 $orderby = "$wpdb->posts.post_date ".$q['order'];
2641 $orderby .= " {$q['order']}";
2644 // Order search results by relevance only when another "orderby" is not specified in the query.
2645 if ( ! empty( $q['s'] ) ) {
2646 $search_orderby = '';
2647 if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2648 $search_orderby = $this->parse_search_order( $q );
2651 * Filter the ORDER BY used when ordering search results.
2655 * @param string $search_orderby The ORDER BY clause.
2656 * @param WP_Query $this The current WP_Query instance.
2658 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2659 if ( $search_orderby )
2660 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2663 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2664 $post_type_cap = 'multiple_post_type';
2666 if ( is_array( $post_type ) )
2667 $post_type = reset( $post_type );
2668 $post_type_object = get_post_type_object( $post_type );
2669 if ( empty( $post_type_object ) )
2670 $post_type_cap = $post_type;
2673 if ( 'any' == $post_type ) {
2674 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2675 if ( empty( $in_search_post_types ) )
2676 $where .= ' AND 1=0 ';
2678 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2679 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2680 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2681 } elseif ( ! empty( $post_type ) ) {
2682 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2683 $post_type_object = get_post_type_object ( $post_type );
2684 } elseif ( $this->is_attachment ) {
2685 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2686 $post_type_object = get_post_type_object ( 'attachment' );
2687 } elseif ( $this->is_page ) {
2688 $where .= " AND $wpdb->posts.post_type = 'page'";
2689 $post_type_object = get_post_type_object ( 'page' );
2691 $where .= " AND $wpdb->posts.post_type = 'post'";
2692 $post_type_object = get_post_type_object ( 'post' );
2695 $edit_cap = 'edit_post';
2696 $read_cap = 'read_post';
2698 if ( ! empty( $post_type_object ) ) {
2699 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2700 $read_private_cap = $post_type_object->cap->read_private_posts;
2702 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2703 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2706 $user_id = get_current_user_id();
2708 if ( ! empty( $q['post_status'] ) ) {
2709 $statuswheres = array();
2710 $q_status = $q['post_status'];
2711 if ( ! is_array( $q_status ) )
2712 $q_status = explode(',', $q_status);
2713 $r_status = array();
2714 $p_status = array();
2715 $e_status = array();
2716 if ( in_array('any', $q_status) ) {
2717 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2718 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2720 foreach ( get_post_stati() as $status ) {
2721 if ( in_array( $status, $q_status ) ) {
2722 if ( 'private' == $status )
2723 $p_status[] = "$wpdb->posts.post_status = '$status'";
2725 $r_status[] = "$wpdb->posts.post_status = '$status'";
2730 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2731 $r_status = array_merge($r_status, $p_status);
2735 if ( !empty($e_status) ) {
2736 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2738 if ( !empty($r_status) ) {
2739 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2740 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2742 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2744 if ( !empty($p_status) ) {
2745 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2746 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2748 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2750 if ( $post_status_join ) {
2751 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2752 foreach ( $statuswheres as $index => $statuswhere )
2753 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2755 foreach ( $statuswheres as $statuswhere )
2756 $where .= " AND $statuswhere";
2757 } elseif ( !$this->is_singular ) {
2758 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2760 // Add public states.
2761 $public_states = get_post_stati( array('public' => true) );
2762 foreach ( (array) $public_states as $state ) {
2763 if ( 'publish' == $state ) // Publish is hard-coded above.
2765 $where .= " OR $wpdb->posts.post_status = '$state'";
2768 if ( $this->is_admin ) {
2769 // Add protected states that should show in the admin all list.
2770 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2771 foreach ( (array) $admin_all_states as $state )
2772 $where .= " OR $wpdb->posts.post_status = '$state'";
2775 if ( is_user_logged_in() ) {
2776 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2777 $private_states = get_post_stati( array('private' => true) );
2778 foreach ( (array) $private_states as $state )
2779 $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'";
2785 if ( !empty( $this->meta_query->queries ) ) {
2786 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2787 $join .= $clauses['join'];
2788 $where .= $clauses['where'];
2791 // Apply filters on where and join prior to paging so that any
2792 // manipulations to them are reflected in the paging by day queries.
2793 if ( !$q['suppress_filters'] ) {
2794 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2795 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2799 if ( empty($q['nopaging']) && !$this->is_singular ) {
2800 $page = absint($q['paged']);
2804 if ( empty($q['offset']) ) {
2805 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2806 } else { // we're ignoring $page and using 'offset'
2807 $q['offset'] = absint($q['offset']);
2808 $pgstrt = $q['offset'] . ', ';
2810 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2814 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2815 if ( $this->is_archive || $this->is_search ) {
2816 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2817 $cwhere = "WHERE comment_approved = '1' $where";
2818 $cgroupby = "$wpdb->comments.comment_id";
2819 } else { // Other non singular e.g. front
2820 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2821 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2825 if ( !$q['suppress_filters'] ) {
2826 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2827 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2828 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2829 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2830 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2832 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2833 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2835 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2836 $this->comment_count = count($this->comments);
2838 $post_ids = array();
2840 foreach ( $this->comments as $comment )
2841 $post_ids[] = (int) $comment->comment_post_ID;
2843 $post_ids = join(',', $post_ids);
2846 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2851 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2853 // Apply post-paging filters on where and join. Only plugins that
2854 // manipulate paging queries should use these hooks.
2855 if ( !$q['suppress_filters'] ) {
2856 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2857 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2858 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2859 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2860 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2861 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2862 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2864 // Filter all clauses at once, for convenience
2865 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2866 foreach ( $pieces as $piece )
2867 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2870 // Announce current selection parameters. For use by caching plugins.
2871 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2873 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2874 if ( !$q['suppress_filters'] ) {
2875 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2876 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2877 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2878 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2879 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2880 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2881 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2883 // Filter all clauses at once, for convenience
2884 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2885 foreach ( $pieces as $piece )
2886 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2889 if ( ! empty($groupby) )
2890 $groupby = 'GROUP BY ' . $groupby;
2891 if ( !empty( $orderby ) )
2892 $orderby = 'ORDER BY ' . $orderby;
2895 if ( !$q['no_found_rows'] && !empty($limits) )
2896 $found_rows = 'SQL_CALC_FOUND_ROWS';
2898 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2900 if ( !$q['suppress_filters'] ) {
2901 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2904 if ( 'ids' == $q['fields'] ) {
2905 $this->posts = $wpdb->get_col( $this->request );
2906 $this->post_count = count( $this->posts );
2907 $this->set_found_posts( $q, $limits );
2909 return $this->posts;
2912 if ( 'id=>parent' == $q['fields'] ) {
2913 $this->posts = $wpdb->get_results( $this->request );
2914 $this->post_count = count( $this->posts );
2915 $this->set_found_posts( $q, $limits );
2918 foreach ( $this->posts as $post )
2919 $r[ $post->ID ] = $post->post_parent;
2924 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2925 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2927 if ( $split_the_query ) {
2928 // First get the IDs and then fill in the objects
2930 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2932 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2934 $ids = $wpdb->get_col( $this->request );
2937 $this->posts = $ids;
2938 $this->set_found_posts( $q, $limits );
2939 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2941 $this->posts = array();
2944 $this->posts = $wpdb->get_results( $this->request );
2945 $this->set_found_posts( $q, $limits );
2948 // Convert to WP_Post objects
2950 $this->posts = array_map( 'get_post', $this->posts );
2952 // Raw results filter. Prior to status checks.
2953 if ( !$q['suppress_filters'] )
2954 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2956 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2957 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2958 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2959 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2960 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2961 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2962 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2963 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2964 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2965 $this->comments = $wpdb->get_results($comments_request);
2966 $this->comment_count = count($this->comments);
2969 // Check post status to determine if post should be displayed.
2970 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2971 $status = get_post_status($this->posts[0]);
2972 $post_status_obj = get_post_status_object($status);
2973 //$type = get_post_type($this->posts[0]);
2974 if ( !$post_status_obj->public ) {
2975 if ( ! is_user_logged_in() ) {
2976 // User must be logged in to view unpublished posts.
2977 $this->posts = array();
2979 if ( $post_status_obj->protected ) {
2980 // User must have edit permissions on the draft to preview.
2981 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2982 $this->posts = array();
2984 $this->is_preview = true;
2985 if ( 'future' != $status )
2986 $this->posts[0]->post_date = current_time('mysql');
2988 } elseif ( $post_status_obj->private ) {
2989 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2990 $this->posts = array();
2992 $this->posts = array();
2997 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2998 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
3001 // Put sticky posts at the top of the posts array
3002 $sticky_posts = get_option('sticky_posts');
3003 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
3004 $num_posts = count($this->posts);
3006 // Loop over posts and relocate stickies to the front.
3007 for ( $i = 0; $i < $num_posts; $i++ ) {
3008 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
3009 $sticky_post = $this->posts[$i];
3010 // Remove sticky from current position
3011 array_splice($this->posts, $i, 1);
3012 // Move to front, after other stickies
3013 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
3014 // Increment the sticky offset. The next sticky will be placed at this offset.
3016 // Remove post from sticky posts array
3017 $offset = array_search($sticky_post->ID, $sticky_posts);
3018 unset( $sticky_posts[$offset] );
3022 // If any posts have been excluded specifically, Ignore those that are sticky.
3023 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3024 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3026 // Fetch sticky posts that weren't in the query results
3027 if ( !empty($sticky_posts) ) {
3028 $stickies = get_posts( array(
3029 'post__in' => $sticky_posts,
3030 'post_type' => $post_type,
3031 'post_status' => 'publish',
3035 foreach ( $stickies as $sticky_post ) {
3036 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3042 if ( !$q['suppress_filters'] )
3043 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
3045 // Ensure that any posts added/modified via one of the filters above are
3046 // of the type WP_Post and are filtered.
3047 if ( $this->posts ) {
3048 $this->post_count = count( $this->posts );
3050 $this->posts = array_map( 'get_post', $this->posts );
3052 if ( $q['cache_results'] )
3053 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3055 $this->post = reset( $this->posts );
3057 $this->post_count = 0;
3058 $this->posts = array();
3061 return $this->posts;
3065 * Set up the amount of found posts and the number of pages (if limit clause was used)
3066 * for the current query.
3071 function set_found_posts( $q, $limits ) {
3074 // Bail if posts is an empty array. Continue if posts is an empty string,
3075 // null, or false to accommodate caching plugins that fill posts later.
3076 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3079 if ( ! empty( $limits ) )
3080 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3082 $this->found_posts = count( $this->posts );
3084 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3086 if ( ! empty( $limits ) )
3087 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3091 * Set up the next post and iterate current post index.
3096 * @return WP_Post Next post.
3098 function next_post() {
3100 $this->current_post++;
3102 $this->post = $this->posts[$this->current_post];
3107 * Sets up the current post.
3109 * Retrieves the next post, sets up the post, sets the 'in the loop'
3115 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3117 function the_post() {
3119 $this->in_the_loop = true;
3121 if ( $this->current_post == -1 ) // loop has just started
3122 do_action_ref_array('loop_start', array(&$this));
3124 $post = $this->next_post();
3125 setup_postdata($post);
3129 * Whether there are more posts available in the loop.
3131 * Calls action 'loop_end', when the loop is complete.
3135 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3137 * @return bool True if posts are available, false if end of loop.
3139 function have_posts() {
3140 if ( $this->current_post + 1 < $this->post_count ) {
3142 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3143 do_action_ref_array('loop_end', array(&$this));
3144 // Do some cleaning up after the loop
3145 $this->rewind_posts();
3148 $this->in_the_loop = false;
3153 * Rewind the posts and reset post index.
3158 function rewind_posts() {
3159 $this->current_post = -1;
3160 if ( $this->post_count > 0 ) {
3161 $this->post = $this->posts[0];
3166 * Iterate current comment index and return comment object.
3171 * @return object Comment object.
3173 function next_comment() {
3174 $this->current_comment++;
3176 $this->comment = $this->comments[$this->current_comment];
3177 return $this->comment;
3181 * Sets up the current comment.
3185 * @global object $comment Current comment.
3186 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3188 function the_comment() {
3191 $comment = $this->next_comment();
3193 if ( $this->current_comment == 0 ) {
3194 do_action('comment_loop_start');
3199 * Whether there are more comments available.
3201 * Automatically rewinds comments when finished.
3206 * @return bool True, if more comments. False, if no more posts.
3208 function have_comments() {
3209 if ( $this->current_comment + 1 < $this->comment_count ) {
3211 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3212 $this->rewind_comments();
3219 * Rewind the comments, resets the comment index and comment to first.
3224 function rewind_comments() {
3225 $this->current_comment = -1;
3226 if ( $this->comment_count > 0 ) {
3227 $this->comment = $this->comments[0];
3232 * Sets up the WordPress query by parsing query string.
3237 * @param string $query URL query string.
3238 * @return array List of posts.
3240 function query( $query ) {
3242 $this->query = $this->query_vars = wp_parse_args( $query );
3243 return $this->get_posts();
3247 * Retrieve queried object.
3249 * If queried object is not set, then the queried object will be set from
3250 * the category, tag, taxonomy, posts page, single post, page, or author
3251 * query variable. After it is set up, it will be returned.
3258 function get_queried_object() {
3259 if ( isset($this->queried_object) )
3260 return $this->queried_object;
3262 $this->queried_object = null;
3263 $this->queried_object_id = 0;
3265 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3266 if ( $this->is_category ) {
3267 $term = get_term( $this->get( 'cat' ), 'category' );
3268 } elseif ( $this->is_tag ) {
3269 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
3271 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3272 $query = reset( $tax_query_in_and );
3274 if ( 'term_id' == $query['field'] )
3275 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3277 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3280 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3281 $this->queried_object = $term;
3282 $this->queried_object_id = (int) $term->term_id;
3284 if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
3285 _make_cat_compat( $this->queried_object );
3287 } elseif ( $this->is_post_type_archive ) {
3288 $post_type = $this->get( 'post_type' );
3289 if ( is_array( $post_type ) )
3290 $post_type = reset( $post_type );
3291 $this->queried_object = get_post_type_object( $post_type );
3292 } elseif ( $this->is_posts_page ) {
3293 $page_for_posts = get_option('page_for_posts');
3294 $this->queried_object = get_post( $page_for_posts );
3295 $this->queried_object_id = (int) $this->queried_object->ID;
3296 } elseif ( $this->is_singular && !is_null($this->post) ) {
3297 $this->queried_object = $this->post;
3298 $this->queried_object_id = (int) $this->post->ID;
3299 } elseif ( $this->is_author ) {
3300 $this->queried_object_id = (int) $this->get('author');
3301 $this->queried_object = get_userdata( $this->queried_object_id );
3304 return $this->queried_object;
3308 * Retrieve ID of the current queried object.
3315 function get_queried_object_id() {
3316 $this->get_queried_object();
3318 if ( isset($this->queried_object_id) ) {
3319 return $this->queried_object_id;
3328 * Sets up the WordPress query, if parameter is not empty.
3333 * @param string $query URL query string.
3336 function __construct($query = '') {
3337 if ( ! empty($query) ) {
3338 $this->query($query);
3343 * Is the query for an existing archive page?
3345 * Month, Year, Category, Author, Post Type archive...
3351 function is_archive() {
3352 return (bool) $this->is_archive;
3356 * Is the query for an existing post type archive page?
3360 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3363 function is_post_type_archive( $post_types = '' ) {
3364 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3365 return (bool) $this->is_post_type_archive;
3367 $post_type = $this->get( 'post_type' );
3368 if ( is_array( $post_type ) )
3369 $post_type = reset( $post_type );
3370 $post_type_object = get_post_type_object( $post_type );
3372 return in_array( $post_type_object->name, (array) $post_types );
3376 * Is the query for an existing attachment page?
3382 function is_attachment() {
3383 return (bool) $this->is_attachment;
3387 * Is the query for an existing author archive page?
3389 * If the $author parameter is specified, this function will additionally
3390 * check if the query is for one of the authors specified.
3394 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3397 function is_author( $author = '' ) {
3398 if ( !$this->is_author )
3401 if ( empty($author) )
3404 $author_obj = $this->get_queried_object();
3406 $author = (array) $author;
3408 if ( in_array( $author_obj->ID, $author ) )
3410 elseif ( in_array( $author_obj->nickname, $author ) )
3412 elseif ( in_array( $author_obj->user_nicename, $author ) )
3419 * Is the query for an existing category archive page?
3421 * If the $category parameter is specified, this function will additionally
3422 * check if the query is for one of the categories specified.
3426 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3429 function is_category( $category = '' ) {
3430 if ( !$this->is_category )
3433 if ( empty($category) )
3436 $cat_obj = $this->get_queried_object();
3438 $category = (array) $category;
3440 if ( in_array( $cat_obj->term_id, $category ) )
3442 elseif ( in_array( $cat_obj->name, $category ) )
3444 elseif ( in_array( $cat_obj->slug, $category ) )
3451 * Is the query for an existing tag archive page?
3453 * If the $tag parameter is specified, this function will additionally
3454 * check if the query is for one of the tags specified.
3458 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3461 function is_tag( $tag = '' ) {
3462 if ( ! $this->is_tag )
3465 if ( empty( $tag ) )
3468 $tag_obj = $this->get_queried_object();
3470 $tag = (array) $tag;
3472 if ( in_array( $tag_obj->term_id, $tag ) )
3474 elseif ( in_array( $tag_obj->name, $tag ) )
3476 elseif ( in_array( $tag_obj->slug, $tag ) )
3483 * Is the query for an existing taxonomy archive page?
3485 * If the $taxonomy parameter is specified, this function will additionally
3486 * check if the query is for that specific $taxonomy.
3488 * If the $term parameter is specified in addition to the $taxonomy parameter,
3489 * this function will additionally check if the query is for one of the terms
3494 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3495 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3498 function is_tax( $taxonomy = '', $term = '' ) {
3499 global $wp_taxonomies;
3501 if ( !$this->is_tax )
3504 if ( empty( $taxonomy ) )
3507 $queried_object = $this->get_queried_object();
3508 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3509 $term_array = (array) $term;
3511 // Check that the taxonomy matches.
3512 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3515 // Only a Taxonomy provided.
3516 if ( empty( $term ) )
3519 return isset( $queried_object->term_id ) &&
3520 count( array_intersect(
3521 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3527 * Whether the current URL is within the comments popup window.
3533 function is_comments_popup() {
3534 return (bool) $this->is_comments_popup;
3538 * Is the query for an existing date archive?
3544 function is_date() {
3545 return (bool) $this->is_date;
3549 * Is the query for an existing day archive?
3556 return (bool) $this->is_day;
3560 * Is the query for a feed?
3564 * @param string|array $feeds Optional feed types to check.
3567 function is_feed( $feeds = '' ) {
3568 if ( empty( $feeds ) || ! $this->is_feed )
3569 return (bool) $this->is_feed;
3570 $qv = $this->get( 'feed' );
3571 if ( 'feed' == $qv )
3572 $qv = get_default_feed();
3573 return in_array( $qv, (array) $feeds );
3577 * Is the query for a comments feed?
3583 function is_comment_feed() {
3584 return (bool) $this->is_comment_feed;
3588 * Is the query for the front page of the site?
3590 * This is for what is displayed at your site's main URL.
3592 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3594 * If you set a static page for the front page of your site, this function will return
3595 * true when viewing that page.
3597 * Otherwise the same as @see WP_Query::is_home()
3601 * @uses get_option()
3603 * @return bool True, if front of site.
3605 function is_front_page() {
3607 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3609 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3616 * Is the query for the blog homepage?
3618 * This is the page which shows the time based blog content of your site.
3620 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3622 * If you set a static page for the front page of your site, this function will return
3623 * true only on the page you set as the "Posts page".
3625 * @see WP_Query::is_front_page()
3629 * @return bool True if blog view homepage.
3631 function is_home() {
3632 return (bool) $this->is_home;
3636 * Is the query for an existing month archive?
3642 function is_month() {
3643 return (bool) $this->is_month;
3647 * Is the query for an existing single page?
3649 * If the $page parameter is specified, this function will additionally
3650 * check if the query is for one of the pages specified.
3652 * @see WP_Query::is_single()
3653 * @see WP_Query::is_singular()
3657 * @param mixed $page Page ID, title, slug, or array of such.
3660 function is_page( $page = '' ) {
3661 if ( !$this->is_page )
3664 if ( empty( $page ) )
3667 $page_obj = $this->get_queried_object();
3669 $page = (array) $page;
3671 if ( in_array( $page_obj->ID, $page ) )
3673 elseif ( in_array( $page_obj->post_title, $page ) )
3675 else if ( in_array( $page_obj->post_name, $page ) )
3682 * Is the query for paged result and not for the first page?
3688 function is_paged() {
3689 return (bool) $this->is_paged;
3693 * Is the query for a post or page preview?
3699 function is_preview() {
3700 return (bool) $this->is_preview;
3704 * Is the query for the robots file?
3710 function is_robots() {
3711 return (bool) $this->is_robots;
3715 * Is the query for a search?
3721 function is_search() {
3722 return (bool) $this->is_search;
3726 * Is the query for an existing single post?
3728 * Works for any post type, except attachments and pages
3730 * If the $post parameter is specified, this function will additionally
3731 * check if the query is for one of the Posts specified.
3733 * @see WP_Query::is_page()
3734 * @see WP_Query::is_singular()
3738 * @param mixed $post Post ID, title, slug, or array of such.
3741 function is_single( $post = '' ) {
3742 if ( !$this->is_single )
3748 $post_obj = $this->get_queried_object();
3750 $post = (array) $post;
3752 if ( in_array( $post_obj->ID, $post ) )
3754 elseif ( in_array( $post_obj->post_title, $post ) )
3756 elseif ( in_array( $post_obj->post_name, $post ) )
3763 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
3765 * If the $post_types parameter is specified, this function will additionally
3766 * check if the query is for one of the Posts Types specified.
3768 * @see WP_Query::is_page()
3769 * @see WP_Query::is_single()
3773 * @param mixed $post_types Optional. Post Type or array of Post Types
3776 function is_singular( $post_types = '' ) {
3777 if ( empty( $post_types ) || !$this->is_singular )
3778 return (bool) $this->is_singular;
3780 $post_obj = $this->get_queried_object();
3782 return in_array( $post_obj->post_type, (array) $post_types );
3786 * Is the query for a specific time?
3792 function is_time() {
3793 return (bool) $this->is_time;
3797 * Is the query for a trackback endpoint call?
3803 function is_trackback() {
3804 return (bool) $this->is_trackback;
3808 * Is the query for an existing year archive?
3814 function is_year() {
3815 return (bool) $this->is_year;
3819 * Is the query a 404 (returns no results)?
3826 return (bool) $this->is_404;
3830 * Is the query the main query?
3836 function is_main_query() {
3837 global $wp_the_query;
3838 return $wp_the_query === $this;
3842 * After looping through a nested query, this function
3843 * restores the $post global to the current post in this query.
3849 function reset_postdata() {
3850 if ( ! empty( $this->post ) ) {
3851 $GLOBALS['post'] = $this->post;
3852 setup_postdata( $this->post );
3858 * Redirect old slugs to the correct permalink.
3860 * Attempts to find the current slug from the past slugs.
3866 * @return null If no link is found, null is returned.
3868 function wp_old_slug_redirect() {
3870 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3873 // Guess the current post_type based on the query vars.
3874 if ( get_query_var('post_type') )
3875 $post_type = get_query_var('post_type');
3876 elseif ( !empty($wp_query->query_vars['pagename']) )
3877 $post_type = 'page';
3879 $post_type = 'post';
3881 if ( is_array( $post_type ) ) {
3882 if ( count( $post_type ) > 1 )
3884 $post_type = array_shift( $post_type );
3887 // Do not attempt redirect for hierarchical post types
3888 if ( is_post_type_hierarchical( $post_type ) )
3891 $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']);
3893 // if year, monthnum, or day have been specified, make our query more precise
3894 // just in case there are multiple identical _wp_old_slug values
3895 if ( '' != $wp_query->query_vars['year'] )
3896 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3897 if ( '' != $wp_query->query_vars['monthnum'] )
3898 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3899 if ( '' != $wp_query->query_vars['day'] )
3900 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3902 $id = (int) $wpdb->get_var($query);
3907 $link = get_permalink($id);
3912 wp_redirect( $link, 301 ); // Permanent redirect
3918 * Set up global post data.
3922 * @param object $post Post data.
3923 * @uses do_action_ref_array() Calls 'the_post'
3924 * @return bool True when finished.
3926 function setup_postdata( $post ) {
3927 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3929 $id = (int) $post->ID;
3931 $authordata = get_userdata($post->post_author);
3933 $currentday = mysql2date('d.m.y', $post->post_date, false);
3934 $currentmonth = mysql2date('m', $post->post_date, false);
3937 $page = get_query_var('page');
3940 if ( is_single() || is_page() || is_feed() )
3942 $content = $post->post_content;
3943 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
3946 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
3947 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
3948 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
3949 // Ignore nextpage at the beginning of the content.
3950 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
3951 $content = substr( $content, 15 );
3952 $pages = explode('<!--nextpage-->', $content);
3953 $numpages = count($pages);
3954 if ( $numpages > 1 )
3957 $pages = array( $post->post_content );
3960 do_action_ref_array('the_post', array(&$post));