5 * The query API attempts to get which part of WordPress to the user is on. It
6 * also provides functionality to getting URL query information.
8 * @link http://codex.wordpress.org/The_Loop More information on The Loop.
15 * Retrieve variable in the WP_Query class.
17 * @see WP_Query::get()
21 * @param string $var The variable key to retrieve.
24 function get_query_var($var) {
27 return $wp_query->get($var);
33 * @see WP_Query::set()
37 * @param string $var Query variable key.
41 function set_query_var($var, $value) {
44 return $wp_query->set($var, $value);
48 * Set up The Loop with query parameters.
50 * This will override the current WordPress Loop and shouldn't be used more than
51 * once. This must not be used within the WordPress Loop.
56 * @param string $query
57 * @return array List of posts
59 function &query_posts($query) {
60 unset($GLOBALS['wp_query']);
61 $GLOBALS['wp_query'] =& new WP_Query();
62 return $GLOBALS['wp_query']->query($query);
66 * Destroy the previous query and set up a new query.
68 * This should be used after {@link query_posts()} and before another {@link
69 * query_posts()}. This will remove obscure bugs that occur when the previous
70 * wp_query object is not destroyed properly before another is set up.
75 function wp_reset_query() {
76 unset($GLOBALS['wp_query']);
77 $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
82 * After looping through a separate query, this function restores
83 * the $post global to the current post in the main query
88 function wp_reset_postdata() {
90 if ( !empty($wp_query->post) ) {
91 $GLOBALS['post'] = $wp_query->post;
92 setup_postdata($wp_query->post);
101 * Is query requesting an archive page.
106 * @return bool True if page is archive.
108 function is_archive() {
111 return $wp_query->is_archive;
115 * Is query requesting an attachment page.
120 * @return bool True if page is attachment.
122 function is_attachment() {
125 return $wp_query->is_attachment;
129 * Is query requesting an author page.
131 * If the $author parameter is specified then the check will be expanded to
132 * include whether the queried author matches the one given in the parameter.
133 * You can match against integers and against strings.
135 * If matching against an integer, the ID should be used of the author for the
136 * test. If the $author is an ID and matches the author page user ID, then
137 * 'true' will be returned.
139 * If matching against strings, then the test will be matched against both the
140 * nickname and user nicename and will return true on success.
145 * @param string|int $author Optional. Is current page this author.
146 * @return bool True if page is author or $author (if set).
148 function is_author($author = '') {
151 if ( !$wp_query->is_author )
154 if ( empty($author) )
157 $author_obj = $wp_query->get_queried_object();
159 $author = (array) $author;
161 if ( in_array( $author_obj->ID, $author ) )
163 elseif ( in_array( $author_obj->nickname, $author ) )
165 elseif ( in_array( $author_obj->user_nicename, $author ) )
172 * Whether current page query contains a category name or given category name.
174 * The category list can contain category IDs, names, or category slugs. If any
175 * of them are part of the query, then it will return true.
180 * @param string|array $category Optional.
183 function is_category($category = '') {
186 if ( !$wp_query->is_category )
189 if ( empty($category) )
192 $cat_obj = $wp_query->get_queried_object();
194 $category = (array) $category;
196 if ( in_array( $cat_obj->term_id, $category ) )
198 elseif ( in_array( $cat_obj->name, $category ) )
200 elseif ( in_array( $cat_obj->slug, $category ) )
207 * Whether the current page query has the given tag slug or contains tag.
212 * @param string|array $slug Optional. Single tag or list of tags to check for.
215 function is_tag( $slug = '' ) {
218 if ( !$wp_query->is_tag )
221 if ( empty( $slug ) )
224 $tag_obj = $wp_query->get_queried_object();
226 $slug = (array) $slug;
228 if ( in_array( $tag_obj->slug, $slug ) )
235 * Whether the current query is for the given taxonomy and/or term.
237 * If no taxonomy argument is set, returns true if any taxonomy is queried.
238 * If the taxonomy argument is passed but no term argument, returns true
239 * if the taxonomy or taxonomies in the argument are being queried.
240 * If both taxonomy and term arguments are passed, returns true
241 * if the current query is for a term contained in the terms argument
242 * which has a taxonomy contained in the taxonomy argument.
247 * @param string|array $taxonomy Optional. Taxonomy slug or slugs to check in current query.
248 * @param int|array|string $term. Optional. A single or array of, The term's ID, Name or Slug
251 function is_tax( $taxonomy = '', $term = '' ) {
252 global $wp_query, $wp_taxonomies;
254 $queried_object = $wp_query->get_queried_object();
255 $tax_array = array_intersect(array_keys($wp_taxonomies), (array) $taxonomy);
256 $term_array = (array) $term;
258 if ( !$wp_query->is_tax )
261 if ( empty( $taxonomy ) )
264 if ( empty( $term ) ) // Only a Taxonomy provided
265 return isset($queried_object->taxonomy) && count( $tax_array ) && in_array($queried_object->taxonomy, $tax_array);
267 return isset($queried_object->term_id) &&
268 count(array_intersect(
269 array($queried_object->term_id, $queried_object->name, $queried_object->slug),
275 * Whether the current URL is within the comments popup window.
282 function is_comments_popup() {
285 return $wp_query->is_comments_popup;
289 * Whether current URL is based on a date.
299 return $wp_query->is_date;
303 * Whether current blog URL contains a day.
313 return $wp_query->is_day;
317 * Whether current page query is feed URL.
327 return $wp_query->is_feed;
331 * Whether current page query is comment feed URL.
338 function is_comment_feed() {
341 return $wp_query->is_comment_feed;
345 * Whether current page query is the front of the site.
351 * @return bool True, if front of site.
353 function is_front_page() {
355 if ( 'posts' == get_option('show_on_front') && is_home() )
357 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
364 * Whether current page view is the blog homepage.
366 * This is the page which is showing the time based blog content of your site
367 * so if you set a static page for the front page of your site then this will
368 * only be true on the page which you set as the "Posts page" in Reading Settings.
373 * @return bool True if blog view homepage.
378 return $wp_query->is_home;
382 * Whether current page query contains a month.
389 function is_month() {
392 return $wp_query->is_month;
396 * Whether query is page or contains given page(s).
398 * Calls the function without any parameters will only test whether the current
399 * query is of the page type. Either a list or a single item can be tested
400 * against for whether the query is a page and also is the value or one of the
401 * values in the page parameter.
403 * The parameter can contain the page ID, page title, or page name. The
404 * parameter can also be an array of those three values.
409 * @param mixed $page Either page or list of pages to test against.
412 function is_page($page = '') {
415 if ( !$wp_query->is_page )
421 $page_obj = $wp_query->get_queried_object();
423 $page = (array) $page;
425 if ( in_array( $page_obj->ID, $page ) )
427 elseif ( in_array( $page_obj->post_title, $page ) )
429 else if ( in_array( $page_obj->post_name, $page ) )
436 * Whether query contains multiple pages for the results.
443 function is_paged() {
446 return $wp_query->is_paged;
450 * Whether the current page was created by a plugin.
452 * The plugin can set this by using the global $plugin_page and setting it to
456 * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
460 function is_plugin_page() {
463 if ( isset($plugin_page) )
470 * Whether the current query is preview of post or page.
477 function is_preview() {
480 return $wp_query->is_preview;
484 * Whether the current query post is robots.
491 function is_robots() {
494 return $wp_query->is_robots;
498 * Whether current query is the result of a user search.
505 function is_search() {
508 return $wp_query->is_search;
512 * Whether the current page query is single page.
514 * The parameter can contain the post ID, post title, or post name. The
515 * parameter can also be an array of those three values.
517 * This applies to other post types, attachments, pages, posts. Just means that
518 * the current query has only a single object.
523 * @param mixed $post Either post or list of posts to test against.
526 function is_single($post = '') {
529 if ( !$wp_query->is_single )
535 $post_obj = $wp_query->get_queried_object();
537 $post = (array) $post;
539 if ( in_array( $post_obj->ID, $post ) )
541 elseif ( in_array( $post_obj->post_title, $post ) )
543 elseif ( in_array( $post_obj->post_name, $post ) )
550 * Whether is single post, is a page, or is an attachment.
555 * @param string|array $post_types Optional. Post type or types to check in current query.
558 function is_singular($post_types = '') {
561 if ( empty($post_types) || !$wp_query->is_singular )
562 return $wp_query->is_singular;
564 $post_obj = $wp_query->get_queried_object();
566 return in_array($post_obj->post_type, (array) $post_types);
570 * Whether the query contains a time.
580 return $wp_query->is_time;
584 * Whether the query is a trackback.
591 function is_trackback() {
594 return $wp_query->is_trackback;
598 * Whether the query contains a year.
608 return $wp_query->is_year;
612 * Whether current page query is a 404 and no results for WordPress query.
617 * @return bool True, if nothing is found matching WordPress Query.
622 return $wp_query->is_404;
626 * The Loop. Post loop control.
630 * Whether current WordPress query has results to loop over.
632 * @see WP_Query::have_posts()
638 function have_posts() {
641 return $wp_query->have_posts();
645 * Whether the caller is in the Loop.
650 * @return bool True if caller is within loop, false if loop hasn't started or ended.
652 function in_the_loop() {
655 return $wp_query->in_the_loop;
659 * Rewind the loop posts.
661 * @see WP_Query::rewind_posts()
667 function rewind_posts() {
670 return $wp_query->rewind_posts();
674 * Iterate the post index in the loop.
676 * @see WP_Query::the_post()
680 function the_post() {
683 $wp_query->the_post();
691 * Whether there are comments to loop over.
693 * @see WP_Query::have_comments()
699 function have_comments() {
701 return $wp_query->have_comments();
705 * Iterate comment index in the comment loop.
707 * @see WP_Query::the_comment()
713 function the_comment() {
715 return $wp_query->the_comment();
723 * The WordPress Query class.
725 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
741 * Query search variables set by the user.
747 var $query_vars = array();
750 * Holds the data for a single object that is queried.
752 * Holds the contents of a post, page, category, attachment.
761 * The ID of the queried object.
767 var $queried_object_id;
770 * Get post database query.
788 * The amount of posts for the current query.
797 * Index of the current item in the loop.
803 var $current_post = -1;
806 * Whether the loop has started and the caller is in the loop.
812 var $in_the_loop = false;
815 * The current post ID.
824 * The list of comments for current post.
833 * The amount of comments for the posts.
839 var $comment_count = 0;
842 * The index of the comment in the comment loop.
848 var $current_comment = -1;
851 * Current comment ID.
860 * Amount of posts if limit clause was not used.
866 var $found_posts = 0;
869 * The amount of pages.
875 var $max_num_pages = 0;
878 * The amount of comment pages.
884 var $max_num_comment_pages = 0;
887 * Set if query is single post.
893 var $is_single = false;
896 * Set if query is preview of blog.
902 var $is_preview = false;
905 * Set if query returns a page.
911 var $is_page = false;
914 * Set if query is an archive list.
920 var $is_archive = false;
923 * Set if query is part of a date.
929 var $is_date = false;
932 * Set if query contains a year.
938 var $is_year = false;
941 * Set if query contains a month.
947 var $is_month = false;
950 * Set if query contains a day.
959 * Set if query contains time.
965 var $is_time = false;
968 * Set if query contains an author.
974 var $is_author = false;
977 * Set if query contains category.
983 var $is_category = false;
986 * Set if query contains tag.
995 * Set if query contains taxonomy.
1001 var $is_tax = false;
1004 * Set if query was part of a search result.
1010 var $is_search = false;
1013 * Set if query is feed display.
1019 var $is_feed = false;
1022 * Set if query is comment feed display.
1028 var $is_comment_feed = false;
1031 * Set if query is trackback.
1037 var $is_trackback = false;
1040 * Set if query is blog homepage.
1046 var $is_home = false;
1049 * Set if query couldn't found anything.
1055 var $is_404 = false;
1058 * Set if query is within comments popup window.
1064 var $is_comments_popup = false;
1067 * Set if query is part of administration page.
1073 var $is_admin = false;
1076 * Set if query is an attachment.
1082 var $is_attachment = false;
1085 * Set if is single, is a page, or is an attachment.
1091 var $is_singular = false;
1094 * Set if query is for robots.
1100 var $is_robots = false;
1103 * Set if query contains posts.
1105 * Basically, the homepage if the option isn't set for the static homepage.
1111 var $is_posts_page = false;
1114 * Resets query flags to false.
1116 * The query flags are what page info WordPress was able to figure out.
1121 function init_query_flags() {
1122 $this->is_single = false;
1123 $this->is_page = false;
1124 $this->is_archive = false;
1125 $this->is_date = false;
1126 $this->is_year = false;
1127 $this->is_month = false;
1128 $this->is_day = false;
1129 $this->is_time = false;
1130 $this->is_author = false;
1131 $this->is_category = false;
1132 $this->is_tag = false;
1133 $this->is_tax = false;
1134 $this->is_search = false;
1135 $this->is_feed = false;
1136 $this->is_comment_feed = false;
1137 $this->is_trackback = false;
1138 $this->is_home = false;
1139 $this->is_404 = false;
1140 $this->is_paged = false;
1141 $this->is_admin = false;
1142 $this->is_attachment = false;
1143 $this->is_singular = false;
1144 $this->is_robots = false;
1145 $this->is_posts_page = false;
1149 * Initiates object properties and sets default values.
1155 unset($this->posts);
1156 unset($this->query);
1157 $this->query_vars = array();
1158 unset($this->queried_object);
1159 unset($this->queried_object_id);
1160 $this->post_count = 0;
1161 $this->current_post = -1;
1162 $this->in_the_loop = false;
1164 $this->init_query_flags();
1168 * Reparse the query vars.
1173 function parse_query_vars() {
1174 $this->parse_query('');
1178 * Fills in the query variables, which do not exist within the parameter.
1183 * @param array $array Defined query variables.
1184 * @return array Complete query variables with undefined ones filled in empty.
1186 function fill_query_vars($array) {
1223 foreach ( $keys as $key ) {
1224 if ( !isset($array[$key]))
1228 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1229 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1231 foreach ( $array_keys as $key ) {
1232 if ( !isset($array[$key]))
1233 $array[$key] = array();
1239 * Parse a query string and set query type booleans.
1244 * @param string|array $query
1246 function parse_query($query) {
1247 if ( !empty($query) || !isset($this->query) ) {
1249 if ( is_array($query) )
1250 $this->query_vars = $query;
1252 parse_str($query, $this->query_vars);
1253 $this->query = $query;
1256 $this->query_vars = $this->fill_query_vars($this->query_vars);
1257 $qv = &$this->query_vars;
1259 if ( ! empty($qv['robots']) )
1260 $this->is_robots = true;
1262 $qv['p'] = absint($qv['p']);
1263 $qv['page_id'] = absint($qv['page_id']);
1264 $qv['year'] = absint($qv['year']);
1265 $qv['monthnum'] = absint($qv['monthnum']);
1266 $qv['day'] = absint($qv['day']);
1267 $qv['w'] = absint($qv['w']);
1268 $qv['m'] = absint($qv['m']);
1269 $qv['paged'] = absint($qv['paged']);
1270 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1271 $qv['pagename'] = trim( $qv['pagename'] );
1272 $qv['name'] = trim( $qv['name'] );
1273 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1274 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1275 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1277 // Compat. Map subpost to attachment.
1278 if ( '' != $qv['subpost'] )
1279 $qv['attachment'] = $qv['subpost'];
1280 if ( '' != $qv['subpost_id'] )
1281 $qv['attachment_id'] = $qv['subpost_id'];
1283 $qv['attachment_id'] = absint($qv['attachment_id']);
1285 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1286 $this->is_single = true;
1287 $this->is_attachment = true;
1288 } elseif ( '' != $qv['name'] ) {
1289 $this->is_single = true;
1290 } elseif ( $qv['p'] ) {
1291 $this->is_single = true;
1292 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1293 // If year, month, day, hour, minute, and second are set, a single
1294 // post is being queried.
1295 $this->is_single = true;
1296 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1297 $this->is_page = true;
1298 $this->is_single = false;
1299 } elseif ( !empty($qv['s']) ) {
1300 $this->is_search = true;
1302 // Look for archive queries. Dates, categories, authors.
1304 if ( '' !== $qv['second'] ) {
1305 $this->is_time = true;
1306 $this->is_date = true;
1309 if ( '' !== $qv['minute'] ) {
1310 $this->is_time = true;
1311 $this->is_date = true;
1314 if ( '' !== $qv['hour'] ) {
1315 $this->is_time = true;
1316 $this->is_date = true;
1320 if ( ! $this->is_date ) {
1321 $this->is_day = true;
1322 $this->is_date = true;
1326 if ( $qv['monthnum'] ) {
1327 if ( ! $this->is_date ) {
1328 $this->is_month = true;
1329 $this->is_date = true;
1333 if ( $qv['year'] ) {
1334 if ( ! $this->is_date ) {
1335 $this->is_year = true;
1336 $this->is_date = true;
1341 $this->is_date = true;
1342 if ( strlen($qv['m']) > 9 ) {
1343 $this->is_time = true;
1344 } else if ( strlen($qv['m']) > 7 ) {
1345 $this->is_day = true;
1346 } else if ( strlen($qv['m']) > 5 ) {
1347 $this->is_month = true;
1349 $this->is_year = true;
1353 if ( '' != $qv['w'] ) {
1354 $this->is_date = true;
1357 if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
1358 $this->is_category = false;
1360 if ( strpos($qv['cat'], '-') !== false ) {
1361 $this->is_category = false;
1363 $this->is_category = true;
1367 if ( '' != $qv['category_name'] ) {
1368 $this->is_category = true;
1371 if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
1372 $qv['category__in'] = array();
1374 $qv['category__in'] = array_map('absint', $qv['category__in']);
1375 $this->is_category = true;
1378 if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
1379 $qv['category__not_in'] = array();
1381 $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
1384 if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
1385 $qv['category__and'] = array();
1387 $qv['category__and'] = array_map('absint', $qv['category__and']);
1388 $this->is_category = true;
1391 if ( '' != $qv['tag'] )
1392 $this->is_tag = true;
1394 $qv['tag_id'] = absint($qv['tag_id']);
1395 if ( !empty($qv['tag_id']) )
1396 $this->is_tag = true;
1398 if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
1399 $qv['tag__in'] = array();
1401 $qv['tag__in'] = array_map('absint', $qv['tag__in']);
1402 $this->is_tag = true;
1405 if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
1406 $qv['tag__not_in'] = array();
1408 $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
1411 if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
1412 $qv['tag__and'] = array();
1414 $qv['tag__and'] = array_map('absint', $qv['tag__and']);
1415 $this->is_category = true;
1418 if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
1419 $qv['tag_slug__in'] = array();
1421 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
1422 $this->is_tag = true;
1425 if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
1426 $qv['tag_slug__and'] = array();
1428 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
1429 $this->is_tag = true;
1432 if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
1433 $this->is_tax = false;
1434 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1435 if ( $t->query_var && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
1436 $qv['taxonomy'] = $taxonomy;
1437 $qv['term'] = $qv[$t->query_var];
1438 $this->is_tax = true;
1443 $this->is_tax = true;
1446 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1447 $this->is_author = false;
1449 $this->is_author = true;
1452 if ( '' != $qv['author_name'] ) {
1453 $this->is_author = true;
1456 if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
1457 $this->is_archive = true;
1460 if ( '' != $qv['feed'] )
1461 $this->is_feed = true;
1463 if ( '' != $qv['tb'] )
1464 $this->is_trackback = true;
1466 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1467 $this->is_paged = true;
1469 if ( '' != $qv['comments_popup'] )
1470 $this->is_comments_popup = true;
1472 // if we're previewing inside the write screen
1473 if ( '' != $qv['preview'] )
1474 $this->is_preview = true;
1477 $this->is_admin = true;
1479 if ( false !== strpos($qv['feed'], 'comments-') ) {
1480 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1481 $qv['withcomments'] = 1;
1484 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1486 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1487 $this->is_comment_feed = true;
1489 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 ) )
1490 $this->is_home = true;
1492 // Correct is_* for page_on_front and page_for_posts
1493 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1494 $_query = wp_parse_args($query);
1495 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1496 $this->is_page = true;
1497 $this->is_home = false;
1498 $qv['page_id'] = get_option('page_on_front');
1499 // Correct <!--nextpage--> for page_on_front
1500 if ( !empty($qv['paged']) ) {
1501 $qv['page'] = $qv['paged'];
1502 unset($qv['paged']);
1507 if ( '' != $qv['pagename'] ) {
1508 $this->queried_object =& get_page_by_path($qv['pagename']);
1509 if ( !empty($this->queried_object) )
1510 $this->queried_object_id = (int) $this->queried_object->ID;
1512 unset($this->queried_object);
1514 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1515 $this->is_page = false;
1516 $this->is_home = true;
1517 $this->is_posts_page = true;
1521 if ( $qv['page_id'] ) {
1522 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1523 $this->is_page = false;
1524 $this->is_home = true;
1525 $this->is_posts_page = true;
1529 if ( !empty($qv['post_type']) ) {
1530 if ( is_array($qv['post_type']) )
1531 $qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true));
1533 $qv['post_type'] = sanitize_user($qv['post_type'], true);
1536 if ( !empty($qv['post_status']) )
1537 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1539 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1540 $this->is_comment_feed = false;
1542 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1543 // Done correcting is_* for page_on_front and page_for_posts
1545 if ( '404' == $qv['error'] )
1548 if ( !empty($query) )
1549 do_action_ref_array('parse_query', array(&$this));
1553 * Sets the 404 property and saves whether query is feed.
1558 function set_404() {
1559 $is_feed = $this->is_feed;
1561 $this->init_query_flags();
1562 $this->is_404 = true;
1564 $this->is_feed = $is_feed;
1568 * Retrieve query variable.
1573 * @param string $query_var Query variable key.
1576 function get($query_var) {
1577 if ( isset($this->query_vars[$query_var]) )
1578 return $this->query_vars[$query_var];
1584 * Set query variable.
1589 * @param string $query_var Query variable key.
1590 * @param mixed $value Query variable value.
1592 function set($query_var, $value) {
1593 $this->query_vars[$query_var] = $value;
1597 * Retrieve the posts based on query variables.
1599 * There are a few filters and actions that can be used to modify the post
1604 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1606 * @return array List of posts.
1608 function &get_posts() {
1609 global $wpdb, $user_ID, $_wp_using_ext_object_cache;
1611 do_action_ref_array('pre_get_posts', array(&$this));
1614 $q = &$this->query_vars;
1616 $q = $this->fill_query_vars($q);
1618 // First let's clear some variables
1622 $whichmimetype = '';
1628 $fields = "$wpdb->posts.*";
1629 $post_status_join = false;
1632 if ( !isset($q['caller_get_posts']) )
1633 $q['caller_get_posts'] = false;
1635 if ( !isset($q['suppress_filters']) )
1636 $q['suppress_filters'] = false;
1638 if ( !isset($q['cache_results']) ) {
1639 if ( $_wp_using_ext_object_cache )
1640 $q['cache_results'] = false;
1642 $q['cache_results'] = true;
1645 if ( !isset($q['update_post_term_cache']) )
1646 $q['update_post_term_cache'] = true;
1648 if ( !isset($q['update_post_meta_cache']) )
1649 $q['update_post_meta_cache'] = true;
1651 if ( !isset($q['post_type']) ) {
1652 if ( $this->is_search )
1653 $q['post_type'] = 'any';
1655 $q['post_type'] = '';
1657 $post_type = $q['post_type'];
1658 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1659 $q['posts_per_page'] = get_option('posts_per_page');
1660 if ( isset($q['showposts']) && $q['showposts'] ) {
1661 $q['showposts'] = (int) $q['showposts'];
1662 $q['posts_per_page'] = $q['showposts'];
1664 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1665 $q['posts_per_page'] = $q['posts_per_archive_page'];
1666 if ( !isset($q['nopaging']) ) {
1667 if ( $q['posts_per_page'] == -1 ) {
1668 $q['nopaging'] = true;
1670 $q['nopaging'] = false;
1673 if ( $this->is_feed ) {
1674 $q['posts_per_page'] = get_option('posts_per_rss');
1675 $q['nopaging'] = false;
1677 $q['posts_per_page'] = (int) $q['posts_per_page'];
1678 if ( $q['posts_per_page'] < -1 )
1679 $q['posts_per_page'] = abs($q['posts_per_page']);
1680 else if ( $q['posts_per_page'] == 0 )
1681 $q['posts_per_page'] = 1;
1683 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
1684 $q['comments_per_page'] = get_option('comments_per_page');
1686 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
1687 $this->is_page = true;
1688 $this->is_home = false;
1689 $q['page_id'] = get_option('page_on_front');
1692 if ( isset($q['page']) ) {
1693 $q['page'] = trim($q['page'], '/');
1694 $q['page'] = absint($q['page']);
1697 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
1698 if ( isset($q['no_found_rows']) )
1699 $q['no_found_rows'] = (bool) $q['no_found_rows'];
1701 $q['no_found_rows'] = false;
1703 // If a month is specified in the querystring, load that month
1705 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
1706 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
1707 if ( strlen($q['m']) > 5 )
1708 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
1709 if ( strlen($q['m']) > 7 )
1710 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
1711 if ( strlen($q['m']) > 9 )
1712 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
1713 if ( strlen($q['m']) > 11 )
1714 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
1715 if ( strlen($q['m']) > 13 )
1716 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
1719 if ( '' !== $q['hour'] )
1720 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
1722 if ( '' !== $q['minute'] )
1723 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
1725 if ( '' !== $q['second'] )
1726 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
1729 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
1731 if ( $q['monthnum'] )
1732 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
1735 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
1737 // If we've got a post_type AND its not "any" post_type.
1738 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
1739 foreach ( (array)$q['post_type'] as $_post_type ) {
1740 $ptype_obj = get_post_type_object($_post_type);
1741 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
1744 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
1745 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
1746 $q['name'] = $q[ $ptype_obj->query_var ];
1748 // Hierarchical post_types will operate through the
1749 $q['pagename'] = $q[ $ptype_obj->query_var ];
1753 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
1759 if ( '' != $q['name'] ) {
1760 $q['name'] = sanitize_title($q['name']);
1761 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
1762 } elseif ( '' != $q['pagename'] ) {
1763 if ( isset($this->queried_object_id) ) {
1764 $reqpage = $this->queried_object_id;
1766 if ( 'page' != $q['post_type'] ) {
1767 foreach ( (array)$q['post_type'] as $_post_type ) {
1768 $ptype_obj = get_post_type_object($_post_type);
1769 if ( !$ptype_obj || !$ptype_obj->hierarchical )
1772 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
1778 $reqpage = get_page_by_path($q['pagename']);
1780 if ( !empty($reqpage) )
1781 $reqpage = $reqpage->ID;
1786 $page_for_posts = get_option('page_for_posts');
1787 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
1788 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
1789 $page_paths = '/' . trim($q['pagename'], '/');
1790 $q['pagename'] = sanitize_title(basename($page_paths));
1791 $q['name'] = $q['pagename'];
1792 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
1793 $reqpage_obj = get_page($reqpage);
1794 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
1795 $this->is_attachment = true;
1796 $post_type = $q['post_type'] = 'attachment';
1797 $this->is_page = true;
1798 $q['attachment_id'] = $reqpage;
1801 } elseif ( '' != $q['attachment'] ) {
1802 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
1803 $attach_paths = '/' . trim($q['attachment'], '/');
1804 $q['attachment'] = sanitize_title(basename($attach_paths));
1805 $q['name'] = $q['attachment'];
1806 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
1810 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
1812 if ( intval($q['comments_popup']) )
1813 $q['p'] = absint($q['comments_popup']);
1815 // If an attachment is requested by number, let it supercede any post number.
1816 if ( $q['attachment_id'] )
1817 $q['p'] = absint($q['attachment_id']);
1819 // If a post number is specified, load that post
1821 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
1822 } elseif ( $q['post__in'] ) {
1823 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
1824 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
1825 } elseif ( $q['post__not_in'] ) {
1826 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
1827 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
1830 if ( is_numeric($q['post_parent']) )
1831 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
1833 if ( $q['page_id'] ) {
1834 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
1835 $q['p'] = $q['page_id'];
1836 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
1840 // If a search pattern is specified, load the posts that match
1841 if ( !empty($q['s']) ) {
1842 // added slashes screw with quote grouping when done early, so done later
1843 $q['s'] = stripslashes($q['s']);
1844 if ( !empty($q['sentence']) ) {
1845 $q['search_terms'] = array($q['s']);
1847 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
1848 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
1850 $n = !empty($q['exact']) ? '' : '%';
1852 foreach( (array) $q['search_terms'] as $term ) {
1853 $term = addslashes_gpc($term);
1854 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1855 $searchand = ' AND ';
1857 $term = esc_sql($q['s']);
1858 if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
1859 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
1861 if ( !empty($search) ) {
1862 $search = " AND ({$search}) ";
1863 if ( !is_user_logged_in() )
1864 $search .= " AND ($wpdb->posts.post_password = '') ";
1868 // Allow plugins to contextually add/remove/modify the search section of the database query
1869 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
1873 if ( empty($q['cat']) || ($q['cat'] == '0') ||
1874 // Bypass cat checks if fetching specific posts
1875 $this->is_singular ) {
1878 $q['cat'] = ''.urldecode($q['cat']).'';
1879 $q['cat'] = addslashes_gpc($q['cat']);
1880 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1882 $req_cats = array();
1883 foreach ( (array) $cat_array as $cat ) {
1884 $cat = intval($cat);
1889 $q['category__in'][] = $cat;
1890 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
1892 $q['category__not_in'][] = $cat;
1893 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
1896 $q['cat'] = implode(',', $req_cats);
1899 if ( !empty($q['category__in']) ) {
1900 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1901 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1902 $include_cats = "'" . implode("', '", $q['category__in']) . "'";
1903 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
1906 if ( !empty($q['category__not_in']) ) {
1907 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
1908 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ($cat_string) )";
1911 // Category stuff for nice URLs
1912 if ( '' != $q['category_name'] && !$this->is_singular ) {
1913 $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
1914 $reqcat = get_category_by_path($q['category_name']);
1915 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
1916 $cat_paths = '/' . trim($q['category_name'], '/');
1917 $q['category_name'] = sanitize_title(basename($cat_paths));
1919 $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
1920 $q['category_name'] = sanitize_title(basename($cat_paths));
1921 $cat_paths = explode('/', $cat_paths);
1923 foreach ( (array) $cat_paths as $pathdir )
1924 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
1926 //if we don't match the entire hierarchy fallback on just matching the nicename
1927 if ( empty($reqcat) )
1928 $reqcat = get_category_by_path($q['category_name'], false);
1930 if ( !empty($reqcat) )
1931 $reqcat = $reqcat->term_id;
1935 $q['cat'] = $reqcat;
1937 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1938 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1939 $in_cats = array($q['cat']);
1940 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
1941 $in_cats = "'" . implode("', '", $in_cats) . "'";
1942 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
1943 $groupby = "{$wpdb->posts}.ID";
1947 if ( '' != $q['tag'] ) {
1948 if ( strpos($q['tag'], ',') !== false ) {
1949 $tags = preg_split('/[,\s]+/', $q['tag']);
1950 foreach ( (array) $tags as $tag ) {
1951 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1952 $q['tag_slug__in'][] = $tag;
1954 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1955 $tags = preg_split('/[+\s]+/', $q['tag']);
1956 foreach ( (array) $tags as $tag ) {
1957 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1958 $q['tag_slug__and'][] = $tag;
1961 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1962 $q['tag_slug__in'][] = $q['tag'];
1966 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
1967 $groupby = "{$wpdb->posts}.ID";
1970 if ( !empty($q['tag__in']) && empty($q['cat']) ) {
1971 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1972 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1973 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1974 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1975 $reqtag = term_exists( $q['tag__in'][0], 'post_tag' );
1976 if ( !empty($reqtag) )
1977 $q['tag_id'] = $reqtag['term_id'];
1980 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
1981 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
1982 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1983 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1984 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1985 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
1986 if ( !empty($reqtag) )
1987 $q['tag_id'] = $reqtag->term_id;
1990 if ( !empty($q['tag__not_in']) ) {
1991 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
1992 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ($tag_string) )";
1995 // Tag and slug intersections.
1996 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
1997 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
1998 foreach ( $intersections as $item => $taxonomy ) {
1999 if ( empty($q[$item]) ) continue;
2000 if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
2002 if ( $item != 'category__and' ) {
2003 $reqtag = term_exists( $q[$item][0], 'post_tag' );
2004 if ( !empty($reqtag) )
2005 $q['tag_id'] = $reqtag['term_id'];
2008 if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
2009 $taxonomy_field = 'slug';
2011 $taxonomy_field = 'term_id';
2013 $q[$item] = array_unique($q[$item]);
2014 $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
2015 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
2016 if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
2017 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
2019 $post_ids = $wpdb->get_col($tsql);
2021 if ( count($post_ids) )
2022 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
2024 $whichcat = " AND 0 = 1";
2030 if ( $this->is_tax ) {
2031 if ( '' != $q['taxonomy'] ) {
2032 $taxonomy = $q['taxonomy'];
2033 $tt[$taxonomy] = $q['term'];
2035 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
2036 if ( $t->query_var && '' != $q[$t->query_var] ) {
2037 $tt[$taxonomy] = $q[$t->query_var];
2043 $terms = get_terms($taxonomy, array('slug' => $tt[$taxonomy], 'hide_empty' => !is_taxonomy_hierarchical($taxonomy)));
2045 if ( is_wp_error($terms) || empty($terms) ) {
2046 $whichcat = " AND 0 ";
2048 foreach ( $terms as $term ) {
2049 $term_ids[] = $term->term_id;
2050 if ( is_taxonomy_hierarchical($taxonomy) ) {
2051 $children = get_term_children($term->term_id, $taxonomy);
2052 $term_ids = array_merge($term_ids, $children);
2055 $post_ids = get_objects_in_term($term_ids, $taxonomy);
2056 if ( !is_wp_error($post_ids) && !empty($post_ids) ) {
2057 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
2058 if ( empty($post_type) ) {
2060 $post_status_join = true;
2061 } elseif ( in_array('attachment', (array)$post_type) ) {
2062 $post_status_join = true;
2065 $whichcat = " AND 0 ";
2070 // Author/user stuff
2072 if ( empty($q['author']) || ($q['author'] == '0') ) {
2075 $q['author'] = (string)urldecode($q['author']);
2076 $q['author'] = addslashes_gpc($q['author']);
2077 if ( strpos($q['author'], '-') !== false ) {
2080 $q['author'] = explode('-', $q['author']);
2081 $q['author'] = (string)absint($q['author'][1]);
2086 $author_array = preg_split('/[,\s]+/', $q['author']);
2087 $_author_array = array();
2088 foreach ( $author_array as $key => $_author )
2089 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
2090 $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
2091 unset($author_array, $_author_array);
2094 // Author stuff for nice URLs
2096 if ( '' != $q['author_name'] ) {
2097 if ( strpos($q['author_name'], '/') !== false ) {
2098 $q['author_name'] = explode('/', $q['author_name']);
2099 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2100 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2102 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
2105 $q['author_name'] = sanitize_title($q['author_name']);
2106 $q['author'] = get_user_by('slug', $q['author_name']);
2108 $q['author'] = $q['author']->ID;
2109 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2112 // MIME-Type stuff for attachment browsing
2114 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
2115 $table_alias = $post_status_join ? $wpdb->posts : '';
2116 $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
2119 $where .= $search . $whichcat . $whichauthor . $whichmimetype;
2121 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2122 $q['order'] = 'DESC';
2125 if ( empty($q['orderby']) ) {
2126 $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
2127 } elseif ( 'none' == $q['orderby'] ) {
2130 // Used to filter values
2131 $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2132 if ( !empty($q['meta_key']) ) {
2133 $allowed_keys[] = $q['meta_key'];
2134 $allowed_keys[] = 'meta_value';
2135 $allowed_keys[] = 'meta_value_num';
2137 $q['orderby'] = urldecode($q['orderby']);
2138 $q['orderby'] = addslashes_gpc($q['orderby']);
2139 $orderby_array = explode(' ', $q['orderby']);
2142 foreach ( $orderby_array as $i => $orderby ) {
2143 // Only allow certain values for safety
2144 if ( ! in_array($orderby, $allowed_keys) )
2147 switch ( $orderby ) {
2151 $orderby = "$wpdb->posts.ID";
2154 $orderby = 'RAND()';
2156 case $q['meta_key']:
2158 $orderby = "$wpdb->postmeta.meta_value";
2160 case 'meta_value_num':
2161 $orderby = "$wpdb->postmeta.meta_value+0";
2163 case 'comment_count':
2164 $orderby = "$wpdb->posts.comment_count";
2167 $orderby = "$wpdb->posts.post_" . $orderby;
2170 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
2173 // append ASC or DESC at the end
2174 if ( !empty($q['orderby']))
2175 $q['orderby'] .= " {$q['order']}";
2177 if ( empty($q['orderby']) )
2178 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
2181 if ( is_array($post_type) ) {
2182 $post_type_cap = 'multiple_post_type';
2184 $post_type_object = get_post_type_object ( $post_type );
2185 if ( !empty($post_type_object) )
2186 $post_type_cap = $post_type_object->capability_type;
2188 $post_type_cap = $post_type;
2191 $exclude_post_types = '';
2192 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2193 if ( ! empty( $in_search_post_types ) )
2194 $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
2196 if ( 'any' == $post_type ) {
2197 $where .= $exclude_post_types;
2198 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2199 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2200 } elseif ( ! empty( $post_type ) ) {
2201 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2202 $post_type_object = get_post_type_object ( $post_type );
2203 } elseif ( $this->is_attachment ) {
2204 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2205 $post_type_object = get_post_type_object ( 'attachment' );
2206 } elseif ( $this->is_page ) {
2207 $where .= " AND $wpdb->posts.post_type = 'page'";
2208 $post_type_object = get_post_type_object ( 'page' );
2210 $where .= " AND $wpdb->posts.post_type = 'post'";
2211 $post_type_object = get_post_type_object ( 'post' );
2214 if ( !empty($post_type_object) ) {
2215 $post_type_cap = $post_type_object->capability_type;
2216 $edit_cap = $post_type_object->cap->edit_post;
2217 $read_cap = $post_type_object->cap->read_post;
2218 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2219 $read_private_cap = $post_type_object->cap->read_private_posts;
2221 $edit_cap = 'edit_' . $post_type_cap;
2222 $read_cap = 'read_' . $post_type_cap;
2223 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2224 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2227 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
2228 $statuswheres = array();
2229 $q_status = explode(',', $q['post_status']);
2230 $r_status = array();
2231 $p_status = array();
2232 $e_status = array();
2233 if ( $q['post_status'] == 'any' ) {
2234 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2235 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2237 foreach ( get_post_stati() as $status ) {
2238 if ( in_array( $status, $q_status ) ) {
2239 if ( 'private' == $status )
2240 $p_status[] = "$wpdb->posts.post_status = '$status'";
2242 $r_status[] = "$wpdb->posts.post_status = '$status'";
2247 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2248 $r_status = array_merge($r_status, $p_status);
2252 if ( !empty($e_status) ) {
2253 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2255 if ( !empty($r_status) ) {
2256 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2257 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2259 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2261 if ( !empty($p_status) ) {
2262 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2263 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2265 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2267 if ( $post_status_join ) {
2268 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2269 foreach ( $statuswheres as $index => $statuswhere )
2270 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2272 foreach ( $statuswheres as $statuswhere )
2273 $where .= " AND $statuswhere";
2274 } elseif ( !$this->is_singular ) {
2275 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2277 // Add public states.
2278 $public_states = get_post_stati( array('public' => true) );
2279 foreach ( (array) $public_states as $state ) {
2280 if ( 'publish' == $state ) // Publish is hard-coded above.
2282 $where .= " OR $wpdb->posts.post_status = '$state'";
2286 // Add protected states that should show in the admin all list.
2287 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2288 foreach ( (array) $admin_all_states as $state )
2289 $where .= " OR $wpdb->posts.post_status = '$state'";
2292 if ( is_user_logged_in() ) {
2293 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2294 $private_states = get_post_stati( array('private' => true) );
2295 foreach ( (array) $private_states as $state )
2296 $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'";
2303 if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
2304 $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
2305 if ( ! empty($q['meta_key']) )
2306 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
2307 if ( ! empty($q['meta_value']) ) {
2308 if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
2309 $q['meta_compare'] = '=';
2311 $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
2314 // Apply filters on where and join prior to paging so that any
2315 // manipulations to them are reflected in the paging by day queries.
2316 if ( !$q['suppress_filters'] ) {
2317 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2318 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2322 if ( empty($q['nopaging']) && !$this->is_singular ) {
2323 $page = absint($q['paged']);
2327 if ( empty($q['offset']) ) {
2329 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2330 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2331 } else { // we're ignoring $page and using 'offset'
2332 $q['offset'] = absint($q['offset']);
2333 $pgstrt = $q['offset'] . ', ';
2334 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2339 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2340 if ( $this->is_archive || $this->is_search ) {
2341 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2342 $cwhere = "WHERE comment_approved = '1' $where";
2343 $cgroupby = "$wpdb->comments.comment_id";
2344 } else { // Other non singular e.g. front
2345 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2346 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2350 if ( !$q['suppress_filters'] ) {
2351 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2352 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2353 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2354 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2355 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2357 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2358 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2360 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2361 $this->comment_count = count($this->comments);
2363 $post_ids = array();
2365 foreach ( $this->comments as $comment )
2366 $post_ids[] = (int) $comment->comment_post_ID;
2368 $post_ids = join(',', $post_ids);
2371 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2376 $orderby = $q['orderby'];
2378 // Apply post-paging filters on where and join. Only plugins that
2379 // manipulate paging queries should use these hooks.
2380 if ( !$q['suppress_filters'] ) {
2381 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2382 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2383 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2384 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2385 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2386 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2387 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2390 // Announce current selection parameters. For use by caching plugins.
2391 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2393 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2394 if ( !$q['suppress_filters'] ) {
2395 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2396 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2397 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2398 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2399 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2400 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2401 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2404 if ( ! empty($groupby) )
2405 $groupby = 'GROUP BY ' . $groupby;
2406 if ( !empty( $orderby ) )
2407 $orderby = 'ORDER BY ' . $orderby;
2409 if ( !$q['no_found_rows'] && !empty($limits) )
2410 $found_rows = 'SQL_CALC_FOUND_ROWS';
2412 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2413 if ( !$q['suppress_filters'] )
2414 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
2416 $this->posts = $wpdb->get_results($this->request);
2417 // Raw results filter. Prior to status checks.
2418 if ( !$q['suppress_filters'] )
2419 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2421 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2422 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2423 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2424 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2425 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2426 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2427 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2428 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2429 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2430 $this->comments = $wpdb->get_results($comments_request);
2431 $this->comment_count = count($this->comments);
2434 if ( !$q['no_found_rows'] && !empty($limits) ) {
2435 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
2436 $this->found_posts = $wpdb->get_var( $found_posts_query );
2437 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
2438 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
2441 // Check post status to determine if post should be displayed.
2442 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2443 $status = get_post_status($this->posts[0]);
2444 $post_status_obj = get_post_status_object($status);
2445 //$type = get_post_type($this->posts[0]);
2446 if ( !$post_status_obj->public ) {
2447 if ( ! is_user_logged_in() ) {
2448 // User must be logged in to view unpublished posts.
2449 $this->posts = array();
2451 if ( $post_status_obj->protected ) {
2452 // User must have edit permissions on the draft to preview.
2453 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2454 $this->posts = array();
2456 $this->is_preview = true;
2457 if ( 'future' != $status )
2458 $this->posts[0]->post_date = current_time('mysql');
2460 } elseif ( $post_status_obj->private ) {
2461 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2462 $this->posts = array();
2464 $this->posts = array();
2469 if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2470 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
2473 // Put sticky posts at the top of the posts array
2474 $sticky_posts = get_option('sticky_posts');
2475 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
2476 $num_posts = count($this->posts);
2478 // Loop over posts and relocate stickies to the front.
2479 for ( $i = 0; $i < $num_posts; $i++ ) {
2480 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2481 $sticky_post = $this->posts[$i];
2482 // Remove sticky from current position
2483 array_splice($this->posts, $i, 1);
2484 // Move to front, after other stickies
2485 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2486 // Increment the sticky offset. The next sticky will be placed at this offset.
2488 // Remove post from sticky posts array
2489 $offset = array_search($sticky_post->ID, $sticky_posts);
2490 unset( $sticky_posts[$offset] );
2494 // If any posts have been excluded specifically, Ignore those that are sticky.
2495 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
2496 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
2498 // Fetch sticky posts that weren't in the query results
2499 if ( !empty($sticky_posts) ) {
2500 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2501 // honor post type(s) if not set to any
2502 $stickies_where = '';
2503 if ( 'any' != $post_type && '' != $post_type ) {
2504 if ( is_array( $post_type ) ) {
2505 $post_types = join( "', '", $post_type );
2507 $post_types = $post_type;
2509 $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
2512 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
2513 foreach ( $stickies as $sticky_post ) {
2514 // Ignore sticky posts the current user cannot read or are not published.
2515 if ( 'publish' != $sticky_post->post_status )
2517 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2523 if ( !$q['suppress_filters'] )
2524 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
2526 $this->post_count = count($this->posts);
2528 // Sanitize before caching so it'll only get done once
2529 for ( $i = 0; $i < $this->post_count; $i++ ) {
2530 $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
2533 if ( $q['cache_results'] )
2534 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
2536 if ( $this->post_count > 0 ) {
2537 $this->post = $this->posts[0];
2540 return $this->posts;
2544 * Set up the next post and iterate current post index.
2549 * @return object Next post.
2551 function next_post() {
2553 $this->current_post++;
2555 $this->post = $this->posts[$this->current_post];
2560 * Sets up the current post.
2562 * Retrieves the next post, sets up the post, sets the 'in the loop'
2568 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2570 function the_post() {
2572 $this->in_the_loop = true;
2574 if ( $this->current_post == -1 ) // loop has just started
2575 do_action_ref_array('loop_start', array(&$this));
2577 $post = $this->next_post();
2578 setup_postdata($post);
2582 * Whether there are more posts available in the loop.
2584 * Calls action 'loop_end', when the loop is complete.
2588 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2590 * @return bool True if posts are available, false if end of loop.
2592 function have_posts() {
2593 if ( $this->current_post + 1 < $this->post_count ) {
2595 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
2596 do_action_ref_array('loop_end', array(&$this));
2597 // Do some cleaning up after the loop
2598 $this->rewind_posts();
2601 $this->in_the_loop = false;
2606 * Rewind the posts and reset post index.
2611 function rewind_posts() {
2612 $this->current_post = -1;
2613 if ( $this->post_count > 0 ) {
2614 $this->post = $this->posts[0];
2619 * Iterate current comment index and return comment object.
2624 * @return object Comment object.
2626 function next_comment() {
2627 $this->current_comment++;
2629 $this->comment = $this->comments[$this->current_comment];
2630 return $this->comment;
2634 * Sets up the current comment.
2638 * @global object $comment Current comment.
2639 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2641 function the_comment() {
2644 $comment = $this->next_comment();
2646 if ( $this->current_comment == 0 ) {
2647 do_action('comment_loop_start');
2652 * Whether there are more comments available.
2654 * Automatically rewinds comments when finished.
2659 * @return bool True, if more comments. False, if no more posts.
2661 function have_comments() {
2662 if ( $this->current_comment + 1 < $this->comment_count ) {
2664 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
2665 $this->rewind_comments();
2672 * Rewind the comments, resets the comment index and comment to first.
2677 function rewind_comments() {
2678 $this->current_comment = -1;
2679 if ( $this->comment_count > 0 ) {
2680 $this->comment = $this->comments[0];
2685 * Sets up the WordPress query by parsing query string.
2690 * @param string $query URL query string.
2691 * @return array List of posts.
2693 function &query($query) {
2694 $this->parse_query($query);
2695 return $this->get_posts();
2699 * Retrieve queried object.
2701 * If queried object is not set, then the queried object will be set from
2702 * the category, tag, taxonomy, posts page, single post, page, or author
2703 * query variable. After it is set up, it will be returned.
2710 function get_queried_object() {
2711 if ( isset($this->queried_object) )
2712 return $this->queried_object;
2714 $this->queried_object = NULL;
2715 $this->queried_object_id = 0;
2717 if ( $this->is_category ) {
2718 $cat = $this->get('cat');
2719 $category = &get_category($cat);
2720 if ( is_wp_error( $category ) )
2722 $this->queried_object = &$category;
2723 $this->queried_object_id = (int) $cat;
2724 } elseif ( $this->is_tag ) {
2725 $tag_id = $this->get('tag_id');
2726 $tag = &get_term($tag_id, 'post_tag');
2727 if ( is_wp_error( $tag ) )
2729 $this->queried_object = &$tag;
2730 $this->queried_object_id = (int) $tag_id;
2731 } elseif ( $this->is_tax ) {
2732 $tax = $this->get('taxonomy');
2733 $slug = $this->get('term');
2734 $term = &get_terms($tax, array( 'slug' => $slug, 'hide_empty' => false ) );
2735 if ( is_wp_error($term) || empty($term) )
2738 $this->queried_object = $term;
2739 $this->queried_object_id = $term->term_id;
2740 } elseif ( $this->is_posts_page ) {
2741 $page_for_posts = get_option('page_for_posts');
2742 $this->queried_object = & get_page( $page_for_posts );
2743 $this->queried_object_id = (int) $this->queried_object->ID;
2744 } elseif ( $this->is_single && !is_null($this->post) ) {
2745 $this->queried_object = $this->post;
2746 $this->queried_object_id = (int) $this->post->ID;
2747 } elseif ( $this->is_page && !is_null($this->post) ) {
2748 $this->queried_object = $this->post;
2749 $this->queried_object_id = (int) $this->post->ID;
2750 } elseif ( $this->is_author ) {
2751 $author_id = (int) $this->get('author');
2752 $author = get_userdata($author_id);
2753 $this->queried_object = $author;
2754 $this->queried_object_id = $author_id;
2757 return $this->queried_object;
2761 * Retrieve ID of the current queried object.
2768 function get_queried_object_id() {
2769 $this->get_queried_object();
2771 if ( isset($this->queried_object_id) ) {
2772 return $this->queried_object_id;
2779 * PHP4 type constructor.
2781 * Sets up the WordPress query, if parameter is not empty.
2786 * @param string $query URL query string.
2789 function WP_Query($query = '') {
2790 if ( ! empty($query) ) {
2791 $this->query($query);
2797 * Redirect old slugs to the correct permalink.
2799 * Attempts to find the current slug from the past slugs.
2805 * @return null If no link is found, null is returned.
2807 function wp_old_slug_redirect() {
2809 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
2812 $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'";
2814 // if year, monthnum, or day have been specified, make our query more precise
2815 // just in case there are multiple identical _wp_old_slug values
2816 if ( '' != $wp_query->query_vars['year'] )
2817 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
2818 if ( '' != $wp_query->query_vars['monthnum'] )
2819 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
2820 if ( '' != $wp_query->query_vars['day'] )
2821 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
2823 $id = (int) $wpdb->get_var($query);
2828 $link = get_permalink($id);
2833 wp_redirect($link, '301'); // Permanent redirect
2839 * Set up global post data.
2843 * @param object $post Post data.
2844 * @uses do_action_ref_array() Calls 'the_post'
2845 * @return bool True when finished.
2847 function setup_postdata($post) {
2848 global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
2850 $id = (int) $post->ID;
2852 $authordata = get_userdata($post->post_author);
2854 $day = mysql2date('d.m.y', $post->post_date, false);
2855 $currentmonth = mysql2date('m', $post->post_date, false);
2857 $page = get_query_var('page');
2860 if ( is_single() || is_page() || is_feed() )
2862 $content = $post->post_content;
2863 if ( strpos( $content, '<!--nextpage-->' ) ) {
2867 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
2868 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
2869 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
2870 $pages = explode('<!--nextpage-->', $content);
2871 $numpages = count($pages);
2873 $pages = array( $post->post_content );
2877 do_action_ref_array('the_post', array(&$post));