]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/query.php
Wordpress 2.3.2-scripts
[autoinstalls/wordpress.git] / wp-includes / query.php
1 <?php
2
3 /*
4  * The Big Query.
5  */
6
7 function get_query_var($var) {
8         global $wp_query;
9
10         return $wp_query->get($var);
11 }
12
13 function set_query_var($var, $value) {
14         global $wp_query;
15
16         return $wp_query->set($var, $value);
17 }
18
19 function &query_posts($query) {
20         unset($GLOBALS['wp_query']);
21         $GLOBALS['wp_query'] =& new WP_Query();
22         return $GLOBALS['wp_query']->query($query);
23 }
24
25 function wp_reset_query() {
26         unset($GLOBALS['wp_query']);
27         $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
28 }
29
30 /*
31  * Query type checks.
32  */
33
34 function is_admin () {
35         if ( defined('WP_ADMIN') ) 
36                 return WP_ADMIN;
37         return false;
38 }
39
40 function is_archive () {
41         global $wp_query;
42
43         return $wp_query->is_archive;
44 }
45
46 function is_attachment () {
47         global $wp_query;
48
49         return $wp_query->is_attachment;
50 }
51
52 function is_author ($author = '') {
53         global $wp_query;
54
55         if ( !$wp_query->is_author )
56                 return false;
57
58         if ( empty($author) )
59                 return true;
60
61         $author_obj = $wp_query->get_queried_object();
62
63         if ( $author == $author_obj->ID )
64                 return true;
65         elseif ( $author == $author_obj->nickname )
66                 return true;
67         elseif ( $author == $author_obj->user_nicename )
68                 return true;
69
70         return false;
71 }
72
73 function is_category ($category = '') {
74         global $wp_query;
75
76         if ( !$wp_query->is_category )
77                 return false;
78
79         if ( empty($category) )
80                 return true;
81
82         $cat_obj = $wp_query->get_queried_object();
83
84         if ( $category == $cat_obj->term_id )
85                 return true;
86         else if ( $category == $cat_obj->name )
87                 return true;
88         elseif ( $category == $cat_obj->slug )
89                 return true;
90
91         return false;
92 }
93
94 function is_tag( $slug = '' ) {
95         global $wp_query;
96         if ( !$wp_query->is_tag )
97                 return false;
98
99         if ( empty( $slug ) )
100                 return true;
101
102         $tag_obj = $wp_query->get_queried_object();
103         if ( $slug == $tag_obj->slug )
104                 return true;
105         return false;
106 }
107
108 function is_comments_popup () {
109         global $wp_query;
110
111         return $wp_query->is_comments_popup;
112 }
113
114 function is_date () {
115         global $wp_query;
116
117         return $wp_query->is_date;
118 }
119
120 function is_day () {
121         global $wp_query;
122
123         return $wp_query->is_day;
124 }
125
126 function is_feed () {
127         global $wp_query;
128
129         return $wp_query->is_feed;
130 }
131
132 function is_home () {
133         global $wp_query;
134
135         return $wp_query->is_home;
136 }
137
138 function is_month () {
139         global $wp_query;
140
141         return $wp_query->is_month;
142 }
143
144 function is_page ($page = '') {
145         global $wp_query;
146
147         if ( !$wp_query->is_page )
148                 return false;
149
150         if ( empty($page) )
151                 return true;
152
153         $page_obj = $wp_query->get_queried_object();
154
155         if ( $page == $page_obj->ID )
156                 return true;
157         elseif ( $page == $page_obj->post_title )
158                 return true;
159         else if ( $page == $page_obj->post_name )
160                 return true;
161
162         return false;
163 }
164
165 function is_paged () {
166         global $wp_query;
167
168         return $wp_query->is_paged;
169 }
170
171 function is_plugin_page() {
172         global $plugin_page;
173
174         if ( isset($plugin_page) )
175                 return true;
176
177         return false;
178 }
179
180 function is_preview() {
181         global $wp_query;
182
183         return $wp_query->is_preview;
184 }
185
186 function is_robots() {
187         global $wp_query;
188
189         return $wp_query->is_robots;
190 }
191
192 function is_search () {
193         global $wp_query;
194
195         return $wp_query->is_search;
196 }
197
198 function is_single ($post = '') {
199         global $wp_query;
200
201         if ( !$wp_query->is_single )
202                 return false;
203
204         if ( empty( $post) )
205                 return true;
206
207         $post_obj = $wp_query->get_queried_object();
208
209         if ( $post == $post_obj->ID )
210                 return true;
211         elseif ( $post == $post_obj->post_title )
212                 return true;
213         elseif ( $post == $post_obj->post_name )
214                 return true;
215
216         return false;
217 }
218
219 function is_singular() {
220         global $wp_query;
221
222         return $wp_query->is_singular;
223 }
224
225 function is_time () {
226         global $wp_query;
227
228         return $wp_query->is_time;
229 }
230
231 function is_trackback () {
232         global $wp_query;
233
234         return $wp_query->is_trackback;
235 }
236
237 function is_year () {
238         global $wp_query;
239
240         return $wp_query->is_year;
241 }
242
243 function is_404 () {
244         global $wp_query;
245
246         return $wp_query->is_404;
247 }
248
249 /*
250  * The Loop.  Post loop control.
251  */
252
253 function have_posts() {
254         global $wp_query;
255
256         return $wp_query->have_posts();
257 }
258
259 function in_the_loop() {
260         global $wp_query;
261
262         return $wp_query->in_the_loop;
263 }
264
265 function rewind_posts() {
266         global $wp_query;
267
268         return $wp_query->rewind_posts();
269 }
270
271 function the_post() {
272         global $wp_query;
273
274         $wp_query->the_post();
275 }
276
277 /*
278  * Comments loop.
279  */
280
281 function have_comments() {
282         global $wp_query;
283         return $wp_query->have_comments();
284 }
285
286 function the_comment() {
287         global $wp_query;
288         return $wp_query->the_comment();
289 }
290
291 /*
292  * WP_Query
293  */
294
295 class WP_Query {
296         var $query;
297         var $query_vars = array();
298         var $queried_object;
299         var $queried_object_id;
300         var $request;
301
302         var $posts;
303         var $post_count = 0;
304         var $current_post = -1;
305         var $in_the_loop = false;
306         var $post;
307
308         var $comments;
309         var $comment_count = 0;
310         var $current_comment = -1;
311         var $comment;
312
313         var $found_posts = 0;
314         var $max_num_pages = 0;
315
316         var $is_single = false;
317         var $is_preview = false;
318         var $is_page = false;
319         var $is_archive = false;
320         var $is_date = false;
321         var $is_year = false;
322         var $is_month = false;
323         var $is_day = false;
324         var $is_time = false;
325         var $is_author = false;
326         var $is_category = false;
327         var $is_tag = false;
328         var $is_search = false;
329         var $is_feed = false;
330         var $is_comment_feed = false;
331         var $is_trackback = false;
332         var $is_home = false;
333         var $is_404 = false;
334         var $is_comments_popup = false;
335         var $is_admin = false;
336         var $is_attachment = false;
337         var $is_singular = false;
338         var $is_robots = false;
339         var $is_posts_page = false;
340
341         function init_query_flags() {
342                 $this->is_single = false;
343                 $this->is_page = false;
344                 $this->is_archive = false;
345                 $this->is_date = false;
346                 $this->is_year = false;
347                 $this->is_month = false;
348                 $this->is_day = false;
349                 $this->is_time = false;
350                 $this->is_author = false;
351                 $this->is_category = false;
352                 $this->is_tag = false;
353                 $this->is_search = false;
354                 $this->is_feed = false;
355                 $this->is_comment_feed = false;
356                 $this->is_trackback = false;
357                 $this->is_home = false;
358                 $this->is_404 = false;
359                 $this->is_paged = false;
360                 $this->is_admin = false;
361                 $this->is_attachment = false;
362                 $this->is_singular = false;
363                 $this->is_robots = false;
364                 $this->is_posts_page = false;
365         }
366
367         function init () {
368                 unset($this->posts);
369                 unset($this->query);
370                 $this->query_vars = array();
371                 unset($this->queried_object);
372                 unset($this->queried_object_id);
373                 $this->post_count = 0;
374                 $this->current_post = -1;
375                 $this->in_the_loop = false;
376
377                 $this->init_query_flags();
378         }
379
380         // Reparse the query vars.
381         function parse_query_vars() {
382                 $this->parse_query('');
383         }
384
385         function fill_query_vars($array) {
386                 $keys = array(
387                         'error'
388                         , 'm'
389                         , 'p'
390                         , 'subpost'
391                         , 'subpost_id'
392                         , 'attachment'
393                         , 'attachment_id'
394                         , 'name'
395                         , 'hour'
396                         , 'static'
397                         , 'pagename'
398                         , 'page_id'
399                         , 'second'
400                         , 'minute'
401                         , 'hour'
402                         , 'day'
403                         , 'monthnum'
404                         , 'year'
405                         , 'w'
406                         , 'category_name'
407                         , 'tag'
408                         , 'tag_id'
409                         , 'author_name'
410                         , 'feed'
411                         , 'tb'
412                         , 'paged'
413                         , 'comments_popup'
414                         , 'preview'
415                 );
416
417                 foreach ($keys as $key) {
418                         if ( !isset($array[$key]))
419                                 $array[$key] = '';
420                 }
421
422                 $array_keys = array('category__in', 'category__not_in', 'category__and',
423                         'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
424
425                 foreach ( $array_keys as $key ) {
426                         if ( !isset($array[$key]))
427                                 $array[$key] = array();
428                 }
429                 return $array;
430         }
431
432         // Parse a query string and set query type booleans.
433         function parse_query ($query) {
434                 if ( !empty($query) || !isset($this->query) ) {
435                         $this->init();
436                         if ( is_array($query) )
437                                 $this->query_vars = $query;
438                         else
439                                 parse_str($query, $this->query_vars);
440                         $this->query = $query;
441                 }
442
443                 $this->query_vars = $this->fill_query_vars($this->query_vars);
444                 $qv = &$this->query_vars;
445
446                 if ( ! empty($qv['robots']) )
447                         $this->is_robots = true;
448
449                 $qv['p'] =  (int) $qv['p'];
450                 $qv['page_id'] =  (int) $qv['page_id'];
451                 $qv['year'] = (int) $qv['year'];
452                 $qv['monthnum'] = (int) $qv['monthnum'];
453                 $qv['day'] = (int) $qv['day'];
454                 $qv['w'] = (int) $qv['w'];
455                 $qv['m'] =  (int) $qv['m'];
456                 if ( '' !== $qv['hour'] ) $qv['hour'] = (int) $qv['hour'];
457                 if ( '' !== $qv['minute'] ) $qv['minute'] = (int) $qv['minute'];
458                 if ( '' !== $qv['second'] ) $qv['second'] = (int) $qv['second'];
459
460                 // Compat.  Map subpost to attachment.
461                 if ( '' != $qv['subpost'] )
462                         $qv['attachment'] = $qv['subpost'];
463                 if ( '' != $qv['subpost_id'] )
464                         $qv['attachment_id'] = $qv['subpost_id'];
465
466                 $qv['attachment_id'] = (int) $qv['attachment_id'];
467
468                 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
469                         $this->is_single = true;
470                         $this->is_attachment = true;
471                 } elseif ( '' != $qv['name'] ) {
472                         $this->is_single = true;
473                 } elseif ( $qv['p'] ) {
474                         $this->is_single = true;
475                 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
476                         // If year, month, day, hour, minute, and second are set, a single
477                         // post is being queried.
478                         $this->is_single = true;
479                 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
480                         $this->is_page = true;
481                         $this->is_single = false;
482                 } elseif ( !empty($qv['s']) ) {
483                         $this->is_search = true;
484                 } else {
485                 // Look for archive queries.  Dates, categories, authors.
486
487                         if ( '' !== $qv['second'] ) {
488                                 $this->is_time = true;
489                                 $this->is_date = true;
490                         }
491
492                         if ( '' !== $qv['minute'] ) {
493                                 $this->is_time = true;
494                                 $this->is_date = true;
495                         }
496
497                         if ( '' !== $qv['hour'] ) {
498                                 $this->is_time = true;
499                                 $this->is_date = true;
500                         }
501
502                         if ( $qv['day'] ) {
503                                 if (! $this->is_date) {
504                                         $this->is_day = true;
505                                         $this->is_date = true;
506                                 }
507                         }
508
509                         if ( $qv['monthnum'] ) {
510                                 if (! $this->is_date) {
511                                         $this->is_month = true;
512                                         $this->is_date = true;
513                                 }
514                         }
515
516                         if ( $qv['year'] ) {
517                                 if (! $this->is_date) {
518                                         $this->is_year = true;
519                                         $this->is_date = true;
520                                 }
521                         }
522
523                         if ( $qv['m'] ) {
524                                 $this->is_date = true;
525                                 if (strlen($qv['m']) > 9) {
526                                         $this->is_time = true;
527                                 } else if (strlen($qv['m']) > 7) {
528                                         $this->is_day = true;
529                                 } else if (strlen($qv['m']) > 5) {
530                                         $this->is_month = true;
531                                 } else {
532                                         $this->is_year = true;
533                                 }
534                         }
535
536                         if ('' != $qv['w']) {
537                                 $this->is_date = true;
538                         }
539
540                         if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
541                                 $this->is_category = false;
542                         } else {
543                                 if (strpos($qv['cat'], '-') !== false) {
544                                         $this->is_category = false;
545                                 } else {
546                                         $this->is_category = true;
547                                 }
548                         }
549
550                         if ( '' != $qv['category_name'] ) {
551                                 $this->is_category = true;
552                         }
553
554                         if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
555                                 $qv['category__in'] = array();
556                         } else {
557                                 $qv['category__in'] = array_map('intval', $qv['category__in']);
558                                 $this->is_category = true;
559                         }
560
561                         if ( !is_array($qv['category___not_in']) || empty($qv['category__not_in']) ) {
562                                 $qv['category__not_in'] = array();
563                         } else {
564                                 $qv['category__not_in'] = array_map('intval', $qv['category__not_in']);
565                         }
566
567                         if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
568                                 $qv['category__and'] = array();
569                         } else {
570                                 $qv['category__and'] = array_map('intval', $qv['category__and']);
571                                 $this->is_category = true;
572                         }
573
574                         if (  '' != $qv['tag'] )
575                                 $this->is_tag = true;
576
577                         $qv['tag_id'] = (int) $qv['tag_id'];
578                         if (  !empty($qv['tag_id']) )
579                                 $this->is_tag = true;
580
581                         if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
582                                 $qv['tag__in'] = array();
583                         } else {
584                                 $qv['tag__in'] = array_map('intval', $qv['tag__in']);
585                                 $this->is_tag = true;
586                         }
587
588                         if ( !is_array($qv['tag___not_in']) || empty($qv['tag__not_in']) ) {
589                                 $qv['tag__not_in'] = array();
590                         } else {
591                                 $qv['tag__not_in'] = array_map('intval', $qv['tag__not_in']);
592                         }
593
594                         if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
595                                 $qv['tag__and'] = array();
596                         } else {
597                                 $qv['tag__and'] = array_map('intval', $qv['tag__and']);
598                                 $this->is_category = true;
599                         }
600
601                         if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
602                                 $qv['tag_slug__in'] = array();
603                         } else {
604                                 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
605                                 $this->is_tag = true;
606                         }
607
608                         if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
609                                 $qv['tag_slug__and'] = array();
610                         } else {
611                                 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
612                                 $this->is_tag = true;
613                         }
614
615                         if ( empty($qv['author']) || ($qv['author'] == '0') ) {
616                                 $this->is_author = false;
617                         } else {
618                                 $this->is_author = true;
619                         }
620
621                         if ( '' != $qv['author_name'] ) {
622                                 $this->is_author = true;
623                         }
624
625                         if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag ) )
626                                 $this->is_archive = true;
627                 }
628
629                 if ( '' != $qv['feed'] )
630                         $this->is_feed = true;
631
632                 if ( '' != $qv['tb'] )
633                         $this->is_trackback = true;
634
635                 if ( '' != $qv['paged'] )
636                         $this->is_paged = true;
637
638                 if ( '' != $qv['comments_popup'] )
639                         $this->is_comments_popup = true;
640
641                 // if we're previewing inside the write screen
642                 if ('' != $qv['preview'])
643                         $this->is_preview = true;
644
645                 if ( is_admin() )
646                         $this->is_admin = true;
647
648                 if ( false !== strpos($qv['feed'], 'comments-') ) {
649                         $qv['feed'] = str_replace('comments-', '', $qv['feed']);
650                         $qv['withcomments'] = 1;
651                 }
652
653                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
654
655                 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
656                         $this->is_comment_feed = true;
657
658                 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 ) )
659                         $this->is_home = true;
660
661                 // Correct is_* for page_on_front and page_for_posts
662                 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
663                         $this->is_page = true;
664                         $this->is_home = false;
665                         $qv['page_id'] = get_option('page_on_front');
666                 }
667
668                 if ( '' != $qv['pagename'] ) {
669                         $this->queried_object =& get_page_by_path($qv['pagename']);
670                         if ( !empty($this->queried_object) )
671                                 $this->queried_object_id = (int) $this->queried_object->ID;
672                         else
673                                 unset($this->queried_object);
674
675                         if  ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
676                                 $this->is_page = false;
677                                 $this->is_home = true;
678                                 $this->is_posts_page = true;
679                         }
680                 }
681
682                 if ( $qv['page_id'] ) {
683                         if  ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
684                                 $this->is_page = false;
685                                 $this->is_home = true;
686                                 $this->is_posts_page = true;
687                         }
688                 }
689
690                 if ( !empty($qv['post_type']) )
691                         $qv['post_type'] = sanitize_user($qv['post_type'], true);
692
693                 if ( !empty($qv['post_status']) )
694                         $qv['post_status'] = sanitize_user($qv['post_status'], true);
695
696                 if ( $this->is_posts_page && !$qv['withcomments'] )
697                         $this->is_comment_feed = false;
698
699                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
700                 // Done correcting is_* for page_on_front and page_for_posts
701
702                 if ('404' == $qv['error'])
703                         $this->set_404();
704
705                 if ( !empty($query) )
706                         do_action_ref_array('parse_query', array(&$this));
707         }
708
709         function set_404() {
710                 $is_feed = $this->is_feed;
711
712                 $this->init_query_flags();
713                 $this->is_404 = true;
714
715                 $this->is_feed = $is_feed;
716         }
717
718         function get($query_var) {
719                 if (isset($this->query_vars[$query_var])) {
720                         return $this->query_vars[$query_var];
721                 }
722
723                 return '';
724         }
725
726         function set($query_var, $value) {
727                 $this->query_vars[$query_var] = $value;
728         }
729
730         function &get_posts() {
731                 global $wpdb, $pagenow, $user_ID;
732
733                 do_action_ref_array('pre_get_posts', array(&$this));
734
735                 // Shorthand.
736                 $q = &$this->query_vars;
737
738                 $q = $this->fill_query_vars($q);
739
740                 // First let's clear some variables
741                 $distinct = '';
742                 $whichcat = '';
743                 $whichauthor = '';
744                 $whichpage = '';
745                 $result = '';
746                 $where = '';
747                 $limits = '';
748                 $join = '';
749                 $search = '';
750                 $groupby = '';
751
752                 if ( !isset($q['post_type']) )
753                         $q['post_type'] = 'post';
754                 $post_type = $q['post_type'];
755                 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
756                         $q['posts_per_page'] = get_option('posts_per_page');
757                 if ( isset($q['showposts']) && $q['showposts'] ) {
758                         $q['showposts'] = (int) $q['showposts'];
759                         $q['posts_per_page'] = $q['showposts'];
760                 }
761                 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
762                         $q['posts_per_page'] = $q['posts_per_archive_page'];
763                 if ( !isset($q['nopaging']) ) {
764                         if ($q['posts_per_page'] == -1) {
765                                 $q['nopaging'] = true;
766                         } else {
767                                 $q['nopaging'] = false;
768                         }
769                 }
770                 if ( $this->is_feed ) {
771                         $q['posts_per_page'] = get_option('posts_per_rss');
772                         $q['nopaging'] = false;
773                 }
774                 $q['posts_per_page'] = (int) $q['posts_per_page'];
775                 if ( $q['posts_per_page'] < -1 )
776                         $q['posts_per_page'] = abs($q['posts_per_page']);
777                 else if ( $q['posts_per_page'] == 0 )
778                         $q['posts_per_page'] = 1;
779
780                 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
781                         $this->is_page = true;
782                         $this->is_home = false;
783                         $q['page_id'] = get_option('page_on_front');
784                 }
785
786                 if (isset($q['page'])) {
787                         $q['page'] = trim($q['page'], '/');
788                         $q['page'] = (int) $q['page'];
789                         $q['page'] = abs($q['page']);
790                 }
791
792                 $add_hours = intval(get_option('gmt_offset'));
793                 $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
794                 $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
795
796                 // If a month is specified in the querystring, load that month
797                 if ( $q['m'] ) {
798                         $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
799                         $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
800                         if (strlen($q['m'])>5)
801                                 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
802                         if (strlen($q['m'])>7)
803                                 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
804                         if (strlen($q['m'])>9)
805                                 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
806                         if (strlen($q['m'])>11)
807                                 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
808                         if (strlen($q['m'])>13)
809                                 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
810                 }
811
812                 if ( '' !== $q['hour'] )
813                         $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
814
815                 if ( '' !== $q['minute'] )
816                         $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
817
818                 if ( '' !== $q['second'] )
819                         $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
820
821                 if ( $q['year'] )
822                         $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
823
824                 if ( $q['monthnum'] )
825                         $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
826
827                 if ( $q['day'] )
828                         $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
829
830                 if ('' != $q['name']) {
831                         $q['name'] = sanitize_title($q['name']);
832                         $where .= " AND post_name = '" . $q['name'] . "'";
833                 } else if ('' != $q['pagename']) {
834                         if ( isset($this->queried_object_id) )
835                                 $reqpage = $this->queried_object_id;
836                         else {
837                                 $reqpage = get_page_by_path($q['pagename']);
838                                 if ( !empty($reqpage) )
839                                         $reqpage = $reqpage->ID;
840                                 else
841                                         $reqpage = 0;
842                         }
843
844                         if  ( ('page' != get_option('show_on_front') ) || ( $reqpage != get_option('page_for_posts') ) ) {
845                                 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
846                                 $page_paths = '/' . trim($q['pagename'], '/');
847                                 $q['pagename'] = sanitize_title(basename($page_paths));
848                                 $q['name'] = $q['pagename'];
849                                 $where .= " AND (ID = '$reqpage')";
850                         }
851                 } elseif ('' != $q['attachment']) {
852                         $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
853                         $attach_paths = '/' . trim($q['attachment'], '/');
854                         $q['attachment'] = sanitize_title(basename($attach_paths));
855                         $q['name'] = $q['attachment'];
856                         $where .= " AND post_name = '" . $q['attachment'] . "'";
857                 }
858
859                 if ( $q['w'] )
860                         $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
861
862                 if ( intval($q['comments_popup']) )
863                         $q['p'] = intval($q['comments_popup']);
864
865                 // If an attachment is requested by number, let it supercede any post number.
866                 if ( $q['attachment_id'] )
867                         $q['p'] = $q['attachment_id'];
868
869                 // If a post number is specified, load that post
870                 if ( $q['p'] )
871                         $where = ' AND ID = ' . $q['p'];
872
873                 if ( $q['page_id'] ) {
874                         if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
875                                 $q['p'] = $q['page_id'];
876                                 $where = ' AND ID = ' . $q['page_id'];
877                         }
878                 }
879
880                 // If a search pattern is specified, load the posts that match
881                 if ( !empty($q['s']) ) {
882                         // added slashes screw with quote grouping when done early, so done later
883                         $q['s'] = stripslashes($q['s']);
884                         if ($q['sentence']) {
885                                 $q['search_terms'] = array($q['s']);
886                         }
887                         else {
888                                 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
889                                 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
890                         }
891                         $n = ($q['exact']) ? '' : '%';
892                         $searchand = '';
893                         foreach((array)$q['search_terms'] as $term) {
894                                 $term = addslashes_gpc($term);
895                                 $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
896                                 $searchand = ' AND ';
897                         }
898                         $term = addslashes_gpc($q['s']);
899                         if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
900                                 $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
901
902                         if ( !empty($search) )
903                                 $search = " AND ({$search}) ";
904                 }
905
906                 // Category stuff
907
908                 if ( empty($q['cat']) || ($q['cat'] == '0') ||
909                                 // Bypass cat checks if fetching specific posts
910                                 $this->is_singular ) {
911                         $whichcat = '';
912                 } else {
913                         $q['cat'] = ''.urldecode($q['cat']).'';
914                         $q['cat'] = addslashes_gpc($q['cat']);
915                         $cat_array = preg_split('/[,\s]+/', $q['cat']);
916                         foreach ( $cat_array as $cat ) {
917                                 $cat = intval($cat);
918                                 $in = ($cat > 0);
919                                 $cat = abs($cat);
920                                 if ( $in ) {
921                                         $q['category__in'][] = $cat;
922                                         $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
923                                 } else {
924                                         $q['category__not_in'][] = $cat;
925                                         $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
926                                 }
927                         }
928                 }
929
930                 if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
931                         $groupby = "{$wpdb->posts}.ID";
932                 }
933
934                 if ( !empty($q['category__in']) ) {
935                         $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) ";
936                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
937                         $include_cats = "'" . implode("', '", $q['category__in']) . "'";
938                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
939                 }
940
941                 if ( !empty($q['category__not_in']) ) {
942                         $ids = get_objects_in_term($q['category__not_in'], 'category');
943                         if ( is_wp_error( $ids ) ) 
944                                 return $ids;
945                         if ( is_array($ids) && count($ids > 0) ) {
946                                 $out_posts = "'" . implode("', '", $ids) . "'";
947                                 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
948                         }
949                 }
950
951                 // Category stuff for nice URLs
952                 if ( '' != $q['category_name'] ) {
953                         $reqcat = get_category_by_path($q['category_name']);
954                         $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
955                         $cat_paths = '/' . trim($q['category_name'], '/');
956                         $q['category_name'] = sanitize_title(basename($cat_paths));
957
958                         $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
959                         $q['category_name'] = sanitize_title(basename($cat_paths));
960                         $cat_paths = explode('/', $cat_paths);
961                         $cat_path = '';
962                         foreach ( (array) $cat_paths as $pathdir )
963                                 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
964
965                         //if we don't match the entire hierarchy fallback on just matching the nicename
966                         if ( empty($reqcat) )
967                                 $reqcat = get_category_by_path($q['category_name'], false);
968
969                         if ( !empty($reqcat) )
970                                 $reqcat = $reqcat->term_id;
971                         else
972                                 $reqcat = 0;
973
974                         $q['cat'] = $reqcat;
975
976                         $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) ";
977                         $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
978                         $in_cats = array($q['cat']);
979                         $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
980                         $in_cats = "'" . implode("', '", $in_cats) . "'";
981                         $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
982                         $groupby = "{$wpdb->posts}.ID";
983                 }
984
985                 // Tags
986                 if ( '' != $q['tag'] ) {
987                         if ( strpos($q['tag'], ',') !== false ) {
988                                 $tags = preg_split('/[,\s]+/', $q['tag']);
989                                 foreach ( (array) $tags as $tag ) {
990                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
991                                         $q['tag_slug__in'][] = $tag;
992                                 }
993                         } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
994                                 $tags = preg_split('/[+\s]+/', $q['tag']);
995                                 foreach ( (array) $tags as $tag ) {
996                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
997                                         $q['tag_slug__and'][] = $tag;
998                                 }
999                         } else {
1000                                 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1001                                 $reqtag = is_term( $q['tag'], 'post_tag' );
1002                                 if ( !empty($reqtag) )
1003                                         $reqtag = $reqtag['term_id'];
1004                                 else
1005                                         $reqtag = 0;
1006
1007                                 $q['tag_id'] = $reqtag;
1008                                 $q['tag__in'][] = $reqtag;
1009                         }
1010                 }
1011
1012                 if ( !empty($q['tag__in']) || !empty($q['tag__not_in']) || !empty($q['tag__and']) ||
1013                         !empty($q['tag_slug__in']) || !empty($q['tag_slug__and']) ) {
1014                         $groupby = "{$wpdb->posts}.ID";
1015                 }
1016
1017                 if ( !empty($q['tag__in']) ) {
1018                         $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) ";
1019                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1020                         $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1021                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1022                         $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
1023                         if ( !empty($reqtag) )
1024                                 $q['tag_id'] = $reqtag['term_id'];
1025                 }
1026
1027                 if ( !empty($q['tag_slug__in']) ) {
1028                         $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) ";
1029                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1030                         $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1031                         $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1032                         $reqtag = is_term( $q['tag_slug__in'][0], 'post_tag' );
1033                         if ( !empty($reqtag) )
1034                                 $q['tag_id'] = $reqtag['term_id'];
1035                 }
1036
1037                 if ( !empty($q['tag__not_in']) ) {
1038                         $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
1039                         if ( is_array($ids) && count($ids > 0) ) {
1040                                 $out_posts = "'" . implode("', '", $ids) . "'";
1041                                 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
1042                         }
1043                 }
1044
1045                 // Tag and slug intersections.
1046                 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
1047                 foreach ($intersections as $item => $taxonomy) {
1048                         if ( empty($q[$item]) ) continue;
1049
1050                         if ( $item != 'category__and' ) {
1051                                 $reqtag = is_term( $q[$item][0], 'post_tag' );
1052                                 if ( !empty($reqtag) )
1053                                         $q['tag_id'] = $reqtag['term_id'];
1054                         }
1055
1056                         $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
1057
1058                         $q[$item] = array_unique($q[$item]);
1059                         $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)";
1060                         $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
1061                         $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
1062
1063                         $post_ids = $wpdb->get_col($tsql);
1064
1065                         if ( count($post_ids) )
1066                                 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1067                         else {
1068                                 $whichcat = " AND 0 = 1";
1069                                 break;
1070                         }
1071                 }
1072
1073                 // Author/user stuff
1074
1075                 if ( empty($q['author']) || ($q['author'] == '0') ) {
1076                         $whichauthor='';
1077                 } else {
1078                         $q['author'] = ''.urldecode($q['author']).'';
1079                         $q['author'] = addslashes_gpc($q['author']);
1080                         if (strpos($q['author'], '-') !== false) {
1081                                 $eq = '!=';
1082                                 $andor = 'AND';
1083                                 $q['author'] = explode('-', $q['author']);
1084                                 $q['author'] = ''.intval($q['author'][1]);
1085                         } else {
1086                                 $eq = '=';
1087                                 $andor = 'OR';
1088                         }
1089                         $author_array = preg_split('/[,\s]+/', $q['author']);
1090                         $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
1091                         for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
1092                                 $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
1093                         }
1094                         $whichauthor .= ')';
1095                 }
1096
1097                 // Author stuff for nice URLs
1098
1099                 if ('' != $q['author_name']) {
1100                         if (strpos($q['author_name'], '/') !== false) {
1101                                 $q['author_name'] = explode('/',$q['author_name']);
1102                                 if ($q['author_name'][count($q['author_name'])-1]) {
1103                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
1104                                 } else {
1105                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
1106                                 }
1107                         }
1108                         $q['author_name'] = sanitize_title($q['author_name']);
1109                         $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
1110                         $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
1111                 }
1112
1113                 $where .= $search.$whichcat.$whichauthor;
1114
1115                 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
1116                         $q['order'] = 'DESC';
1117
1118                 // Order by
1119                 if ( empty($q['orderby']) ) {
1120                         $q['orderby'] = 'post_date '.$q['order'];
1121                 } else {
1122                         // Used to filter values
1123                         $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');
1124                         $q['orderby'] = urldecode($q['orderby']);
1125                         $q['orderby'] = addslashes_gpc($q['orderby']);
1126                         $orderby_array = explode(' ',$q['orderby']);
1127                         if ( empty($orderby_array) )
1128                                 $orderby_array[] = $q['orderby'];
1129                         $q['orderby'] = '';
1130                         for ($i = 0; $i < count($orderby_array); $i++) {
1131                                 // Only allow certain values for safety
1132                                 $orderby = $orderby_array[$i];
1133                                 if ( 'menu_order' != $orderby )
1134                                         $orderby = 'post_' . $orderby;
1135                                 if ( in_array($orderby_array[$i], $allowed_keys) )
1136                                         $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}";
1137                         }
1138                         if ( empty($q['orderby']) )
1139                                 $q['orderby'] = 'post_date '.$q['order'];
1140                 }
1141
1142                 if ( $this->is_attachment ) {
1143                         $where .= " AND post_type = 'attachment'";
1144                 } elseif ($this->is_page) {
1145                         $where .= " AND post_type = 'page'";
1146                 } elseif ($this->is_single) {
1147                         $where .= " AND post_type = 'post'";
1148                 } else {
1149                         $where .= " AND post_type = '$post_type'";
1150                 }
1151
1152                 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
1153                         $q_status = explode(',', $q['post_status']);
1154                         $r_status = array();
1155                         if ( in_array( 'draft'  , $q_status ) )
1156                                 $r_status[] = "post_status = 'draft'";
1157                         if ( in_array( 'pending', $q_status ) )
1158                                 $r_status[] = "post_status = 'pending'";
1159                         if ( in_array( 'future' , $q_status ) )
1160                                 $r_status[] = "post_status = 'future'";
1161                         if ( in_array( 'inherit' , $q_status ) )
1162                                 $r_status[] = "post_status = 'inherit'";
1163                         if ( in_array( 'private', $q_status ) )
1164                                 $r_status[] = "post_status = 'private'";
1165                         if ( in_array( 'publish', $q_status ) )
1166                                 $r_status[] = "post_status = 'publish'";
1167                         if ( !empty($r_status) )
1168                                 $where .= " AND (" . join( ' OR ', $r_status ) . ")";
1169                 } elseif ( !$this->is_singular ) {
1170                         $where .= " AND (post_status = 'publish'";
1171
1172                         if ( is_admin() )
1173                                 $where .= " OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending'";
1174
1175                         if ( is_user_logged_in() ) {
1176                                 $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR post_status = 'private'" : " OR post_author = $user_ID AND post_status = 'private'";
1177                         }
1178
1179                         $where .= ')';
1180                 }
1181
1182                 // Apply filters on where and join prior to paging so that any
1183                 // manipulations to them are reflected in the paging by day queries.
1184                 $where = apply_filters('posts_where', $where);
1185                 $join = apply_filters('posts_join', $join);
1186
1187                 // Paging
1188                 if ( empty($q['nopaging']) && !$this->is_singular ) {
1189                         $page = abs(intval($q['paged']));
1190                         if (empty($page)) {
1191                                 $page = 1;
1192                         }
1193
1194                         if ( empty($q['offset']) ) {
1195                                 $pgstrt = '';
1196                                 $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
1197                                 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
1198                         } else { // we're ignoring $page and using 'offset'
1199                                 $q['offset'] = abs(intval($q['offset']));
1200                                 $pgstrt = $q['offset'] . ', ';
1201                                 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
1202                         }
1203                 }
1204
1205                 // Comments feeds
1206                 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
1207                         if ( $this->is_archive || $this->is_search ) {
1208                                 $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
1209                                 $cwhere = "WHERE comment_approved = '1' $where";
1210                                 $cgroupby = "GROUP BY $wpdb->comments.comment_id";
1211                         } else { // Other non singular e.g. front
1212                                 $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
1213                                 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
1214                                 $cgroupby = '';
1215                         }
1216
1217                         $cjoin = apply_filters('comment_feed_join', $cjoin);
1218                         $cwhere = apply_filters('comment_feed_where', $cwhere);
1219                         $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
1220
1221                         $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss'));
1222                         $this->comment_count = count($this->comments);
1223
1224                         $post_ids = array();
1225
1226                         foreach ($this->comments as $comment)
1227                                 $post_ids[] = (int) $comment->comment_post_ID;
1228
1229                         $post_ids = join(',', $post_ids);
1230                         $join = '';
1231                         if ( $post_ids )
1232                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
1233                         else
1234                                 $where = "AND 0";
1235                 }
1236
1237                 // Apply post-paging filters on where and join.  Only plugins that
1238                 // manipulate paging queries should use these hooks.
1239
1240                 // Announce current selection parameters.  For use by caching plugins.
1241                 do_action( 'posts_selection', $where . $groupby . $q['orderby'] . $limits . $join );
1242
1243                 $where = apply_filters('posts_where_paged', $where);
1244                 $groupby = apply_filters('posts_groupby', $groupby);
1245                 if ( ! empty($groupby) )
1246                         $groupby = 'GROUP BY ' . $groupby;
1247                 $join = apply_filters('posts_join_paged', $join);
1248                 $orderby = apply_filters('posts_orderby', $q['orderby']);
1249                 if ( !empty( $orderby ) )
1250                         $orderby = 'ORDER BY ' . $orderby;
1251                 $distinct = apply_filters('posts_distinct', $distinct);
1252                 $fields = apply_filters('posts_fields', "$wpdb->posts.*");
1253                 $limits = apply_filters( 'post_limits', $limits );
1254                 $found_rows = '';
1255                 if ( !empty($limits) )
1256                         $found_rows = 'SQL_CALC_FOUND_ROWS';
1257
1258                 $request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
1259                 $this->request = apply_filters('posts_request', $request);
1260
1261                 $this->posts = $wpdb->get_results($this->request);
1262                 // Raw results filter.  Prior to status checks.
1263                 $this->posts = apply_filters('posts_results', $this->posts);
1264
1265                 if ( $this->is_comment_feed && $this->is_singular ) {
1266                         $cjoin = apply_filters('comment_feed_join', '');
1267                         $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = {$this->posts[0]->ID} AND comment_approved = '1'");
1268                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss');
1269                         $this->comments = $wpdb->get_results($comments_request);
1270                         $this->comment_count = count($this->comments);
1271                 }
1272
1273                 if ( !empty($limits) ) {
1274                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
1275                         $this->found_posts = $wpdb->get_var( $found_posts_query );
1276                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
1277                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
1278                 }
1279
1280                 // Check post status to determine if post should be displayed.
1281                 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
1282                         $status = get_post_status($this->posts[0]);
1283                         //$type = get_post_type($this->posts[0]);
1284                         if ( ('publish' != $status) ) {
1285                                 if ( ! is_user_logged_in() ) {
1286                                         // User must be logged in to view unpublished posts.
1287                                         $this->posts = array();
1288                                 } else {
1289                                         if  (in_array($status, array('draft', 'pending')) ) {
1290                                                 // User must have edit permissions on the draft to preview.
1291                                                 if (! current_user_can('edit_post', $this->posts[0]->ID)) {
1292                                                         $this->posts = array();
1293                                                 } else {
1294                                                         $this->is_preview = true;
1295                                                         $this->posts[0]->post_date = current_time('mysql');
1296                                                 }
1297                                         }  else if ('future' == $status) {
1298                                                 $this->is_preview = true;
1299                                                 if (!current_user_can('edit_post', $this->posts[0]->ID)) {
1300                                                         $this->posts = array ( );
1301                                                 }
1302                                         } else {
1303                                                 if (! current_user_can('read_post', $this->posts[0]->ID))
1304                                                         $this->posts = array();
1305                                         }
1306                                 }
1307                         }
1308                 }
1309
1310                 $this->posts = apply_filters('the_posts', $this->posts);
1311
1312                 update_post_caches($this->posts);
1313
1314                 $this->post_count = count($this->posts);
1315                 if ($this->post_count > 0) {
1316                         $this->post = $this->posts[0];
1317                 }
1318
1319                 return $this->posts;
1320         }
1321
1322         function next_post() {
1323
1324                 $this->current_post++;
1325
1326                 $this->post = $this->posts[$this->current_post];
1327                 return $this->post;
1328         }
1329
1330         function the_post() {
1331                 global $post;
1332                 $this->in_the_loop = true;
1333                 $post = $this->next_post();
1334                 setup_postdata($post);
1335
1336                 if ( $this->current_post == 0 ) // loop has just started
1337                         do_action('loop_start');
1338         }
1339
1340         function have_posts() {
1341                 if ($this->current_post + 1 < $this->post_count) {
1342                         return true;
1343                 } elseif ($this->current_post + 1 == $this->post_count) {
1344                         do_action('loop_end');
1345                         // Do some cleaning up after the loop
1346                         $this->rewind_posts();
1347                 }
1348
1349                 $this->in_the_loop = false;
1350                 return false;
1351         }
1352
1353         function rewind_posts() {
1354                 $this->current_post = -1;
1355                 if ($this->post_count > 0) {
1356                         $this->post = $this->posts[0];
1357                 }
1358         }
1359
1360         function next_comment() {
1361                 $this->current_comment++;
1362
1363                 $this->comment = $this->comments[$this->current_comment];
1364                 return $this->comment;
1365         }
1366
1367         function the_comment() {
1368                 global $comment;
1369
1370                 $comment = $this->next_comment();
1371
1372                 if ($this->current_comment == 0) {
1373                         do_action('comment_loop_start');
1374                 }
1375         }
1376
1377         function have_comments() {
1378                 if ($this->current_comment + 1 < $this->comment_count) {
1379                         return true;
1380                 } elseif ($this->current_comment + 1 == $this->comment_count) {
1381                         $this->rewind_comments();
1382                 }
1383
1384                 return false;
1385         }
1386
1387         function rewind_comments() {
1388                 $this->current_comment = -1;
1389                 if ($this->comment_count > 0) {
1390                         $this->comment = $this->comments[0];
1391                 }
1392         }
1393
1394         function &query($query) {
1395                 $this->parse_query($query);
1396                 return $this->get_posts();
1397         }
1398
1399         function get_queried_object() {
1400                 if (isset($this->queried_object)) {
1401                         return $this->queried_object;
1402                 }
1403
1404                 $this->queried_object = NULL;
1405                 $this->queried_object_id = 0;
1406
1407                 if ($this->is_category) {
1408                         $cat = $this->get('cat');
1409                         $category = &get_category($cat);
1410                         $this->queried_object = &$category;
1411                         $this->queried_object_id = (int) $cat;
1412                 } else if ($this->is_tag) {
1413                         $tag_id = $this->get('tag_id');
1414                         $tag = &get_term($tag_id, 'post_tag');
1415                         if ( is_wp_error( $tag ) )
1416                                 return $tag;
1417                         $this->queried_object = &$tag;
1418                         $this->queried_object_id = (int) $tag_id;
1419                 } else if ($this->is_posts_page) {
1420                         $this->queried_object = & get_page(get_option('page_for_posts'));
1421                         $this->queried_object_id = (int) $this->queried_object->ID;
1422                 } else if ($this->is_single) {
1423                         $this->queried_object = $this->post;
1424                         $this->queried_object_id = (int) $this->post->ID;
1425                 } else if ($this->is_page) {
1426                         $this->queried_object = $this->post;
1427                         $this->queried_object_id = (int) $this->post->ID;
1428                 } else if ($this->is_author) {
1429                         $author_id = (int) $this->get('author');
1430                         $author = get_userdata($author_id);
1431                         $this->queried_object = $author;
1432                         $this->queried_object_id = $author_id;
1433                 }
1434
1435                 return $this->queried_object;
1436         }
1437
1438         function get_queried_object_id() {
1439                 $this->get_queried_object();
1440
1441                 if (isset($this->queried_object_id)) {
1442                         return $this->queried_object_id;
1443                 }
1444
1445                 return 0;
1446         }
1447
1448         function WP_Query ($query = '') {
1449                 if (! empty($query)) {
1450                         $this->query($query);
1451                 }
1452         }
1453 }
1454
1455
1456 // Redirect old slugs
1457 function wp_old_slug_redirect () {
1458         global $wp_query;
1459         if ( is_404() && '' != $wp_query->query_vars['name'] ) :
1460                 global $wpdb;
1461
1462                 $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'] . "'";
1463
1464                 // if year, monthnum, or day have been specified, make our query more precise
1465                 // just in case there are multiple identical _wp_old_slug values
1466                 if ( '' != $wp_query->query_vars['year'] )
1467                         $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
1468                 if ( '' != $wp_query->query_vars['monthnum'] )
1469                         $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
1470                 if ( '' != $wp_query->query_vars['day'] )
1471                         $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
1472
1473                 $id = (int) $wpdb->get_var($query);
1474
1475                 if ( !$id )
1476                         return;
1477
1478                 $link = get_permalink($id);
1479
1480                 if ( !$link )
1481                         return;
1482
1483                 wp_redirect($link, '301'); // Permanent redirect
1484                 exit;
1485         endif;
1486 }
1487
1488
1489 //
1490 // Private helper functions
1491 //
1492
1493 // Setup global post data.
1494 function setup_postdata($post) {
1495         global $id, $postdata, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages, $wp_query;
1496         global $pagenow;
1497
1498         $id = (int) $post->ID;
1499
1500         $authordata = get_userdata($post->post_author);
1501
1502         $day = mysql2date('d.m.y', $post->post_date);
1503         $currentmonth = mysql2date('m', $post->post_date);
1504         $numpages = 1;
1505         $page = get_query_var('page');
1506         if ( !$page )
1507                 $page = 1;
1508         if ( is_single() || is_page() )
1509                 $more = 1;
1510         $content = $post->post_content;
1511         if ( preg_match('/<!--nextpage-->/', $content) ) {
1512                 if ( $page > 1 )
1513                         $more = 1;
1514                 $multipage = 1;
1515                 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
1516                 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
1517                 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
1518                 $pages = explode('<!--nextpage-->', $content);
1519                 $numpages = count($pages);
1520         } else {
1521                 $pages[0] = $post->post_content;
1522                 $multipage = 0;
1523         }
1524         return true;
1525 }
1526
1527 ?>