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']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1771 $q['cat'] = ''.urldecode($q['cat']).'';
1772 $q['cat'] = addslashes_gpc($q['cat']);
1773 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1775 $req_cats = array();
1776 foreach ( (array) $cat_array as $cat ) {
1777 $cat = intval($cat);
1782 $q['category__in'][] = $cat;
1783 $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1785 $q['category__not_in'][] = $cat;
1786 $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1789 $q['cat'] = implode(',', $req_cats);
1792 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1793 $q['category__and'] = (array) $q['category__and'];
1794 if ( ! isset( $q['category__in'] ) )
1795 $q['category__in'] = array();
1796 $q['category__in'][] = absint( reset( $q['category__and'] ) );
1797 unset( $q['category__and'] );
1800 if ( ! empty( $q['category__in'] ) ) {
1801 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1802 $tax_query[] = array(
1803 'taxonomy' => 'category',
1804 'terms' => $q['category__in'],
1805 'field' => 'term_id',
1806 'include_children' => false
1810 if ( ! empty($q['category__not_in']) ) {
1811 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1812 $tax_query[] = array(
1813 'taxonomy' => 'category',
1814 'terms' => $q['category__not_in'],
1815 'operator' => 'NOT IN',
1816 'include_children' => false
1820 if ( ! empty($q['category__and']) ) {
1821 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1822 $tax_query[] = array(
1823 'taxonomy' => 'category',
1824 'terms' => $q['category__and'],
1825 'field' => 'term_id',
1826 'operator' => 'AND',
1827 'include_children' => false
1832 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1833 if ( strpos($q['tag'], ',') !== false ) {
1834 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1835 foreach ( (array) $tags as $tag ) {
1836 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1837 $q['tag_slug__in'][] = $tag;
1839 } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1840 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1841 foreach ( (array) $tags as $tag ) {
1842 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1843 $q['tag_slug__and'][] = $tag;
1846 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1847 $q['tag_slug__in'][] = $q['tag'];
1851 if ( !empty($q['tag_id']) ) {
1852 $q['tag_id'] = absint( $q['tag_id'] );
1853 $tax_query[] = array(
1854 'taxonomy' => 'post_tag',
1855 'terms' => $q['tag_id']
1859 if ( !empty($q['tag__in']) ) {
1860 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1861 $tax_query[] = array(
1862 'taxonomy' => 'post_tag',
1863 'terms' => $q['tag__in']
1867 if ( !empty($q['tag__not_in']) ) {
1868 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1869 $tax_query[] = array(
1870 'taxonomy' => 'post_tag',
1871 'terms' => $q['tag__not_in'],
1872 'operator' => 'NOT IN'
1876 if ( !empty($q['tag__and']) ) {
1877 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1878 $tax_query[] = array(
1879 'taxonomy' => 'post_tag',
1880 'terms' => $q['tag__and'],
1885 if ( !empty($q['tag_slug__in']) ) {
1886 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1887 $tax_query[] = array(
1888 'taxonomy' => 'post_tag',
1889 'terms' => $q['tag_slug__in'],
1894 if ( !empty($q['tag_slug__and']) ) {
1895 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1896 $tax_query[] = array(
1897 'taxonomy' => 'post_tag',
1898 'terms' => $q['tag_slug__and'],
1904 $this->tax_query = new WP_Tax_Query( $tax_query );
1906 do_action( 'parse_tax_query', $this );
1910 * Generate SQL for the WHERE clause based on passed search terms.
1914 * @global type $wpdb
1915 * @param array $q Query variables.
1917 protected function parse_search( &$q ) {
1922 // added slashes screw with quote grouping when done early, so done later
1923 $q['s'] = stripslashes( $q['s'] );
1924 if ( empty( $_GET['s'] ) && $this->is_main_query() )
1925 $q['s'] = urldecode( $q['s'] );
1926 // there are no line breaks in <input /> fields
1927 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
1928 $q['search_terms_count'] = 1;
1929 if ( ! empty( $q['sentence'] ) ) {
1930 $q['search_terms'] = array( $q['s'] );
1932 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
1933 $q['search_terms_count'] = count( $matches[0] );
1934 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
1935 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
1936 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
1937 $q['search_terms'] = array( $q['s'] );
1939 $q['search_terms'] = array( $q['s'] );
1943 $n = ! empty( $q['exact'] ) ? '' : '%';
1945 $q['search_orderby_title'] = array();
1946 foreach ( $q['search_terms'] as $term ) {
1947 $term = like_escape( esc_sql( $term ) );
1949 $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
1951 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1952 $searchand = ' AND ';
1955 if ( ! empty( $search ) ) {
1956 $search = " AND ({$search}) ";
1957 if ( ! is_user_logged_in() )
1958 $search .= " AND ($wpdb->posts.post_password = '') ";
1965 * Check if the terms are suitable for searching.
1967 * Uses an array of stopwords (terms) that are excluded from the separate
1968 * term matching when searching for posts. The list of English stopwords is
1969 * the approximate search engines list, and is translatable.
1973 * @param array Terms to check.
1974 * @return array Terms that are not stopwords.
1976 protected function parse_search_terms( $terms ) {
1977 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
1980 $stopwords = $this->get_search_stopwords();
1982 foreach ( $terms as $term ) {
1983 // keep before/after spaces when term is for exact match
1984 if ( preg_match( '/^".+"$/', $term ) )
1985 $term = trim( $term, "\"'" );
1987 $term = trim( $term, "\"' " );
1989 // Avoid single A-Z.
1990 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
1993 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2003 * Retrieve stopwords used when parsing search terms.
2007 * @return array Stopwords.
2009 protected function get_search_stopwords() {
2010 if ( isset( $this->stopwords ) )
2011 return $this->stopwords;
2013 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2014 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2015 * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2017 $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',
2018 'Comma-separated list of search stopwords in your language' ) );
2020 foreach( $words as $word ) {
2021 $word = trim( $word, "\r\n\t " );
2023 $stopwords[] = $word;
2027 * Filter stopwords used when parsing search terms.
2031 * @param array $stopwords Stopwords.
2033 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2034 return $this->stopwords;
2038 * Generate SQL for the ORDER BY condition based on passed search terms.
2040 * @global wpdb $wpdb
2041 * @param array $q Query variables.
2042 * @return string ORDER BY clause.
2044 protected function parse_search_order( &$q ) {
2047 $search_orderby = '';
2049 if ( $q['search_terms_count'] > 1 ) {
2050 $num_terms = count( $q['search_orderby_title'] );
2051 $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
2053 $search_orderby = '(CASE ';
2054 // sentence match in 'post_title'
2055 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
2057 // sanity limit, sort as sentence when more than 6 terms
2058 // (few searches are longer than 6 terms and most titles are not)
2059 if ( $num_terms < 7 ) {
2060 // all words in title
2061 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2062 // any word in title, not needed when $num_terms == 1
2063 if ( $num_terms > 1 )
2064 $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2067 // sentence match in 'post_content'
2068 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
2069 $search_orderby .= 'ELSE 5 END)';
2071 // single word or sentence search
2072 $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2075 return $search_orderby;
2079 * Sets the 404 property and saves whether query is feed.
2084 function set_404() {
2085 $is_feed = $this->is_feed;
2087 $this->init_query_flags();
2088 $this->is_404 = true;
2090 $this->is_feed = $is_feed;
2094 * Retrieve query variable.
2099 * @param string $query_var Query variable key.
2102 function get($query_var) {
2103 if ( isset($this->query_vars[$query_var]) )
2104 return $this->query_vars[$query_var];
2110 * Set query variable.
2115 * @param string $query_var Query variable key.
2116 * @param mixed $value Query variable value.
2118 function set($query_var, $value) {
2119 $this->query_vars[$query_var] = $value;
2123 * Retrieve the posts based on query variables.
2125 * There are a few filters and actions that can be used to modify the post
2130 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
2132 * @return array List of posts.
2134 function get_posts() {
2137 $this->parse_query();
2139 do_action_ref_array('pre_get_posts', array(&$this));
2142 $q = &$this->query_vars;
2144 // Fill again in case pre_get_posts unset some vars.
2145 $q = $this->fill_query_vars($q);
2148 $this->meta_query = new WP_Meta_Query();
2149 $this->meta_query->parse_query_vars( $q );
2151 // Set a flag if a pre_get_posts hook changed the query vars.
2152 $hash = md5( serialize( $this->query_vars ) );
2153 if ( $hash != $this->query_vars_hash ) {
2154 $this->query_vars_changed = true;
2155 $this->query_vars_hash = $hash;
2159 // First let's clear some variables
2162 $whichmimetype = '';
2169 $post_status_join = false;
2172 if ( isset( $q['caller_get_posts'] ) ) {
2173 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2174 if ( !isset( $q['ignore_sticky_posts'] ) )
2175 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2178 if ( !isset( $q['ignore_sticky_posts'] ) )
2179 $q['ignore_sticky_posts'] = false;
2181 if ( !isset($q['suppress_filters']) )
2182 $q['suppress_filters'] = false;
2184 if ( !isset($q['cache_results']) ) {
2185 if ( wp_using_ext_object_cache() )
2186 $q['cache_results'] = false;
2188 $q['cache_results'] = true;
2191 if ( !isset($q['update_post_term_cache']) )
2192 $q['update_post_term_cache'] = true;
2194 if ( !isset($q['update_post_meta_cache']) )
2195 $q['update_post_meta_cache'] = true;
2197 if ( !isset($q['post_type']) ) {
2198 if ( $this->is_search )
2199 $q['post_type'] = 'any';
2201 $q['post_type'] = '';
2203 $post_type = $q['post_type'];
2204 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
2205 $q['posts_per_page'] = get_option('posts_per_page');
2206 if ( isset($q['showposts']) && $q['showposts'] ) {
2207 $q['showposts'] = (int) $q['showposts'];
2208 $q['posts_per_page'] = $q['showposts'];
2210 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2211 $q['posts_per_page'] = $q['posts_per_archive_page'];
2212 if ( !isset($q['nopaging']) ) {
2213 if ( $q['posts_per_page'] == -1 ) {
2214 $q['nopaging'] = true;
2216 $q['nopaging'] = false;
2219 if ( $this->is_feed ) {
2220 $q['posts_per_page'] = get_option('posts_per_rss');
2221 $q['nopaging'] = false;
2223 $q['posts_per_page'] = (int) $q['posts_per_page'];
2224 if ( $q['posts_per_page'] < -1 )
2225 $q['posts_per_page'] = abs($q['posts_per_page']);
2226 else if ( $q['posts_per_page'] == 0 )
2227 $q['posts_per_page'] = 1;
2229 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2230 $q['comments_per_page'] = get_option('comments_per_page');
2232 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2233 $this->is_page = true;
2234 $this->is_home = false;
2235 $q['page_id'] = get_option('page_on_front');
2238 if ( isset($q['page']) ) {
2239 $q['page'] = trim($q['page'], '/');
2240 $q['page'] = absint($q['page']);
2243 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2244 if ( isset($q['no_found_rows']) )
2245 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2247 $q['no_found_rows'] = false;
2249 switch ( $q['fields'] ) {
2251 $fields = "$wpdb->posts.ID";
2254 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2257 $fields = "$wpdb->posts.*";
2260 if ( '' !== $q['menu_order'] )
2261 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2263 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2265 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2266 if ( strlen($q['m']) > 5 )
2267 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2268 if ( strlen($q['m']) > 7 )
2269 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2270 if ( strlen($q['m']) > 9 )
2271 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2272 if ( strlen($q['m']) > 11 )
2273 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2274 if ( strlen($q['m']) > 13 )
2275 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2278 // Handle the other individual date parameters
2279 $date_parameters = array();
2281 if ( '' !== $q['hour'] )
2282 $date_parameters['hour'] = $q['hour'];
2284 if ( '' !== $q['minute'] )
2285 $date_parameters['minute'] = $q['minute'];
2287 if ( '' !== $q['second'] )
2288 $date_parameters['second'] = $q['second'];
2291 $date_parameters['year'] = $q['year'];
2293 if ( $q['monthnum'] )
2294 $date_parameters['monthnum'] = $q['monthnum'];
2297 $date_parameters['week'] = $q['w'];
2300 $date_parameters['day'] = $q['day'];
2302 if ( $date_parameters ) {
2303 $date_query = new WP_Date_Query( array( $date_parameters ) );
2304 $where .= $date_query->get_sql();
2306 unset( $date_parameters, $date_query );
2308 // Handle complex date queries
2309 if ( ! empty( $q['date_query'] ) ) {
2310 $this->date_query = new WP_Date_Query( $q['date_query'] );
2311 $where .= $this->date_query->get_sql();
2315 // If we've got a post_type AND it's not "any" post_type.
2316 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2317 foreach ( (array)$q['post_type'] as $_post_type ) {
2318 $ptype_obj = get_post_type_object($_post_type);
2319 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2322 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2323 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2324 $q['name'] = $q[ $ptype_obj->query_var ];
2326 // Hierarchical post_types will operate through the
2327 $q['pagename'] = $q[ $ptype_obj->query_var ];
2331 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2337 if ( '' != $q['name'] ) {
2338 $q['name'] = sanitize_title_for_query( $q['name'] );
2339 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2340 } elseif ( '' != $q['pagename'] ) {
2341 if ( isset($this->queried_object_id) ) {
2342 $reqpage = $this->queried_object_id;
2344 if ( 'page' != $q['post_type'] ) {
2345 foreach ( (array)$q['post_type'] as $_post_type ) {
2346 $ptype_obj = get_post_type_object($_post_type);
2347 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2350 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2356 $reqpage = get_page_by_path($q['pagename']);
2358 if ( !empty($reqpage) )
2359 $reqpage = $reqpage->ID;
2364 $page_for_posts = get_option('page_for_posts');
2365 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2366 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2367 $q['name'] = $q['pagename'];
2368 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2369 $reqpage_obj = get_post( $reqpage );
2370 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2371 $this->is_attachment = true;
2372 $post_type = $q['post_type'] = 'attachment';
2373 $this->is_page = true;
2374 $q['attachment_id'] = $reqpage;
2377 } elseif ( '' != $q['attachment'] ) {
2378 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2379 $q['name'] = $q['attachment'];
2380 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2384 if ( intval($q['comments_popup']) )
2385 $q['p'] = absint($q['comments_popup']);
2387 // If an attachment is requested by number, let it supersede any post number.
2388 if ( $q['attachment_id'] )
2389 $q['p'] = absint($q['attachment_id']);
2391 // If a post number is specified, load that post
2393 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2394 } elseif ( $q['post__in'] ) {
2395 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2396 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2397 } elseif ( $q['post__not_in'] ) {
2398 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2399 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2402 if ( is_numeric( $q['post_parent'] ) ) {
2403 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2404 } elseif ( $q['post_parent__in'] ) {
2405 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2406 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2407 } elseif ( $q['post_parent__not_in'] ) {
2408 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2409 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2412 if ( $q['page_id'] ) {
2413 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2414 $q['p'] = $q['page_id'];
2415 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2419 // If a search pattern is specified, load the posts that match.
2420 if ( ! empty( $q['s'] ) )
2421 $search = $this->parse_search( $q );
2424 * Filter the search SQL that is used in the WHERE clause of WP_Query.
2428 * @param string $search Search SQL for WHERE clause.
2429 * @param WP_Query $this The current WP_Query object.
2431 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2434 if ( !$this->is_singular ) {
2435 $this->parse_tax_query( $q );
2437 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2439 $join .= $clauses['join'];
2440 $where .= $clauses['where'];
2443 if ( $this->is_tax ) {
2444 if ( empty($post_type) ) {
2445 // Do a fully inclusive search for currently registered post types of queried taxonomies
2446 $post_type = array();
2447 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2448 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2449 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2450 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2455 elseif ( count( $post_type ) == 1 )
2456 $post_type = $post_type[0];
2458 $post_status_join = true;
2459 } elseif ( in_array('attachment', (array) $post_type) ) {
2460 $post_status_join = true;
2465 if ( !empty($this->tax_query->queries) ) {
2466 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2467 if ( !empty( $tax_query_in_and ) ) {
2468 if ( !isset( $q['taxonomy'] ) ) {
2469 foreach ( $tax_query_in_and as $a_tax_query ) {
2470 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2471 $q['taxonomy'] = $a_tax_query['taxonomy'];
2472 if ( 'slug' == $a_tax_query['field'] )
2473 $q['term'] = $a_tax_query['terms'][0];
2475 $q['term_id'] = $a_tax_query['terms'][0];
2482 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2483 if ( ! empty( $cat_query ) ) {
2484 $cat_query = reset( $cat_query );
2486 if ( ! empty( $cat_query['terms'][0] ) ) {
2487 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2489 $this->set( 'cat', $the_cat->term_id );
2490 $this->set( 'category_name', $the_cat->slug );
2495 unset( $cat_query );
2497 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2498 if ( ! empty( $tag_query ) ) {
2499 $tag_query = reset( $tag_query );
2501 if ( ! empty( $tag_query['terms'][0] ) ) {
2502 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2504 $this->set( 'tag_id', $the_tag->term_id );
2508 unset( $tag_query );
2512 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2513 $groupby = "{$wpdb->posts}.ID";
2516 // Author/user stuff
2518 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2519 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2520 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2521 foreach ( $authors as $author ) {
2522 $key = $author > 0 ? 'author__in' : 'author__not_in';
2523 $q[$key][] = abs( $author );
2525 $q['author'] = implode( ',', $authors );
2528 if ( ! empty( $q['author__not_in'] ) ) {
2529 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2530 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2531 } elseif ( ! empty( $q['author__in'] ) ) {
2532 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2533 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2536 // Author stuff for nice URLs
2538 if ( '' != $q['author_name'] ) {
2539 if ( strpos($q['author_name'], '/') !== false ) {
2540 $q['author_name'] = explode('/', $q['author_name']);
2541 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2542 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2544 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2547 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2548 $q['author'] = get_user_by('slug', $q['author_name']);
2550 $q['author'] = $q['author']->ID;
2551 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2554 // MIME-Type stuff for attachment browsing
2556 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2557 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2559 $where .= $search . $whichauthor . $whichmimetype;
2561 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2562 $q['order'] = 'DESC';
2565 if ( empty($q['orderby']) ) {
2566 $orderby = "$wpdb->posts.post_date " . $q['order'];
2567 } elseif ( 'none' == $q['orderby'] ) {
2569 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2570 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2571 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2572 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2574 // Used to filter values
2575 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2576 if ( !empty($q['meta_key']) ) {
2577 $allowed_keys[] = $q['meta_key'];
2578 $allowed_keys[] = 'meta_value';
2579 $allowed_keys[] = 'meta_value_num';
2581 $q['orderby'] = urldecode($q['orderby']);
2582 $q['orderby'] = addslashes_gpc($q['orderby']);
2584 $orderby_array = array();
2585 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2586 // Only allow certain values for safety
2587 if ( ! in_array($orderby, $allowed_keys) )
2590 switch ( $orderby ) {
2592 $orderby = "$wpdb->posts.menu_order";
2595 $orderby = "$wpdb->posts.ID";
2598 $orderby = 'RAND()';
2600 case $q['meta_key']:
2602 if ( isset( $q['meta_type'] ) ) {
2603 $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
2604 $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
2606 $orderby = "$wpdb->postmeta.meta_value";
2609 case 'meta_value_num':
2610 $orderby = "$wpdb->postmeta.meta_value+0";
2612 case 'comment_count':
2613 $orderby = "$wpdb->posts.comment_count";
2616 $orderby = "$wpdb->posts.post_" . $orderby;
2619 $orderby_array[] = $orderby;
2621 $orderby = implode( ',', $orderby_array );
2623 if ( empty( $orderby ) )
2624 $orderby = "$wpdb->posts.post_date ".$q['order'];
2626 $orderby .= " {$q['order']}";
2629 // Order search results by relevance only when another "orderby" is not specified in the query.
2630 if ( ! empty( $q['s'] ) ) {
2631 $search_orderby = '';
2632 if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2633 $search_orderby = $this->parse_search_order( $q );
2636 * Filter the ORDER BY used when ordering search results.
2640 * @param string $search_orderby The ORDER BY clause.
2641 * @param WP_Query $this The current WP_Query instance.
2643 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2644 if ( $search_orderby )
2645 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2648 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2649 $post_type_cap = 'multiple_post_type';
2651 if ( is_array( $post_type ) )
2652 $post_type = reset( $post_type );
2653 $post_type_object = get_post_type_object( $post_type );
2654 if ( empty( $post_type_object ) )
2655 $post_type_cap = $post_type;
2658 if ( 'any' == $post_type ) {
2659 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2660 if ( empty( $in_search_post_types ) )
2661 $where .= ' AND 1=0 ';
2663 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2664 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2665 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2666 } elseif ( ! empty( $post_type ) ) {
2667 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2668 $post_type_object = get_post_type_object ( $post_type );
2669 } elseif ( $this->is_attachment ) {
2670 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2671 $post_type_object = get_post_type_object ( 'attachment' );
2672 } elseif ( $this->is_page ) {
2673 $where .= " AND $wpdb->posts.post_type = 'page'";
2674 $post_type_object = get_post_type_object ( 'page' );
2676 $where .= " AND $wpdb->posts.post_type = 'post'";
2677 $post_type_object = get_post_type_object ( 'post' );
2680 $edit_cap = 'edit_post';
2681 $read_cap = 'read_post';
2683 if ( ! empty( $post_type_object ) ) {
2684 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2685 $read_private_cap = $post_type_object->cap->read_private_posts;
2687 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2688 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2691 $user_id = get_current_user_id();
2693 if ( ! empty( $q['post_status'] ) ) {
2694 $statuswheres = array();
2695 $q_status = $q['post_status'];
2696 if ( ! is_array( $q_status ) )
2697 $q_status = explode(',', $q_status);
2698 $r_status = array();
2699 $p_status = array();
2700 $e_status = array();
2701 if ( in_array('any', $q_status) ) {
2702 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2703 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2705 foreach ( get_post_stati() as $status ) {
2706 if ( in_array( $status, $q_status ) ) {
2707 if ( 'private' == $status )
2708 $p_status[] = "$wpdb->posts.post_status = '$status'";
2710 $r_status[] = "$wpdb->posts.post_status = '$status'";
2715 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2716 $r_status = array_merge($r_status, $p_status);
2720 if ( !empty($e_status) ) {
2721 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2723 if ( !empty($r_status) ) {
2724 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2725 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
2727 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2729 if ( !empty($p_status) ) {
2730 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2731 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
2733 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2735 if ( $post_status_join ) {
2736 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2737 foreach ( $statuswheres as $index => $statuswhere )
2738 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2740 foreach ( $statuswheres as $statuswhere )
2741 $where .= " AND $statuswhere";
2742 } elseif ( !$this->is_singular ) {
2743 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2745 // Add public states.
2746 $public_states = get_post_stati( array('public' => true) );
2747 foreach ( (array) $public_states as $state ) {
2748 if ( 'publish' == $state ) // Publish is hard-coded above.
2750 $where .= " OR $wpdb->posts.post_status = '$state'";
2753 if ( $this->is_admin ) {
2754 // Add protected states that should show in the admin all list.
2755 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2756 foreach ( (array) $admin_all_states as $state )
2757 $where .= " OR $wpdb->posts.post_status = '$state'";
2760 if ( is_user_logged_in() ) {
2761 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2762 $private_states = get_post_stati( array('private' => true) );
2763 foreach ( (array) $private_states as $state )
2764 $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'";
2770 if ( !empty( $this->meta_query->queries ) ) {
2771 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2772 $join .= $clauses['join'];
2773 $where .= $clauses['where'];
2776 // Apply filters on where and join prior to paging so that any
2777 // manipulations to them are reflected in the paging by day queries.
2778 if ( !$q['suppress_filters'] ) {
2779 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2780 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2784 if ( empty($q['nopaging']) && !$this->is_singular ) {
2785 $page = absint($q['paged']);
2789 if ( empty($q['offset']) ) {
2790 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2791 } else { // we're ignoring $page and using 'offset'
2792 $q['offset'] = absint($q['offset']);
2793 $pgstrt = $q['offset'] . ', ';
2795 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2799 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2800 if ( $this->is_archive || $this->is_search ) {
2801 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2802 $cwhere = "WHERE comment_approved = '1' $where";
2803 $cgroupby = "$wpdb->comments.comment_id";
2804 } else { // Other non singular e.g. front
2805 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2806 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2810 if ( !$q['suppress_filters'] ) {
2811 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2812 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2813 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2814 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2815 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2817 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2818 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2820 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2821 $this->comment_count = count($this->comments);
2823 $post_ids = array();
2825 foreach ( $this->comments as $comment )
2826 $post_ids[] = (int) $comment->comment_post_ID;
2828 $post_ids = join(',', $post_ids);
2831 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2836 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2838 // Apply post-paging filters on where and join. Only plugins that
2839 // manipulate paging queries should use these hooks.
2840 if ( !$q['suppress_filters'] ) {
2841 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2842 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2843 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2844 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2845 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2846 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2847 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2849 // Filter all clauses at once, for convenience
2850 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2851 foreach ( $pieces as $piece )
2852 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2855 // Announce current selection parameters. For use by caching plugins.
2856 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2858 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2859 if ( !$q['suppress_filters'] ) {
2860 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2861 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2862 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2863 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2864 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2865 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2866 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2868 // Filter all clauses at once, for convenience
2869 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2870 foreach ( $pieces as $piece )
2871 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2874 if ( ! empty($groupby) )
2875 $groupby = 'GROUP BY ' . $groupby;
2876 if ( !empty( $orderby ) )
2877 $orderby = 'ORDER BY ' . $orderby;
2880 if ( !$q['no_found_rows'] && !empty($limits) )
2881 $found_rows = 'SQL_CALC_FOUND_ROWS';
2883 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2885 if ( !$q['suppress_filters'] ) {
2886 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2889 if ( 'ids' == $q['fields'] ) {
2890 $this->posts = $wpdb->get_col( $this->request );
2891 $this->post_count = count( $this->posts );
2892 $this->set_found_posts( $q, $limits );
2894 return $this->posts;
2897 if ( 'id=>parent' == $q['fields'] ) {
2898 $this->posts = $wpdb->get_results( $this->request );
2899 $this->post_count = count( $this->posts );
2900 $this->set_found_posts( $q, $limits );
2903 foreach ( $this->posts as $post )
2904 $r[ $post->ID ] = $post->post_parent;
2909 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2910 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2912 if ( $split_the_query ) {
2913 // First get the IDs and then fill in the objects
2915 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2917 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2919 $ids = $wpdb->get_col( $this->request );
2922 $this->posts = $ids;
2923 $this->set_found_posts( $q, $limits );
2924 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2926 $this->posts = array();
2929 $this->posts = $wpdb->get_results( $this->request );
2930 $this->set_found_posts( $q, $limits );
2933 // Convert to WP_Post objects
2935 $this->posts = array_map( 'get_post', $this->posts );
2937 // Raw results filter. Prior to status checks.
2938 if ( !$q['suppress_filters'] )
2939 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2941 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2942 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2943 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2944 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2945 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2946 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2947 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2948 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2949 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2950 $this->comments = $wpdb->get_results($comments_request);
2951 $this->comment_count = count($this->comments);
2954 // Check post status to determine if post should be displayed.
2955 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2956 $status = get_post_status($this->posts[0]);
2957 $post_status_obj = get_post_status_object($status);
2958 //$type = get_post_type($this->posts[0]);
2959 if ( !$post_status_obj->public ) {
2960 if ( ! is_user_logged_in() ) {
2961 // User must be logged in to view unpublished posts.
2962 $this->posts = array();
2964 if ( $post_status_obj->protected ) {
2965 // User must have edit permissions on the draft to preview.
2966 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2967 $this->posts = array();
2969 $this->is_preview = true;
2970 if ( 'future' != $status )
2971 $this->posts[0]->post_date = current_time('mysql');
2973 } elseif ( $post_status_obj->private ) {
2974 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2975 $this->posts = array();
2977 $this->posts = array();
2982 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2983 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
2986 // Put sticky posts at the top of the posts array
2987 $sticky_posts = get_option('sticky_posts');
2988 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2989 $num_posts = count($this->posts);
2991 // Loop over posts and relocate stickies to the front.
2992 for ( $i = 0; $i < $num_posts; $i++ ) {
2993 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2994 $sticky_post = $this->posts[$i];
2995 // Remove sticky from current position
2996 array_splice($this->posts, $i, 1);
2997 // Move to front, after other stickies
2998 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2999 // Increment the sticky offset. The next sticky will be placed at this offset.
3001 // Remove post from sticky posts array
3002 $offset = array_search($sticky_post->ID, $sticky_posts);
3003 unset( $sticky_posts[$offset] );
3007 // If any posts have been excluded specifically, Ignore those that are sticky.
3008 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3009 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3011 // Fetch sticky posts that weren't in the query results
3012 if ( !empty($sticky_posts) ) {
3013 $stickies = get_posts( array(
3014 'post__in' => $sticky_posts,
3015 'post_type' => $post_type,
3016 'post_status' => 'publish',
3020 foreach ( $stickies as $sticky_post ) {
3021 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3027 if ( !$q['suppress_filters'] )
3028 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
3030 // Ensure that any posts added/modified via one of the filters above are
3031 // of the type WP_Post and are filtered.
3032 if ( $this->posts ) {
3033 $this->post_count = count( $this->posts );
3035 $this->posts = array_map( 'get_post', $this->posts );
3037 if ( $q['cache_results'] )
3038 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3040 $this->post = reset( $this->posts );
3042 $this->post_count = 0;
3043 $this->posts = array();
3046 return $this->posts;
3050 * Set up the amount of found posts and the number of pages (if limit clause was used)
3051 * for the current query.
3056 function set_found_posts( $q, $limits ) {
3059 // Bail if posts is an empty array. Continue if posts is an empty string,
3060 // null, or false to accommodate caching plugins that fill posts later.
3061 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3064 if ( ! empty( $limits ) )
3065 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3067 $this->found_posts = count( $this->posts );
3069 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3071 if ( ! empty( $limits ) )
3072 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3076 * Set up the next post and iterate current post index.
3081 * @return WP_Post Next post.
3083 function next_post() {
3085 $this->current_post++;
3087 $this->post = $this->posts[$this->current_post];
3092 * Sets up the current post.
3094 * Retrieves the next post, sets up the post, sets the 'in the loop'
3100 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
3102 function the_post() {
3104 $this->in_the_loop = true;
3106 if ( $this->current_post == -1 ) // loop has just started
3107 do_action_ref_array('loop_start', array(&$this));
3109 $post = $this->next_post();
3110 setup_postdata($post);
3114 * Whether there are more posts available in the loop.
3116 * Calls action 'loop_end', when the loop is complete.
3120 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
3122 * @return bool True if posts are available, false if end of loop.
3124 function have_posts() {
3125 if ( $this->current_post + 1 < $this->post_count ) {
3127 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3128 do_action_ref_array('loop_end', array(&$this));
3129 // Do some cleaning up after the loop
3130 $this->rewind_posts();
3133 $this->in_the_loop = false;
3138 * Rewind the posts and reset post index.
3143 function rewind_posts() {
3144 $this->current_post = -1;
3145 if ( $this->post_count > 0 ) {
3146 $this->post = $this->posts[0];
3151 * Iterate current comment index and return comment object.
3156 * @return object Comment object.
3158 function next_comment() {
3159 $this->current_comment++;
3161 $this->comment = $this->comments[$this->current_comment];
3162 return $this->comment;
3166 * Sets up the current comment.
3170 * @global object $comment Current comment.
3171 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
3173 function the_comment() {
3176 $comment = $this->next_comment();
3178 if ( $this->current_comment == 0 ) {
3179 do_action('comment_loop_start');
3184 * Whether there are more comments available.
3186 * Automatically rewinds comments when finished.
3191 * @return bool True, if more comments. False, if no more posts.
3193 function have_comments() {
3194 if ( $this->current_comment + 1 < $this->comment_count ) {
3196 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3197 $this->rewind_comments();
3204 * Rewind the comments, resets the comment index and comment to first.
3209 function rewind_comments() {
3210 $this->current_comment = -1;
3211 if ( $this->comment_count > 0 ) {
3212 $this->comment = $this->comments[0];
3217 * Sets up the WordPress query by parsing query string.
3222 * @param string $query URL query string.
3223 * @return array List of posts.
3225 function query( $query ) {
3227 $this->query = $this->query_vars = wp_parse_args( $query );
3228 return $this->get_posts();
3232 * Retrieve queried object.
3234 * If queried object is not set, then the queried object will be set from
3235 * the category, tag, taxonomy, posts page, single post, page, or author
3236 * query variable. After it is set up, it will be returned.
3243 function get_queried_object() {
3244 if ( isset($this->queried_object) )
3245 return $this->queried_object;
3247 $this->queried_object = null;
3248 $this->queried_object_id = 0;
3250 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3251 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3253 $query = reset( $tax_query_in_and );
3255 if ( 'term_id' == $query['field'] )
3256 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3257 elseif ( $query['terms'] )
3258 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3260 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3261 $this->queried_object = $term;
3262 $this->queried_object_id = (int) $term->term_id;
3264 if ( $this->is_category )
3265 _make_cat_compat( $this->queried_object );
3267 } elseif ( $this->is_post_type_archive ) {
3268 $post_type = $this->get( 'post_type' );
3269 if ( is_array( $post_type ) )
3270 $post_type = reset( $post_type );
3271 $this->queried_object = get_post_type_object( $post_type );
3272 } elseif ( $this->is_posts_page ) {
3273 $page_for_posts = get_option('page_for_posts');
3274 $this->queried_object = get_post( $page_for_posts );
3275 $this->queried_object_id = (int) $this->queried_object->ID;
3276 } elseif ( $this->is_singular && !is_null($this->post) ) {
3277 $this->queried_object = $this->post;
3278 $this->queried_object_id = (int) $this->post->ID;
3279 } elseif ( $this->is_author ) {
3280 $this->queried_object_id = (int) $this->get('author');
3281 $this->queried_object = get_userdata( $this->queried_object_id );
3284 return $this->queried_object;
3288 * Retrieve ID of the current queried object.
3295 function get_queried_object_id() {
3296 $this->get_queried_object();
3298 if ( isset($this->queried_object_id) ) {
3299 return $this->queried_object_id;
3308 * Sets up the WordPress query, if parameter is not empty.
3313 * @param string $query URL query string.
3316 function __construct($query = '') {
3317 if ( ! empty($query) ) {
3318 $this->query($query);
3323 * Is the query for an existing archive page?
3325 * Month, Year, Category, Author, Post Type archive...
3331 function is_archive() {
3332 return (bool) $this->is_archive;
3336 * Is the query for an existing post type archive page?
3340 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3343 function is_post_type_archive( $post_types = '' ) {
3344 if ( empty( $post_types ) || ! $this->is_post_type_archive )
3345 return (bool) $this->is_post_type_archive;
3347 $post_type = $this->get( 'post_type' );
3348 if ( is_array( $post_type ) )
3349 $post_type = reset( $post_type );
3350 $post_type_object = get_post_type_object( $post_type );
3352 return in_array( $post_type_object->name, (array) $post_types );
3356 * Is the query for an existing attachment page?
3362 function is_attachment() {
3363 return (bool) $this->is_attachment;
3367 * Is the query for an existing author archive page?
3369 * If the $author parameter is specified, this function will additionally
3370 * check if the query is for one of the authors specified.
3374 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3377 function is_author( $author = '' ) {
3378 if ( !$this->is_author )
3381 if ( empty($author) )
3384 $author_obj = $this->get_queried_object();
3386 $author = (array) $author;
3388 if ( in_array( $author_obj->ID, $author ) )
3390 elseif ( in_array( $author_obj->nickname, $author ) )
3392 elseif ( in_array( $author_obj->user_nicename, $author ) )
3399 * Is the query for an existing category archive page?
3401 * If the $category parameter is specified, this function will additionally
3402 * check if the query is for one of the categories specified.
3406 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3409 function is_category( $category = '' ) {
3410 if ( !$this->is_category )
3413 if ( empty($category) )
3416 $cat_obj = $this->get_queried_object();
3418 $category = (array) $category;
3420 if ( in_array( $cat_obj->term_id, $category ) )
3422 elseif ( in_array( $cat_obj->name, $category ) )
3424 elseif ( in_array( $cat_obj->slug, $category ) )
3431 * Is the query for an existing tag archive page?
3433 * If the $tag parameter is specified, this function will additionally
3434 * check if the query is for one of the tags specified.
3438 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
3441 function is_tag( $tag = '' ) {
3442 if ( ! $this->is_tag )
3445 if ( empty( $tag ) )
3448 $tag_obj = $this->get_queried_object();
3450 $tag = (array) $tag;
3452 if ( in_array( $tag_obj->term_id, $tag ) )
3454 elseif ( in_array( $tag_obj->name, $tag ) )
3456 elseif ( in_array( $tag_obj->slug, $tag ) )
3463 * Is the query for an existing taxonomy archive page?
3465 * If the $taxonomy parameter is specified, this function will additionally
3466 * check if the query is for that specific $taxonomy.
3468 * If the $term parameter is specified in addition to the $taxonomy parameter,
3469 * this function will additionally check if the query is for one of the terms
3474 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3475 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3478 function is_tax( $taxonomy = '', $term = '' ) {
3479 global $wp_taxonomies;
3481 if ( !$this->is_tax )
3484 if ( empty( $taxonomy ) )
3487 $queried_object = $this->get_queried_object();
3488 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3489 $term_array = (array) $term;
3491 // Check that the taxonomy matches.
3492 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3495 // Only a Taxonomy provided.
3496 if ( empty( $term ) )
3499 return isset( $queried_object->term_id ) &&
3500 count( array_intersect(
3501 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3507 * Whether the current URL is within the comments popup window.
3513 function is_comments_popup() {
3514 return (bool) $this->is_comments_popup;
3518 * Is the query for an existing date archive?
3524 function is_date() {
3525 return (bool) $this->is_date;
3529 * Is the query for an existing day archive?
3536 return (bool) $this->is_day;
3540 * Is the query for a feed?
3544 * @param string|array $feeds Optional feed types to check.
3547 function is_feed( $feeds = '' ) {
3548 if ( empty( $feeds ) || ! $this->is_feed )
3549 return (bool) $this->is_feed;
3550 $qv = $this->get( 'feed' );
3551 if ( 'feed' == $qv )
3552 $qv = get_default_feed();
3553 return in_array( $qv, (array) $feeds );
3557 * Is the query for a comments feed?
3563 function is_comment_feed() {
3564 return (bool) $this->is_comment_feed;
3568 * Is the query for the front page of the site?
3570 * This is for what is displayed at your site's main URL.
3572 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3574 * If you set a static page for the front page of your site, this function will return
3575 * true when viewing that page.
3577 * Otherwise the same as @see WP_Query::is_home()
3581 * @uses get_option()
3583 * @return bool True, if front of site.
3585 function is_front_page() {
3587 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3589 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3596 * Is the query for the blog homepage?
3598 * This is the page which shows the time based blog content of your site.
3600 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3602 * If you set a static page for the front page of your site, this function will return
3603 * true only on the page you set as the "Posts page".
3605 * @see WP_Query::is_front_page()
3609 * @return bool True if blog view homepage.
3611 function is_home() {
3612 return (bool) $this->is_home;
3616 * Is the query for an existing month archive?
3622 function is_month() {
3623 return (bool) $this->is_month;
3627 * Is the query for an existing single page?
3629 * If the $page parameter is specified, this function will additionally
3630 * check if the query is for one of the pages specified.
3632 * @see WP_Query::is_single()
3633 * @see WP_Query::is_singular()
3637 * @param mixed $page Page ID, title, slug, or array of such.
3640 function is_page( $page = '' ) {
3641 if ( !$this->is_page )
3644 if ( empty( $page ) )
3647 $page_obj = $this->get_queried_object();
3649 $page = (array) $page;
3651 if ( in_array( $page_obj->ID, $page ) )
3653 elseif ( in_array( $page_obj->post_title, $page ) )
3655 else if ( in_array( $page_obj->post_name, $page ) )
3662 * Is the query for paged result and not for the first page?
3668 function is_paged() {
3669 return (bool) $this->is_paged;
3673 * Is the query for a post or page preview?
3679 function is_preview() {
3680 return (bool) $this->is_preview;
3684 * Is the query for the robots file?
3690 function is_robots() {
3691 return (bool) $this->is_robots;
3695 * Is the query for a search?
3701 function is_search() {
3702 return (bool) $this->is_search;
3706 * Is the query for an existing single post?
3708 * Works for any post type, except attachments and pages
3710 * If the $post parameter is specified, this function will additionally
3711 * check if the query is for one of the Posts specified.
3713 * @see WP_Query::is_page()
3714 * @see WP_Query::is_singular()
3718 * @param mixed $post Post ID, title, slug, or array of such.
3721 function is_single( $post = '' ) {
3722 if ( !$this->is_single )
3728 $post_obj = $this->get_queried_object();
3730 $post = (array) $post;
3732 if ( in_array( $post_obj->ID, $post ) )
3734 elseif ( in_array( $post_obj->post_title, $post ) )
3736 elseif ( in_array( $post_obj->post_name, $post ) )
3743 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
3745 * If the $post_types parameter is specified, this function will additionally
3746 * check if the query is for one of the Posts Types specified.
3748 * @see WP_Query::is_page()
3749 * @see WP_Query::is_single()
3753 * @param mixed $post_types Optional. Post Type or array of Post Types
3756 function is_singular( $post_types = '' ) {
3757 if ( empty( $post_types ) || !$this->is_singular )
3758 return (bool) $this->is_singular;
3760 $post_obj = $this->get_queried_object();
3762 return in_array( $post_obj->post_type, (array) $post_types );
3766 * Is the query for a specific time?
3772 function is_time() {
3773 return (bool) $this->is_time;
3777 * Is the query for a trackback endpoint call?
3783 function is_trackback() {
3784 return (bool) $this->is_trackback;
3788 * Is the query for an existing year archive?
3794 function is_year() {
3795 return (bool) $this->is_year;
3799 * Is the query a 404 (returns no results)?
3806 return (bool) $this->is_404;
3810 * Is the query the main query?
3816 function is_main_query() {
3817 global $wp_the_query;
3818 return $wp_the_query === $this;
3822 * After looping through a nested query, this function
3823 * restores the $post global to the current post in this query.
3829 function reset_postdata() {
3830 if ( ! empty( $this->post ) ) {
3831 $GLOBALS['post'] = $this->post;
3832 setup_postdata( $this->post );
3838 * Redirect old slugs to the correct permalink.
3840 * Attempts to find the current slug from the past slugs.
3846 * @return null If no link is found, null is returned.
3848 function wp_old_slug_redirect() {
3850 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3853 // Guess the current post_type based on the query vars.
3854 if ( get_query_var('post_type') )
3855 $post_type = get_query_var('post_type');
3856 elseif ( !empty($wp_query->query_vars['pagename']) )
3857 $post_type = 'page';
3859 $post_type = 'post';
3861 if ( is_array( $post_type ) ) {
3862 if ( count( $post_type ) > 1 )
3864 $post_type = array_shift( $post_type );
3867 // Do not attempt redirect for hierarchical post types
3868 if ( is_post_type_hierarchical( $post_type ) )
3871 $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']);
3873 // if year, monthnum, or day have been specified, make our query more precise
3874 // just in case there are multiple identical _wp_old_slug values
3875 if ( '' != $wp_query->query_vars['year'] )
3876 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3877 if ( '' != $wp_query->query_vars['monthnum'] )
3878 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3879 if ( '' != $wp_query->query_vars['day'] )
3880 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3882 $id = (int) $wpdb->get_var($query);
3887 $link = get_permalink($id);
3892 wp_redirect( $link, 301 ); // Permanent redirect
3898 * Set up global post data.
3902 * @param object $post Post data.
3903 * @uses do_action_ref_array() Calls 'the_post'
3904 * @return bool True when finished.
3906 function setup_postdata( $post ) {
3907 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3909 $id = (int) $post->ID;
3911 $authordata = get_userdata($post->post_author);
3913 $currentday = mysql2date('d.m.y', $post->post_date, false);
3914 $currentmonth = mysql2date('m', $post->post_date, false);
3917 $page = get_query_var('page');
3920 if ( is_single() || is_page() || is_feed() )
3922 $content = $post->post_content;
3923 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
3926 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
3927 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
3928 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
3929 // Ignore nextpage at the beginning of the content.
3930 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
3931 $content = substr( $content, 15 );
3932 $pages = explode('<!--nextpage-->', $content);
3933 $numpages = count($pages);
3934 if ( $numpages > 1 )
3937 $pages = array( $post->post_content );
3940 do_action_ref_array('the_post', array(&$post));