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 * Setup 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 setup 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 setup.
75 function wp_reset_query() {
76 unset($GLOBALS['wp_query']);
77 $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
79 if ( !empty($wp_query->post) ) {
80 $GLOBALS['post'] = $wp_query->post;
81 setup_postdata($wp_query->post);
90 * Is query requesting an archive page.
95 * @return bool True if page is archive.
97 function is_archive () {
100 return $wp_query->is_archive;
104 * Is query requesting an attachment page.
109 * @return bool True if page is attachment.
111 function is_attachment () {
114 return $wp_query->is_attachment;
118 * Is query requesting an author page.
120 * If the $author parameter is specified then the check will be expanded to
121 * include whether the queried author matches the one given in the parameter.
122 * You can match against integers and against strings.
124 * If matching against an integer, the ID should be used of the author for the
125 * test. If the $author is an ID and matches the author page user ID, then
126 * 'true' will be returned.
128 * If matching against strings, then the test will be matched against both the
129 * nickname and user nicename and will return true on success.
134 * @param string|int $author Optional. Is current page this author.
135 * @return bool True if page is author or $author (if set).
137 function is_author ($author = '') {
140 if ( !$wp_query->is_author )
143 if ( empty($author) )
146 $author_obj = $wp_query->get_queried_object();
148 $author = (array) $author;
150 if ( in_array( $author_obj->ID, $author ) )
152 elseif ( in_array( $author_obj->nickname, $author ) )
154 elseif ( in_array( $author_obj->user_nicename, $author ) )
161 * Whether current page query contains a category name or given category name.
163 * The category list can contain category IDs, names, or category slugs. If any
164 * of them are part of the query, then it will return true.
169 * @param string|array $category Optional.
172 function is_category ($category = '') {
175 if ( !$wp_query->is_category )
178 if ( empty($category) )
181 $cat_obj = $wp_query->get_queried_object();
183 $category = (array) $category;
185 if ( in_array( $cat_obj->term_id, $category ) )
187 elseif ( in_array( $cat_obj->name, $category ) )
189 elseif ( in_array( $cat_obj->slug, $category ) )
196 * Whether the current page query has the given tag slug or contains tag.
201 * @param string|array $slug Optional. Single tag or list of tags to check for.
204 function is_tag( $slug = '' ) {
207 if ( !$wp_query->is_tag )
210 if ( empty( $slug ) )
213 $tag_obj = $wp_query->get_queried_object();
215 $slug = (array) $slug;
217 if ( in_array( $tag_obj->slug, $slug ) )
224 * Whether the current page query has the given taxonomy slug or contains taxonomy.
229 * @param string|array $slug Optional. Slug or slugs to check in current query.
232 function is_tax( $slug = '' ) {
235 if ( !$wp_query->is_tax )
241 return in_array( get_query_var('taxonomy'), (array) $slug );
245 * Whether the current URL is within the comments popup window.
252 function is_comments_popup () {
255 return $wp_query->is_comments_popup;
259 * Whether current URL is based on a date.
266 function is_date () {
269 return $wp_query->is_date;
273 * Whether current blog URL contains a day.
283 return $wp_query->is_day;
287 * Whether current page query is feed URL.
294 function is_feed () {
297 return $wp_query->is_feed;
301 * Whether current page query is the front of the site.
307 * @return bool True, if front of site.
309 function is_front_page () {
311 if ( 'posts' == get_option('show_on_front') && is_home() )
313 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
320 * Whether current page view is the blog homepage.
325 * @return bool True if blog view homepage.
327 function is_home () {
330 return $wp_query->is_home;
334 * Whether current page query contains a month.
341 function is_month () {
344 return $wp_query->is_month;
348 * Whether query is page or contains given page(s).
350 * Calls the function without any parameters will only test whether the current
351 * query is of the page type. Either a list or a single item can be tested
352 * against for whether the query is a page and also is the value or one of the
353 * values in the page parameter.
355 * The parameter can contain the page ID, page title, or page name. The
356 * parameter can also be an array of those three values.
361 * @param mixed $page Either page or list of pages to test against.
364 function is_page ($page = '') {
367 if ( !$wp_query->is_page )
373 $page_obj = $wp_query->get_queried_object();
375 $page = (array) $page;
377 if ( in_array( $page_obj->ID, $page ) )
379 elseif ( in_array( $page_obj->post_title, $page ) )
381 else if ( in_array( $page_obj->post_name, $page ) )
388 * Whether query contains multiple pages for the results.
395 function is_paged () {
398 return $wp_query->is_paged;
402 * Whether the current page was created by a plugin.
404 * The plugin can set this by using the global $plugin_page and setting it to
408 * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
412 function is_plugin_page() {
415 if ( isset($plugin_page) )
422 * Whether the current query is preview of post or page.
429 function is_preview() {
432 return $wp_query->is_preview;
436 * Whether the current query post is robots.
443 function is_robots() {
446 return $wp_query->is_robots;
450 * Whether current query is the result of a user search.
457 function is_search () {
460 return $wp_query->is_search;
464 * Whether the current page query is single page.
466 * The parameter can contain the post ID, post title, or post name. The
467 * parameter can also be an array of those three values.
469 * This applies to other post types, attachments, pages, posts. Just means that
470 * the current query has only a single object.
475 * @param mixed $post Either post or list of posts to test against.
478 function is_single ($post = '') {
481 if ( !$wp_query->is_single )
487 $post_obj = $wp_query->get_queried_object();
489 $post = (array) $post;
491 if ( in_array( $post_obj->ID, $post ) )
493 elseif ( in_array( $post_obj->post_title, $post ) )
495 elseif ( in_array( $post_obj->post_name, $post ) )
502 * Whether is single post, is a page, or is an attachment.
509 function is_singular() {
512 return $wp_query->is_singular;
516 * Whether the query contains a time.
523 function is_time () {
526 return $wp_query->is_time;
530 * Whether the query is a trackback.
537 function is_trackback () {
540 return $wp_query->is_trackback;
544 * Whether the query contains a year.
551 function is_year () {
554 return $wp_query->is_year;
558 * Whether current page query is a 404 and no results for WordPress query.
563 * @return bool True, if nothing is found matching WordPress Query.
568 return $wp_query->is_404;
572 * The Loop. Post loop control.
576 * Whether current WordPress query has results to loop over.
578 * @see WP_Query::have_posts()
584 function have_posts() {
587 return $wp_query->have_posts();
591 * Whether the caller is in the Loop.
596 * @return bool True if caller is within loop, false if loop hasn't started or ended.
598 function in_the_loop() {
601 return $wp_query->in_the_loop;
605 * Rewind the loop posts.
607 * @see WP_Query::rewind_posts()
613 function rewind_posts() {
616 return $wp_query->rewind_posts();
620 * Iterate the post index in the loop.
622 * @see WP_Query::the_post()
626 function the_post() {
629 $wp_query->the_post();
637 * Whether there are comments to loop over.
639 * @see WP_Query::have_comments()
645 function have_comments() {
647 return $wp_query->have_comments();
651 * Iterate comment index in the comment loop.
653 * @see WP_Query::the_comment()
659 function the_comment() {
661 return $wp_query->the_comment();
669 * The WordPress Query class.
671 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
687 * Query search variables set by the user.
693 var $query_vars = array();
696 * Holds the data for a single object that is queried.
698 * Holds the contents of a post, page, category, attachment.
707 * The ID of the queried object.
713 var $queried_object_id;
716 * Get post database query.
734 * The amount of posts for the current query.
743 * Index of the current item in the loop.
749 var $current_post = -1;
752 * Whether the loop has started and the caller is in the loop.
758 var $in_the_loop = false;
761 * The current post ID.
770 * The list of comments for current post.
779 * The amount of comments for the posts.
785 var $comment_count = 0;
788 * The index of the comment in the comment loop.
794 var $current_comment = -1;
797 * Current comment ID.
806 * Amount of posts if limit clause was not used.
812 var $found_posts = 0;
815 * The amount of pages.
821 var $max_num_pages = 0;
824 * The amount of comment pages.
830 var $max_num_comment_pages = 0;
833 * Set if query is single post.
839 var $is_single = false;
842 * Set if query is preview of blog.
848 var $is_preview = false;
851 * Set if query returns a page.
857 var $is_page = false;
860 * Set if query is an archive list.
866 var $is_archive = false;
869 * Set if query is part of a date.
875 var $is_date = false;
878 * Set if query contains a year.
884 var $is_year = false;
887 * Set if query contains a month.
893 var $is_month = false;
896 * Set if query contains a day.
905 * Set if query contains time.
911 var $is_time = false;
914 * Set if query contains an author.
920 var $is_author = false;
923 * Set if query contains category.
929 var $is_category = false;
932 * Set if query contains tag.
941 * Set if query contains taxonomy.
950 * Set if query was part of a search result.
956 var $is_search = false;
959 * Set if query is feed display.
965 var $is_feed = false;
968 * Set if query is comment feed display.
974 var $is_comment_feed = false;
977 * Set if query is trackback.
983 var $is_trackback = false;
986 * Set if query is blog homepage.
992 var $is_home = false;
995 * Set if query couldn't found anything.
1001 var $is_404 = false;
1004 * Set if query is within comments popup window.
1010 var $is_comments_popup = false;
1013 * Set if query is part of administration page.
1019 var $is_admin = false;
1022 * Set if query is an attachment.
1028 var $is_attachment = false;
1031 * Set if is single, is a page, or is an attachment.
1037 var $is_singular = false;
1040 * Set if query is for robots.
1046 var $is_robots = false;
1049 * Set if query contains posts.
1051 * Basically, the homepage if the option isn't set for the static homepage.
1057 var $is_posts_page = false;
1060 * Resets query flags to false.
1062 * The query flags are what page info WordPress was able to figure out.
1067 function init_query_flags() {
1068 $this->is_single = false;
1069 $this->is_page = false;
1070 $this->is_archive = false;
1071 $this->is_date = false;
1072 $this->is_year = false;
1073 $this->is_month = false;
1074 $this->is_day = false;
1075 $this->is_time = false;
1076 $this->is_author = false;
1077 $this->is_category = false;
1078 $this->is_tag = false;
1079 $this->is_tax = false;
1080 $this->is_search = false;
1081 $this->is_feed = false;
1082 $this->is_comment_feed = false;
1083 $this->is_trackback = false;
1084 $this->is_home = false;
1085 $this->is_404 = false;
1086 $this->is_paged = false;
1087 $this->is_admin = false;
1088 $this->is_attachment = false;
1089 $this->is_singular = false;
1090 $this->is_robots = false;
1091 $this->is_posts_page = false;
1095 * Initiates object properties and sets default values.
1101 unset($this->posts);
1102 unset($this->query);
1103 $this->query_vars = array();
1104 unset($this->queried_object);
1105 unset($this->queried_object_id);
1106 $this->post_count = 0;
1107 $this->current_post = -1;
1108 $this->in_the_loop = false;
1110 $this->init_query_flags();
1114 * Reparse the query vars.
1119 function parse_query_vars() {
1120 $this->parse_query('');
1124 * Fills in the query variables, which do not exist within the parameter.
1129 * @param array $array Defined query variables.
1130 * @return array Complete query variables with undefined ones filled in empty.
1132 function fill_query_vars($array) {
1168 foreach ($keys as $key) {
1169 if ( !isset($array[$key]))
1173 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1174 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1176 foreach ( $array_keys as $key ) {
1177 if ( !isset($array[$key]))
1178 $array[$key] = array();
1184 * Parse a query string and set query type booleans.
1189 * @param string|array $query
1191 function parse_query ($query) {
1192 if ( !empty($query) || !isset($this->query) ) {
1194 if ( is_array($query) )
1195 $this->query_vars = $query;
1197 parse_str($query, $this->query_vars);
1198 $this->query = $query;
1201 $this->query_vars = $this->fill_query_vars($this->query_vars);
1202 $qv = &$this->query_vars;
1204 if ( ! empty($qv['robots']) )
1205 $this->is_robots = true;
1207 $qv['p'] = absint($qv['p']);
1208 $qv['page_id'] = absint($qv['page_id']);
1209 $qv['year'] = absint($qv['year']);
1210 $qv['monthnum'] = absint($qv['monthnum']);
1211 $qv['day'] = absint($qv['day']);
1212 $qv['w'] = absint($qv['w']);
1213 $qv['m'] = absint($qv['m']);
1214 $qv['paged'] = absint($qv['paged']);
1215 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1216 $qv['pagename'] = trim( $qv['pagename'] );
1217 $qv['name'] = trim( $qv['name'] );
1218 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1219 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1220 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1222 // Compat. Map subpost to attachment.
1223 if ( '' != $qv['subpost'] )
1224 $qv['attachment'] = $qv['subpost'];
1225 if ( '' != $qv['subpost_id'] )
1226 $qv['attachment_id'] = $qv['subpost_id'];
1228 $qv['attachment_id'] = absint($qv['attachment_id']);
1230 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1231 $this->is_single = true;
1232 $this->is_attachment = true;
1233 } elseif ( '' != $qv['name'] ) {
1234 $this->is_single = true;
1235 } elseif ( $qv['p'] ) {
1236 $this->is_single = true;
1237 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1238 // If year, month, day, hour, minute, and second are set, a single
1239 // post is being queried.
1240 $this->is_single = true;
1241 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1242 $this->is_page = true;
1243 $this->is_single = false;
1244 } elseif ( !empty($qv['s']) ) {
1245 $this->is_search = true;
1247 // Look for archive queries. Dates, categories, authors.
1249 if ( '' !== $qv['second'] ) {
1250 $this->is_time = true;
1251 $this->is_date = true;
1254 if ( '' !== $qv['minute'] ) {
1255 $this->is_time = true;
1256 $this->is_date = true;
1259 if ( '' !== $qv['hour'] ) {
1260 $this->is_time = true;
1261 $this->is_date = true;
1265 if (! $this->is_date) {
1266 $this->is_day = true;
1267 $this->is_date = true;
1271 if ( $qv['monthnum'] ) {
1272 if (! $this->is_date) {
1273 $this->is_month = true;
1274 $this->is_date = true;
1278 if ( $qv['year'] ) {
1279 if (! $this->is_date) {
1280 $this->is_year = true;
1281 $this->is_date = true;
1286 $this->is_date = true;
1287 if (strlen($qv['m']) > 9) {
1288 $this->is_time = true;
1289 } else if (strlen($qv['m']) > 7) {
1290 $this->is_day = true;
1291 } else if (strlen($qv['m']) > 5) {
1292 $this->is_month = true;
1294 $this->is_year = true;
1298 if ('' != $qv['w']) {
1299 $this->is_date = true;
1302 if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
1303 $this->is_category = false;
1305 if (strpos($qv['cat'], '-') !== false) {
1306 $this->is_category = false;
1308 $this->is_category = true;
1312 if ( '' != $qv['category_name'] ) {
1313 $this->is_category = true;
1316 if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
1317 $qv['category__in'] = array();
1319 $qv['category__in'] = array_map('absint', $qv['category__in']);
1320 $this->is_category = true;
1323 if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
1324 $qv['category__not_in'] = array();
1326 $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
1329 if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
1330 $qv['category__and'] = array();
1332 $qv['category__and'] = array_map('absint', $qv['category__and']);
1333 $this->is_category = true;
1336 if ( '' != $qv['tag'] )
1337 $this->is_tag = true;
1339 $qv['tag_id'] = absint($qv['tag_id']);
1340 if ( !empty($qv['tag_id']) )
1341 $this->is_tag = true;
1343 if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
1344 $qv['tag__in'] = array();
1346 $qv['tag__in'] = array_map('absint', $qv['tag__in']);
1347 $this->is_tag = true;
1350 if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
1351 $qv['tag__not_in'] = array();
1353 $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
1356 if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
1357 $qv['tag__and'] = array();
1359 $qv['tag__and'] = array_map('absint', $qv['tag__and']);
1360 $this->is_category = true;
1363 if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
1364 $qv['tag_slug__in'] = array();
1366 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
1367 $this->is_tag = true;
1370 if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
1371 $qv['tag_slug__and'] = array();
1373 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
1374 $this->is_tag = true;
1377 if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
1378 $this->is_tax = false;
1379 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1380 if ( $t->query_var && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
1381 $qv['taxonomy'] = $taxonomy;
1382 $qv['term'] = $qv[$t->query_var];
1383 $this->is_tax = true;
1388 $this->is_tax = true;
1391 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1392 $this->is_author = false;
1394 $this->is_author = true;
1397 if ( '' != $qv['author_name'] ) {
1398 $this->is_author = true;
1401 if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
1402 $this->is_archive = true;
1405 if ( '' != $qv['feed'] )
1406 $this->is_feed = true;
1408 if ( '' != $qv['tb'] )
1409 $this->is_trackback = true;
1411 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1412 $this->is_paged = true;
1414 if ( '' != $qv['comments_popup'] )
1415 $this->is_comments_popup = true;
1417 // if we're previewing inside the write screen
1418 if ('' != $qv['preview'])
1419 $this->is_preview = true;
1422 $this->is_admin = true;
1424 if ( false !== strpos($qv['feed'], 'comments-') ) {
1425 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1426 $qv['withcomments'] = 1;
1429 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1431 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1432 $this->is_comment_feed = true;
1434 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 ) )
1435 $this->is_home = true;
1437 // Correct is_* for page_on_front and page_for_posts
1438 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1439 $this->is_page = true;
1440 $this->is_home = false;
1441 $qv['page_id'] = get_option('page_on_front');
1444 if ( '' != $qv['pagename'] ) {
1445 $this->queried_object =& get_page_by_path($qv['pagename']);
1446 if ( !empty($this->queried_object) )
1447 $this->queried_object_id = (int) $this->queried_object->ID;
1449 unset($this->queried_object);
1451 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1452 $this->is_page = false;
1453 $this->is_home = true;
1454 $this->is_posts_page = true;
1458 if ( $qv['page_id'] ) {
1459 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1460 $this->is_page = false;
1461 $this->is_home = true;
1462 $this->is_posts_page = true;
1466 if ( !empty($qv['post_type']) ) {
1467 if(is_array($qv['post_type']))
1468 $qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true));
1470 $qv['post_type'] = sanitize_user($qv['post_type'], true);
1473 if ( !empty($qv['post_status']) )
1474 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1476 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1477 $this->is_comment_feed = false;
1479 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1480 // Done correcting is_* for page_on_front and page_for_posts
1482 if ('404' == $qv['error'])
1485 if ( !empty($query) )
1486 do_action_ref_array('parse_query', array(&$this));
1490 * Sets the 404 property and saves whether query is feed.
1495 function set_404() {
1496 $is_feed = $this->is_feed;
1498 $this->init_query_flags();
1499 $this->is_404 = true;
1501 $this->is_feed = $is_feed;
1505 * Retrieve query variable.
1510 * @param string $query_var Query variable key.
1513 function get($query_var) {
1514 if (isset($this->query_vars[$query_var])) {
1515 return $this->query_vars[$query_var];
1522 * Set query variable.
1527 * @param string $query_var Query variable key.
1528 * @param mixed $value Query variable value.
1530 function set($query_var, $value) {
1531 $this->query_vars[$query_var] = $value;
1535 * Retrieve the posts based on query variables.
1537 * There are a few filters and actions that can be used to modify the post
1542 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1544 * @return array List of posts.
1546 function &get_posts() {
1547 global $wpdb, $user_ID;
1549 do_action_ref_array('pre_get_posts', array(&$this));
1552 $q = &$this->query_vars;
1554 $q = $this->fill_query_vars($q);
1556 // First let's clear some variables
1560 $whichmimetype = '';
1566 $fields = "$wpdb->posts.*";
1567 $post_status_join = false;
1570 if ( !isset($q['caller_get_posts']) )
1571 $q['caller_get_posts'] = false;
1573 if ( !isset($q['suppress_filters']) )
1574 $q['suppress_filters'] = false;
1576 if ( !isset($q['post_type']) ) {
1577 if ( $this->is_search )
1578 $q['post_type'] = 'any';
1580 $q['post_type'] = '';
1582 $post_type = $q['post_type'];
1583 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1584 $q['posts_per_page'] = get_option('posts_per_page');
1585 if ( isset($q['showposts']) && $q['showposts'] ) {
1586 $q['showposts'] = (int) $q['showposts'];
1587 $q['posts_per_page'] = $q['showposts'];
1589 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1590 $q['posts_per_page'] = $q['posts_per_archive_page'];
1591 if ( !isset($q['nopaging']) ) {
1592 if ($q['posts_per_page'] == -1) {
1593 $q['nopaging'] = true;
1595 $q['nopaging'] = false;
1598 if ( $this->is_feed ) {
1599 $q['posts_per_page'] = get_option('posts_per_rss');
1600 $q['nopaging'] = false;
1602 $q['posts_per_page'] = (int) $q['posts_per_page'];
1603 if ( $q['posts_per_page'] < -1 )
1604 $q['posts_per_page'] = abs($q['posts_per_page']);
1605 else if ( $q['posts_per_page'] == 0 )
1606 $q['posts_per_page'] = 1;
1608 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
1609 $q['comments_per_page'] = get_option('comments_per_page');
1611 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
1612 $this->is_page = true;
1613 $this->is_home = false;
1614 $q['page_id'] = get_option('page_on_front');
1617 if (isset($q['page'])) {
1618 $q['page'] = trim($q['page'], '/');
1619 $q['page'] = absint($q['page']);
1622 // If a month is specified in the querystring, load that month
1624 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
1625 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
1626 if (strlen($q['m'])>5)
1627 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
1628 if (strlen($q['m'])>7)
1629 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
1630 if (strlen($q['m'])>9)
1631 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
1632 if (strlen($q['m'])>11)
1633 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
1634 if (strlen($q['m'])>13)
1635 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
1638 if ( '' !== $q['hour'] )
1639 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
1641 if ( '' !== $q['minute'] )
1642 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
1644 if ( '' !== $q['second'] )
1645 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
1648 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
1650 if ( $q['monthnum'] )
1651 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
1654 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
1656 if ('' != $q['name']) {
1657 $q['name'] = sanitize_title($q['name']);
1658 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
1659 } else if ('' != $q['pagename']) {
1660 if ( isset($this->queried_object_id) )
1661 $reqpage = $this->queried_object_id;
1663 $reqpage = get_page_by_path($q['pagename']);
1664 if ( !empty($reqpage) )
1665 $reqpage = $reqpage->ID;
1670 $page_for_posts = get_option('page_for_posts');
1671 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
1672 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
1673 $page_paths = '/' . trim($q['pagename'], '/');
1674 $q['pagename'] = sanitize_title(basename($page_paths));
1675 $q['name'] = $q['pagename'];
1676 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
1677 $reqpage_obj = get_page($reqpage);
1678 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
1679 $this->is_attachment = true;
1680 $this->is_page = true;
1681 $q['attachment_id'] = $reqpage;
1684 } elseif ('' != $q['attachment']) {
1685 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
1686 $attach_paths = '/' . trim($q['attachment'], '/');
1687 $q['attachment'] = sanitize_title(basename($attach_paths));
1688 $q['name'] = $q['attachment'];
1689 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
1693 $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
1695 if ( intval($q['comments_popup']) )
1696 $q['p'] = absint($q['comments_popup']);
1698 // If an attachment is requested by number, let it supercede any post number.
1699 if ( $q['attachment_id'] )
1700 $q['p'] = absint($q['attachment_id']);
1702 // If a post number is specified, load that post
1704 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
1705 } elseif ( $q['post__in'] ) {
1706 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
1707 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
1708 } elseif ( $q['post__not_in'] ) {
1709 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
1710 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
1713 if ( is_numeric($q['post_parent']) )
1714 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
1716 if ( $q['page_id'] ) {
1717 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
1718 $q['p'] = $q['page_id'];
1719 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
1723 // If a search pattern is specified, load the posts that match
1724 if ( !empty($q['s']) ) {
1725 // added slashes screw with quote grouping when done early, so done later
1726 $q['s'] = stripslashes($q['s']);
1727 if ( !empty($q['sentence']) ) {
1728 $q['search_terms'] = array($q['s']);
1730 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
1731 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
1733 $n = !empty($q['exact']) ? '' : '%';
1735 foreach( (array) $q['search_terms'] as $term) {
1736 $term = addslashes_gpc($term);
1737 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1738 $searchand = ' AND ';
1740 $term = esc_sql($q['s']);
1741 if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
1742 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
1744 if ( !empty($search) ) {
1745 $search = " AND ({$search}) ";
1746 if ( !is_user_logged_in() )
1747 $search .= " AND ($wpdb->posts.post_password = '') ";
1753 if ( empty($q['cat']) || ($q['cat'] == '0') ||
1754 // Bypass cat checks if fetching specific posts
1755 $this->is_singular ) {
1758 $q['cat'] = ''.urldecode($q['cat']).'';
1759 $q['cat'] = addslashes_gpc($q['cat']);
1760 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1762 $req_cats = array();
1763 foreach ( (array) $cat_array as $cat ) {
1764 $cat = intval($cat);
1769 $q['category__in'][] = $cat;
1770 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
1772 $q['category__not_in'][] = $cat;
1773 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
1776 $q['cat'] = implode(',', $req_cats);
1779 if ( !empty($q['category__in']) ) {
1780 $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) ";
1781 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1782 $include_cats = "'" . implode("', '", $q['category__in']) . "'";
1783 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
1786 if ( !empty($q['category__not_in']) ) {
1787 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
1788 $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) )";
1791 // Category stuff for nice URLs
1792 if ( '' != $q['category_name'] && !$this->is_singular ) {
1793 $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
1794 $reqcat = get_category_by_path($q['category_name']);
1795 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
1796 $cat_paths = '/' . trim($q['category_name'], '/');
1797 $q['category_name'] = sanitize_title(basename($cat_paths));
1799 $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
1800 $q['category_name'] = sanitize_title(basename($cat_paths));
1801 $cat_paths = explode('/', $cat_paths);
1803 foreach ( (array) $cat_paths as $pathdir )
1804 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
1806 //if we don't match the entire hierarchy fallback on just matching the nicename
1807 if ( empty($reqcat) )
1808 $reqcat = get_category_by_path($q['category_name'], false);
1810 if ( !empty($reqcat) )
1811 $reqcat = $reqcat->term_id;
1815 $q['cat'] = $reqcat;
1817 $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) ";
1818 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1819 $in_cats = array($q['cat']);
1820 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
1821 $in_cats = "'" . implode("', '", $in_cats) . "'";
1822 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
1823 $groupby = "{$wpdb->posts}.ID";
1827 if ( '' != $q['tag'] ) {
1828 if ( strpos($q['tag'], ',') !== false ) {
1829 $tags = preg_split('/[,\s]+/', $q['tag']);
1830 foreach ( (array) $tags as $tag ) {
1831 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1832 $q['tag_slug__in'][] = $tag;
1834 } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1835 $tags = preg_split('/[+\s]+/', $q['tag']);
1836 foreach ( (array) $tags as $tag ) {
1837 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1838 $q['tag_slug__and'][] = $tag;
1841 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1842 $q['tag_slug__in'][] = $q['tag'];
1846 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
1847 $groupby = "{$wpdb->posts}.ID";
1850 if ( !empty($q['tag__in']) && empty($q['cat']) ) {
1851 $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) ";
1852 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1853 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1854 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1855 $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
1856 if ( !empty($reqtag) )
1857 $q['tag_id'] = $reqtag['term_id'];
1860 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
1861 $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) ";
1862 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1863 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1864 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1865 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
1866 if ( !empty($reqtag) )
1867 $q['tag_id'] = $reqtag->term_id;
1870 if ( !empty($q['tag__not_in']) ) {
1871 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
1872 $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) )";
1875 // Tag and slug intersections.
1876 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
1877 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
1878 foreach ($intersections as $item => $taxonomy) {
1879 if ( empty($q[$item]) ) continue;
1880 if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
1882 if ( $item != 'category__and' ) {
1883 $reqtag = is_term( $q[$item][0], 'post_tag' );
1884 if ( !empty($reqtag) )
1885 $q['tag_id'] = $reqtag['term_id'];
1888 if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
1889 $taxonomy_field = 'slug';
1891 $taxonomy_field = 'term_id';
1893 $q[$item] = array_unique($q[$item]);
1894 $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)";
1895 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
1896 if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
1897 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
1899 $post_ids = $wpdb->get_col($tsql);
1901 if ( count($post_ids) )
1902 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1904 $whichcat = " AND 0 = 1";
1910 if ( $this->is_tax ) {
1911 if ( '' != $q['taxonomy'] ) {
1912 $taxonomy = $q['taxonomy'];
1913 $tt[$taxonomy] = $q['term'];
1914 $terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
1916 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1917 if ( $t->query_var && '' != $q[$t->query_var] ) {
1918 $terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var]));
1919 if ( !is_wp_error($terms) )
1924 if ( is_wp_error($terms) || empty($terms) ) {
1925 $whichcat = " AND 0 ";
1927 foreach ( $terms as $term )
1928 $term_ids[] = $term->term_id;
1929 $post_ids = get_objects_in_term($term_ids, $taxonomy);
1930 if ( !is_wp_error($post_ids) && count($post_ids) ) {
1931 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1933 $q['post_status'] = 'publish';
1934 $post_status_join = true;
1936 $whichcat = " AND 0 ";
1941 // Author/user stuff
1943 if ( empty($q['author']) || ($q['author'] == '0') ) {
1946 $q['author'] = ''.urldecode($q['author']).'';
1947 $q['author'] = addslashes_gpc($q['author']);
1948 if (strpos($q['author'], '-') !== false) {
1951 $q['author'] = explode('-', $q['author']);
1952 $q['author'] = '' . absint($q['author'][1]);
1957 $author_array = preg_split('/[,\s]+/', $q['author']);
1958 $whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]);
1959 for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
1960 $whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]);
1962 $whichauthor .= ')';
1965 // Author stuff for nice URLs
1967 if ('' != $q['author_name']) {
1968 if (strpos($q['author_name'], '/') !== false) {
1969 $q['author_name'] = explode('/',$q['author_name']);
1970 if ($q['author_name'][count($q['author_name'])-1]) {
1971 $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
1973 $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
1976 $q['author_name'] = sanitize_title($q['author_name']);
1977 $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
1978 $q['author'] = get_user_by('slug', $q['author_name']);
1980 $q['author'] = $q['author']->ID;
1981 $whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')';
1984 // MIME-Type stuff for attachment browsing
1986 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
1987 $whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
1989 $where .= $search.$whichcat.$whichauthor.$whichmimetype;
1991 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
1992 $q['order'] = 'DESC';
1995 if ( empty($q['orderby']) ) {
1996 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
1997 } elseif ( 'none' == $q['orderby'] ) {
2000 // Used to filter values
2001 $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2002 if ( !empty($q['meta_key']) ) {
2003 $allowed_keys[] = $q['meta_key'];
2004 $allowed_keys[] = 'meta_value';
2006 $q['orderby'] = urldecode($q['orderby']);
2007 $q['orderby'] = addslashes_gpc($q['orderby']);
2008 $orderby_array = explode(' ',$q['orderby']);
2009 if ( empty($orderby_array) )
2010 $orderby_array[] = $q['orderby'];
2012 for ($i = 0; $i < count($orderby_array); $i++) {
2013 // Only allow certain values for safety
2014 $orderby = $orderby_array[$i];
2019 $orderby = "$wpdb->posts.ID";
2022 $orderby = 'RAND()';
2024 case $q['meta_key']:
2026 $orderby = "$wpdb->postmeta.meta_value";
2028 case 'comment_count':
2029 $orderby = "$wpdb->posts.comment_count";
2032 $orderby = "$wpdb->posts.post_" . $orderby;
2034 if ( in_array($orderby_array[$i], $allowed_keys) )
2035 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
2037 // append ASC or DESC at the end
2038 if ( !empty($q['orderby']))
2039 $q['orderby'] .= " {$q['order']}";
2041 if ( empty($q['orderby']) )
2042 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
2045 if ( is_array($post_type) )
2046 $post_type_cap = 'multiple_post_type';
2048 $post_type_cap = $post_type;
2050 $exclude_post_types = '';
2051 foreach ( get_post_types( array('exclude_from_search' => true) ) as $_wp_post_type )
2052 $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $_wp_post_type);
2054 if ( 'any' == $post_type ) {
2055 $where .= $exclude_post_types;
2056 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2057 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2058 } elseif ( ! empty( $post_type ) ) {
2059 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2060 } elseif ( $this->is_attachment ) {
2061 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2062 $post_type_cap = 'post';
2063 } elseif ($this->is_page) {
2064 $where .= " AND $wpdb->posts.post_type = 'page'";
2065 $post_type_cap = 'page';
2067 $where .= " AND $wpdb->posts.post_type = 'post'";
2068 $post_type_cap = 'post';
2071 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
2072 $statuswheres = array();
2073 $q_status = explode(',', $q['post_status']);
2074 $r_status = array();
2075 $p_status = array();
2076 if ( $q['post_status'] == 'any' ) {
2077 // @todo Use register_post_status() data to determine which states should be excluded.
2078 $r_status[] = "$wpdb->posts.post_status <> 'trash'";
2080 if ( in_array( 'draft' , $q_status ) )
2081 $r_status[] = "$wpdb->posts.post_status = 'draft'";
2082 if ( in_array( 'pending', $q_status ) )
2083 $r_status[] = "$wpdb->posts.post_status = 'pending'";
2084 if ( in_array( 'future' , $q_status ) )
2085 $r_status[] = "$wpdb->posts.post_status = 'future'";
2086 if ( in_array( 'inherit' , $q_status ) )
2087 $r_status[] = "$wpdb->posts.post_status = 'inherit'";
2088 if ( in_array( 'private', $q_status ) )
2089 $p_status[] = "$wpdb->posts.post_status = 'private'";
2090 if ( in_array( 'publish', $q_status ) )
2091 $r_status[] = "$wpdb->posts.post_status = 'publish'";
2092 if ( in_array( 'trash', $q_status ) )
2093 $r_status[] = "$wpdb->posts.post_status = 'trash'";
2096 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2097 $r_status = array_merge($r_status, $p_status);
2101 if ( !empty($r_status) ) {
2102 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type_cap}s") )
2103 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2105 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2107 if ( !empty($p_status) ) {
2108 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type_cap}s") )
2109 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2111 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2113 if ( $post_status_join ) {
2114 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2115 foreach ( $statuswheres as $index => $statuswhere )
2116 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2118 foreach ( $statuswheres as $statuswhere )
2119 $where .= " AND $statuswhere";
2120 } elseif ( !$this->is_singular ) {
2121 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2124 $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
2126 if ( is_user_logged_in() ) {
2127 $where .= current_user_can( "read_private_{$post_type_cap}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
2134 if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
2135 $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
2136 if ( ! empty($q['meta_key']) )
2137 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
2138 if ( ! empty($q['meta_value']) ) {
2139 if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
2140 $q['meta_compare'] = '=';
2142 $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
2145 // Apply filters on where and join prior to paging so that any
2146 // manipulations to them are reflected in the paging by day queries.
2147 if ( !$q['suppress_filters'] ) {
2148 $where = apply_filters('posts_where', $where);
2149 $join = apply_filters('posts_join', $join);
2153 if ( empty($q['nopaging']) && !$this->is_singular ) {
2154 $page = absint($q['paged']);
2159 if ( empty($q['offset']) ) {
2161 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2162 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
2163 } else { // we're ignoring $page and using 'offset'
2164 $q['offset'] = absint($q['offset']);
2165 $pgstrt = $q['offset'] . ', ';
2166 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2171 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2172 if ( $this->is_archive || $this->is_search ) {
2173 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2174 $cwhere = "WHERE comment_approved = '1' $where";
2175 $cgroupby = "$wpdb->comments.comment_id";
2176 } else { // Other non singular e.g. front
2177 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2178 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2182 if ( !$q['suppress_filters'] ) {
2183 $cjoin = apply_filters('comment_feed_join', $cjoin);
2184 $cwhere = apply_filters('comment_feed_where', $cwhere);
2185 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
2186 $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
2187 $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
2189 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2190 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2192 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2193 $this->comment_count = count($this->comments);
2195 $post_ids = array();
2197 foreach ($this->comments as $comment)
2198 $post_ids[] = (int) $comment->comment_post_ID;
2200 $post_ids = join(',', $post_ids);
2203 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2208 $orderby = $q['orderby'];
2210 // Apply post-paging filters on where and join. Only plugins that
2211 // manipulate paging queries should use these hooks.
2212 if ( !$q['suppress_filters'] ) {
2213 $where = apply_filters('posts_where_paged', $where);
2214 $groupby = apply_filters('posts_groupby', $groupby);
2215 $join = apply_filters('posts_join_paged', $join);
2216 $orderby = apply_filters('posts_orderby', $orderby);
2217 $distinct = apply_filters('posts_distinct', $distinct);
2218 $limits = apply_filters( 'post_limits', $limits );
2220 $fields = apply_filters('posts_fields', $fields);
2223 // Announce current selection parameters. For use by caching plugins.
2224 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2226 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2227 if ( !$q['suppress_filters'] ) {
2228 $where = apply_filters('posts_where_request', $where);
2229 $groupby = apply_filters('posts_groupby_request', $groupby);
2230 $join = apply_filters('posts_join_request', $join);
2231 $orderby = apply_filters('posts_orderby_request', $orderby);
2232 $distinct = apply_filters('posts_distinct_request', $distinct);
2233 $fields = apply_filters('posts_fields_request', $fields);
2234 $limits = apply_filters( 'post_limits_request', $limits );
2237 if ( ! empty($groupby) )
2238 $groupby = 'GROUP BY ' . $groupby;
2239 if ( !empty( $orderby ) )
2240 $orderby = 'ORDER BY ' . $orderby;
2242 if ( !empty($limits) )
2243 $found_rows = 'SQL_CALC_FOUND_ROWS';
2245 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2246 if ( !$q['suppress_filters'] )
2247 $this->request = apply_filters('posts_request', $this->request);
2249 $this->posts = $wpdb->get_results($this->request);
2250 // Raw results filter. Prior to status checks.
2251 if ( !$q['suppress_filters'] )
2252 $this->posts = apply_filters('posts_results', $this->posts);
2254 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2255 $cjoin = apply_filters('comment_feed_join', '');
2256 $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
2257 $cgroupby = apply_filters('comment_feed_groupby', '');
2258 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2259 $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
2260 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2261 $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
2262 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2263 $this->comments = $wpdb->get_results($comments_request);
2264 $this->comment_count = count($this->comments);
2267 if ( !empty($limits) ) {
2268 $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
2269 $this->found_posts = $wpdb->get_var( $found_posts_query );
2270 $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
2271 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
2274 // Check post status to determine if post should be displayed.
2275 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2276 $status = get_post_status($this->posts[0]);
2277 //$type = get_post_type($this->posts[0]);
2278 if ( ('publish' != $status) ) {
2279 if ( ! is_user_logged_in() ) {
2280 // User must be logged in to view unpublished posts.
2281 $this->posts = array();
2283 if (in_array($status, array('draft', 'pending')) ) {
2284 // User must have edit permissions on the draft to preview.
2285 if (! current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
2286 $this->posts = array();
2288 $this->is_preview = true;
2289 $this->posts[0]->post_date = current_time('mysql');
2291 } else if ('future' == $status) {
2292 $this->is_preview = true;
2293 if (!current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
2294 $this->posts = array ( );
2297 if (! current_user_can("read_$post_type_cap", $this->posts[0]->ID))
2298 $this->posts = array();
2303 if ( $this->is_preview && current_user_can( "edit_{$post_type_cap}", $this->posts[0]->ID ) )
2304 $this->posts[0] = apply_filters('the_preview', $this->posts[0]);
2307 // Put sticky posts at the top of the posts array
2308 $sticky_posts = get_option('sticky_posts');
2309 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
2310 $num_posts = count($this->posts);
2312 // Loop over posts and relocate stickies to the front.
2313 for ( $i = 0; $i < $num_posts; $i++ ) {
2314 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2315 $sticky_post = $this->posts[$i];
2316 // Remove sticky from current position
2317 array_splice($this->posts, $i, 1);
2318 // Move to front, after other stickies
2319 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2320 // Increment the sticky offset. The next sticky will be placed at this offset.
2322 // Remove post from sticky posts array
2323 $offset = array_search($sticky_post->ID, $sticky_posts);
2324 array_splice($sticky_posts, $offset, 1);
2328 // Fetch sticky posts that weren't in the query results
2329 if ( !empty($sticky_posts) ) {
2330 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2331 // honor post type(s) if not set to any
2332 $stickies_where = '';
2333 if ( 'any' != $post_type && '' != $post_type ) {
2334 if ( is_array( $post_type ) ) {
2335 $post_types = join( "', '", $post_type );
2337 $post_types = $post_type;
2339 $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
2341 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
2342 /** @todo Make sure post is published or viewable by the current user */
2343 foreach ( $stickies as $sticky_post ) {
2344 if ( 'publish' != $sticky_post->post_status )
2346 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2352 if ( !$q['suppress_filters'] )
2353 $this->posts = apply_filters('the_posts', $this->posts);
2355 $this->post_count = count($this->posts);
2357 // Sanitize before caching so it'll only get done once
2358 for ($i = 0; $i < $this->post_count; $i++) {
2359 $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
2362 update_post_caches($this->posts);
2364 if ($this->post_count > 0) {
2365 $this->post = $this->posts[0];
2368 return $this->posts;
2372 * Setup the next post and iterate current post index.
2377 * @return object Next post.
2379 function next_post() {
2381 $this->current_post++;
2383 $this->post = $this->posts[$this->current_post];
2388 * Sets up the current post.
2390 * Retrieves the next post, sets up the post, sets the 'in the loop'
2396 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2398 function the_post() {
2400 $this->in_the_loop = true;
2402 if ( $this->current_post == -1 ) // loop has just started
2403 do_action_ref_array('loop_start', array(&$this));
2405 $post = $this->next_post();
2406 setup_postdata($post);
2410 * Whether there are more posts available in the loop.
2412 * Calls action 'loop_end', when the loop is complete.
2416 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2418 * @return bool True if posts are available, false if end of loop.
2420 function have_posts() {
2421 if ($this->current_post + 1 < $this->post_count) {
2423 } elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
2424 do_action_ref_array('loop_end', array(&$this));
2425 // Do some cleaning up after the loop
2426 $this->rewind_posts();
2429 $this->in_the_loop = false;
2434 * Rewind the posts and reset post index.
2439 function rewind_posts() {
2440 $this->current_post = -1;
2441 if ($this->post_count > 0) {
2442 $this->post = $this->posts[0];
2447 * Iterate current comment index and return comment object.
2452 * @return object Comment object.
2454 function next_comment() {
2455 $this->current_comment++;
2457 $this->comment = $this->comments[$this->current_comment];
2458 return $this->comment;
2462 * Sets up the current comment.
2466 * @global object $comment Current comment.
2467 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2469 function the_comment() {
2472 $comment = $this->next_comment();
2474 if ($this->current_comment == 0) {
2475 do_action('comment_loop_start');
2480 * Whether there are more comments available.
2482 * Automatically rewinds comments when finished.
2487 * @return bool True, if more comments. False, if no more posts.
2489 function have_comments() {
2490 if ($this->current_comment + 1 < $this->comment_count) {
2492 } elseif ($this->current_comment + 1 == $this->comment_count) {
2493 $this->rewind_comments();
2500 * Rewind the comments, resets the comment index and comment to first.
2505 function rewind_comments() {
2506 $this->current_comment = -1;
2507 if ($this->comment_count > 0) {
2508 $this->comment = $this->comments[0];
2513 * Sets up the WordPress query by parsing query string.
2518 * @param string $query URL query string.
2519 * @return array List of posts.
2521 function &query($query) {
2522 $this->parse_query($query);
2523 return $this->get_posts();
2527 * Retrieve queried object.
2529 * If queried object is not set, then the queried object will be set from
2530 * the category, tag, taxonomy, posts page, single post, page, or author
2531 * query variable. After it is set up, it will be returned.
2538 function get_queried_object() {
2539 if (isset($this->queried_object)) {
2540 return $this->queried_object;
2543 $this->queried_object = NULL;
2544 $this->queried_object_id = 0;
2546 if ($this->is_category) {
2547 $cat = $this->get('cat');
2548 $category = &get_category($cat);
2549 if ( is_wp_error( $category ) )
2551 $this->queried_object = &$category;
2552 $this->queried_object_id = (int) $cat;
2553 } else if ($this->is_tag) {
2554 $tag_id = $this->get('tag_id');
2555 $tag = &get_term($tag_id, 'post_tag');
2556 if ( is_wp_error( $tag ) )
2558 $this->queried_object = &$tag;
2559 $this->queried_object_id = (int) $tag_id;
2560 } else if ($this->is_tax) {
2561 $tax = $this->get('taxonomy');
2562 $slug = $this->get('term');
2563 $term = &get_terms($tax, array('slug'=>$slug));
2564 if ( is_wp_error($term) || empty($term) )
2567 $this->queried_object = $term;
2568 $this->queried_object_id = $term->term_id;
2569 } else if ($this->is_posts_page) {
2570 $this->queried_object = & get_page(get_option('page_for_posts'));
2571 $this->queried_object_id = (int) $this->queried_object->ID;
2572 } else if ($this->is_single) {
2573 $this->queried_object = $this->post;
2574 $this->queried_object_id = (int) $this->post->ID;
2575 } else if ($this->is_page) {
2576 $this->queried_object = $this->post;
2577 $this->queried_object_id = (int) $this->post->ID;
2578 } else if ($this->is_author) {
2579 $author_id = (int) $this->get('author');
2580 $author = get_userdata($author_id);
2581 $this->queried_object = $author;
2582 $this->queried_object_id = $author_id;
2585 return $this->queried_object;
2589 * Retrieve ID of the current queried object.
2596 function get_queried_object_id() {
2597 $this->get_queried_object();
2599 if (isset($this->queried_object_id)) {
2600 return $this->queried_object_id;
2607 * PHP4 type constructor.
2609 * Sets up the WordPress query, if parameter is not empty.
2614 * @param string $query URL query string.
2617 function WP_Query ($query = '') {
2618 if (! empty($query)) {
2619 $this->query($query);
2625 * Redirect old slugs to the correct permalink.
2627 * Attempts to find the current slug from the past slugs.
2633 * @return null If no link is found, null is returned.
2635 function wp_old_slug_redirect () {
2637 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
2640 $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'] . "'";
2642 // if year, monthnum, or day have been specified, make our query more precise
2643 // just in case there are multiple identical _wp_old_slug values
2644 if ( '' != $wp_query->query_vars['year'] )
2645 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
2646 if ( '' != $wp_query->query_vars['monthnum'] )
2647 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
2648 if ( '' != $wp_query->query_vars['day'] )
2649 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
2651 $id = (int) $wpdb->get_var($query);
2656 $link = get_permalink($id);
2661 wp_redirect($link, '301'); // Permanent redirect
2667 * Setup global post data.
2671 * @param object $post Post data.
2672 * @uses do_action_ref_array() Calls 'the_post'
2673 * @return bool True when finished.
2675 function setup_postdata($post) {
2676 global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
2678 $id = (int) $post->ID;
2680 $authordata = get_userdata($post->post_author);
2682 $day = mysql2date('d.m.y', $post->post_date, false);
2683 $currentmonth = mysql2date('m', $post->post_date, false);
2685 $page = get_query_var('page');
2688 if ( is_single() || is_page() || is_feed() )
2690 $content = $post->post_content;
2691 if ( strpos( $content, '<!--nextpage-->' ) ) {
2695 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
2696 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
2697 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
2698 $pages = explode('<!--nextpage-->', $content);
2699 $numpages = count($pages);
2701 $pages[0] = $post->post_content;
2705 do_action_ref_array('the_post', array(&$post));