]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/query.php
Wordpress 2.8
[autoinstalls/wordpress.git] / wp-includes / query.php
1 <?php
2 /**
3  * WordPress Query API
4  *
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.
7  *
8  * @link http://codex.wordpress.org/The_Loop More information on The Loop.
9  *
10  * @package WordPress
11  * @subpackage Query
12  */
13
14 /**
15  * Retrieve variable in the WP_Query class.
16  *
17  * @see WP_Query::get()
18  * @since 1.5.0
19  * @uses $wp_query
20  *
21  * @param string $var The variable key to retrieve.
22  * @return mixed
23  */
24 function get_query_var($var) {
25         global $wp_query;
26
27         return $wp_query->get($var);
28 }
29
30 /**
31  * Set query variable.
32  *
33  * @see WP_Query::set()
34  * @since 2.2.0
35  * @uses $wp_query
36  *
37  * @param string $var Query variable key.
38  * @param mixed $value
39  * @return null
40  */
41 function set_query_var($var, $value) {
42         global $wp_query;
43
44         return $wp_query->set($var, $value);
45 }
46
47 /**
48  * Setup The Loop with query parameters.
49  *
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.
52  *
53  * @since 1.5.0
54  * @uses $wp_query
55  *
56  * @param string $query
57  * @return array List of posts
58  */
59 function &query_posts($query) {
60         unset($GLOBALS['wp_query']);
61         $GLOBALS['wp_query'] =& new WP_Query();
62         return $GLOBALS['wp_query']->query($query);
63 }
64
65 /**
66  * Destroy the previous query and setup a new query.
67  *
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.
71  *
72  * @since 2.3.0
73  * @uses $wp_query
74  */
75 function wp_reset_query() {
76         unset($GLOBALS['wp_query']);
77         $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
78         global $wp_query;
79         if ( !empty($wp_query->post) ) {
80                 $GLOBALS['post'] = $wp_query->post;
81                 setup_postdata($wp_query->post);
82         }
83 }
84
85 /*
86  * Query type checks.
87  */
88
89 /**
90  * Whether the current request is in WordPress admin Panel
91  *
92  * Does not inform on whether the user is an admin! Use capability checks to
93  * tell if the user should be accessing a section or not.
94  *
95  * @since 1.5.1
96  *
97  * @return bool True if inside WordPress administration pages.
98  */
99 function is_admin () {
100         if ( defined('WP_ADMIN') )
101                 return WP_ADMIN;
102         return false;
103 }
104
105 /**
106  * Is query requesting an archive page.
107  *
108  * @since 1.5.0
109  * @uses $wp_query
110  *
111  * @return bool True if page is archive.
112  */
113 function is_archive () {
114         global $wp_query;
115
116         return $wp_query->is_archive;
117 }
118
119 /**
120  * Is query requesting an attachment page.
121  *
122  * @since 2.0.0
123  * @uses $wp_query
124  *
125  * @return bool True if page is attachment.
126  */
127 function is_attachment () {
128         global $wp_query;
129
130         return $wp_query->is_attachment;
131 }
132
133 /**
134  * Is query requesting an author page.
135  *
136  * If the $author parameter is specified then the check will be expanded to
137  * include whether the queried author matches the one given in the parameter.
138  * You can match against integers and against strings.
139  *
140  * If matching against an integer, the ID should be used of the author for the
141  * test. If the $author is an ID and matches the author page user ID, then
142  * 'true' will be returned.
143  *
144  * If matching against strings, then the test will be matched against both the
145  * nickname and user nicename and will return true on success.
146  *
147  * @since 1.5.0
148  * @uses $wp_query
149  *
150  * @param string|int $author Optional. Is current page this author.
151  * @return bool True if page is author or $author (if set).
152  */
153 function is_author ($author = '') {
154         global $wp_query;
155
156         if ( !$wp_query->is_author )
157                 return false;
158
159         if ( empty($author) )
160                 return true;
161
162         $author_obj = $wp_query->get_queried_object();
163
164         $author = (array) $author;
165
166         if ( in_array( $author_obj->ID, $author ) )
167                 return true;
168         elseif ( in_array( $author_obj->nickname, $author ) )
169                 return true;
170         elseif ( in_array( $author_obj->user_nicename, $author ) )
171                 return true;
172
173         return false;
174 }
175
176 /**
177  * Whether current page query contains a category name or given category name.
178  *
179  * The category list can contain category IDs, names, or category slugs. If any
180  * of them are part of the query, then it will return true.
181  *
182  * @since 1.5.0
183  * @uses $wp_query
184  *
185  * @param string|array $category Optional.
186  * @return bool
187  */
188 function is_category ($category = '') {
189         global $wp_query;
190
191         if ( !$wp_query->is_category )
192                 return false;
193
194         if ( empty($category) )
195                 return true;
196
197         $cat_obj = $wp_query->get_queried_object();
198
199         $category = (array) $category;
200
201         if ( in_array( $cat_obj->term_id, $category ) )
202                 return true;
203         elseif ( in_array( $cat_obj->name, $category ) )
204                 return true;
205         elseif ( in_array( $cat_obj->slug, $category ) )
206                 return true;
207
208         return false;
209 }
210
211 /**
212  * Whether the current page query has the given tag slug or contains tag.
213  *
214  * @since 2.3.0
215  * @uses $wp_query
216  *
217  * @param string|array $slug Optional. Single tag or list of tags to check for.
218  * @return bool
219  */
220 function is_tag( $slug = '' ) {
221         global $wp_query;
222
223         if ( !$wp_query->is_tag )
224                 return false;
225
226         if ( empty( $slug ) )
227                 return true;
228
229         $tag_obj = $wp_query->get_queried_object();
230
231         $slug = (array) $slug;
232
233         if ( in_array( $tag_obj->slug, $slug ) )
234                 return true;
235
236         return false;
237 }
238
239 /**
240  * Whether the current page query has the given taxonomy slug or contains taxonomy.
241  *
242  * @since 2.5.0
243  * @uses $wp_query
244  *
245  * @param string|array $slug Optional. Slug or slugs to check in current query.
246  * @return bool
247  */
248 function is_tax( $slug = '' ) {
249         global $wp_query;
250
251         if ( !$wp_query->is_tax )
252                 return false;
253
254         if ( empty($slug) )
255                 return true;
256
257         $term = $wp_query->get_queried_object();
258
259         $slug = (array) $slug;
260
261         if ( in_array( $term->slug, $slug ) )
262                 return true;
263
264         return false;
265 }
266
267 /**
268  * Whether the current URL is within the comments popup window.
269  *
270  * @since 1.5.0
271  * @uses $wp_query
272  *
273  * @return bool
274  */
275 function is_comments_popup () {
276         global $wp_query;
277
278         return $wp_query->is_comments_popup;
279 }
280
281 /**
282  * Whether current URL is based on a date.
283  *
284  * @since 1.5.0
285  * @uses $wp_query
286  *
287  * @return bool
288  */
289 function is_date () {
290         global $wp_query;
291
292         return $wp_query->is_date;
293 }
294
295 /**
296  * Whether current blog URL contains a day.
297  *
298  * @since 1.5.0
299  * @uses $wp_query
300  *
301  * @return bool
302  */
303 function is_day () {
304         global $wp_query;
305
306         return $wp_query->is_day;
307 }
308
309 /**
310  * Whether current page query is feed URL.
311  *
312  * @since 1.5.0
313  * @uses $wp_query
314  *
315  * @return bool
316  */
317 function is_feed () {
318         global $wp_query;
319
320         return $wp_query->is_feed;
321 }
322
323 /**
324  * Whether current page query is the front of the site.
325  *
326  * @since 2.5.0
327  * @uses is_home()
328  * @uses get_option()
329  *
330  * @return bool True, if front of site.
331  */
332 function is_front_page () {
333         // most likely case
334         if ( 'posts' == get_option('show_on_front') && is_home() )
335                 return true;
336         elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
337                 return true;
338         else
339                 return false;
340 }
341
342 /**
343  * Whether current page view is the blog homepage.
344  *
345  * @since 1.5.0
346  * @uses $wp_query
347  *
348  * @return bool True if blog view homepage.
349  */
350 function is_home () {
351         global $wp_query;
352
353         return $wp_query->is_home;
354 }
355
356 /**
357  * Whether current page query contains a month.
358  *
359  * @since 1.5.0
360  * @uses $wp_query
361  *
362  * @return bool
363  */
364 function is_month () {
365         global $wp_query;
366
367         return $wp_query->is_month;
368 }
369
370 /**
371  * Whether query is page or contains given page(s).
372  *
373  * Calls the function without any parameters will only test whether the current
374  * query is of the page type. Either a list or a single item can be tested
375  * against for whether the query is a page and also is the value or one of the
376  * values in the page parameter.
377  *
378  * The parameter can contain the page ID, page title, or page name. The
379  * parameter can also be an array of those three values.
380  *
381  * @since 1.5.0
382  * @uses $wp_query
383  *
384  * @param mixed $page Either page or list of pages to test against.
385  * @return bool
386  */
387 function is_page ($page = '') {
388         global $wp_query;
389
390         if ( !$wp_query->is_page )
391                 return false;
392
393         if ( empty($page) )
394                 return true;
395
396         $page_obj = $wp_query->get_queried_object();
397
398         $page = (array) $page;
399
400         if ( in_array( $page_obj->ID, $page ) )
401                 return true;
402         elseif ( in_array( $page_obj->post_title, $page ) )
403                 return true;
404         else if ( in_array( $page_obj->post_name, $page ) )
405                 return true;
406
407         return false;
408 }
409
410 /**
411  * Whether query contains multiple pages for the results.
412  *
413  * @since 1.5.0
414  * @uses $wp_query
415  *
416  * @return bool
417  */
418 function is_paged () {
419         global $wp_query;
420
421         return $wp_query->is_paged;
422 }
423
424 /**
425  * Whether the current page was created by a plugin.
426  *
427  * The plugin can set this by using the global $plugin_page and setting it to
428  * true.
429  *
430  * @since 1.5.0
431  * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
432  *
433  * @return bool
434  */
435 function is_plugin_page() {
436         global $plugin_page;
437
438         if ( isset($plugin_page) )
439                 return true;
440
441         return false;
442 }
443
444 /**
445  * Whether the current query is preview of post or page.
446  *
447  * @since 2.0.0
448  * @uses $wp_query
449  *
450  * @return bool
451  */
452 function is_preview() {
453         global $wp_query;
454
455         return $wp_query->is_preview;
456 }
457
458 /**
459  * Whether the current query post is robots.
460  *
461  * @since 2.1.0
462  * @uses $wp_query
463  *
464  * @return bool
465  */
466 function is_robots() {
467         global $wp_query;
468
469         return $wp_query->is_robots;
470 }
471
472 /**
473  * Whether current query is the result of a user search.
474  *
475  * @since 1.5.0
476  * @uses $wp_query
477  *
478  * @return bool
479  */
480 function is_search () {
481         global $wp_query;
482
483         return $wp_query->is_search;
484 }
485
486 /**
487  * Whether the current page query is single page.
488  *
489  * The parameter can contain the post ID, post title, or post name. The
490  * parameter can also be an array of those three values.
491  *
492  * This applies to other post types, attachments, pages, posts. Just means that
493  * the current query has only a single object.
494  *
495  * @since 1.5.0
496  * @uses $wp_query
497  *
498  * @param mixed $post Either post or list of posts to test against.
499  * @return bool
500  */
501 function is_single ($post = '') {
502         global $wp_query;
503
504         if ( !$wp_query->is_single )
505                 return false;
506
507         if ( empty( $post) )
508                 return true;
509
510         $post_obj = $wp_query->get_queried_object();
511
512         $post = (array) $post;
513
514         if ( in_array( $post_obj->ID, $post ) )
515                 return true;
516         elseif ( in_array( $post_obj->post_title, $post ) )
517                 return true;
518         elseif ( in_array( $post_obj->post_name, $post ) )
519                 return true;
520
521         return false;
522 }
523
524 /**
525  * Whether is single post, is a page, or is an attachment.
526  *
527  * @since 1.5.0
528  * @uses $wp_query
529  *
530  * @return bool
531  */
532 function is_singular() {
533         global $wp_query;
534
535         return $wp_query->is_singular;
536 }
537
538 /**
539  * Whether the query contains a time.
540  *
541  * @since 1.5.0
542  * @uses $wp_query
543  *
544  * @return bool
545  */
546 function is_time () {
547         global $wp_query;
548
549         return $wp_query->is_time;
550 }
551
552 /**
553  * Whether the query is a trackback.
554  *
555  * @since 1.5.0
556  * @uses $wp_query
557  *
558  * @return bool
559  */
560 function is_trackback () {
561         global $wp_query;
562
563         return $wp_query->is_trackback;
564 }
565
566 /**
567  * Whether the query contains a year.
568  *
569  * @since 1.5.0
570  * @uses $wp_query
571  *
572  * @return bool
573  */
574 function is_year () {
575         global $wp_query;
576
577         return $wp_query->is_year;
578 }
579
580 /**
581  * Whether current page query is a 404 and no results for WordPress query.
582  *
583  * @since 1.5.0
584  * @uses $wp_query
585  *
586  * @return bool True, if nothing is found matching WordPress Query.
587  */
588 function is_404 () {
589         global $wp_query;
590
591         return $wp_query->is_404;
592 }
593
594 /*
595  * The Loop.  Post loop control.
596  */
597
598 /**
599  * Whether current WordPress query has results to loop over.
600  *
601  * @see WP_Query::have_posts()
602  * @since 1.5.0
603  * @uses $wp_query
604  *
605  * @return bool
606  */
607 function have_posts() {
608         global $wp_query;
609
610         return $wp_query->have_posts();
611 }
612
613 /**
614  * Whether the caller is in the Loop.
615  *
616  * @since 2.0.0
617  * @uses $wp_query
618  *
619  * @return bool True if caller is within loop, false if loop hasn't started or ended.
620  */
621 function in_the_loop() {
622         global $wp_query;
623
624         return $wp_query->in_the_loop;
625 }
626
627 /**
628  * Rewind the loop posts.
629  *
630  * @see WP_Query::rewind_posts()
631  * @since 1.5.0
632  * @uses $wp_query
633  *
634  * @return null
635  */
636 function rewind_posts() {
637         global $wp_query;
638
639         return $wp_query->rewind_posts();
640 }
641
642 /**
643  * Iterate the post index in the loop.
644  *
645  * @see WP_Query::the_post()
646  * @since 1.5.0
647  * @uses $wp_query
648  */
649 function the_post() {
650         global $wp_query;
651
652         $wp_query->the_post();
653 }
654
655 /*
656  * Comments loop.
657  */
658
659 /**
660  * Whether there are comments to loop over.
661  *
662  * @see WP_Query::have_comments()
663  * @since 2.2.0
664  * @uses $wp_query
665  *
666  * @return bool
667  */
668 function have_comments() {
669         global $wp_query;
670         return $wp_query->have_comments();
671 }
672
673 /**
674  * Iterate comment index in the comment loop.
675  *
676  * @see WP_Query::the_comment()
677  * @since 2.2.0
678  * @uses $wp_query
679  *
680  * @return object
681  */
682 function the_comment() {
683         global $wp_query;
684         return $wp_query->the_comment();
685 }
686
687 /*
688  * WP_Query
689  */
690
691 /**
692  * The WordPress Query class.
693  *
694  * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
695  *
696  * @since 1.5.0
697  */
698 class WP_Query {
699
700         /**
701          * Query string
702          *
703          * @since 1.5.0
704          * @access public
705          * @var string
706          */
707         var $query;
708
709         /**
710          * Query search variables set by the user.
711          *
712          * @since 1.5.0
713          * @access public
714          * @var array
715          */
716         var $query_vars = array();
717
718         /**
719          * Holds the data for a single object that is queried.
720          *
721          * Holds the contents of a post, page, category, attachment.
722          *
723          * @since 1.5.0
724          * @access public
725          * @var object|array
726          */
727         var $queried_object;
728
729         /**
730          * The ID of the queried object.
731          *
732          * @since 1.5.0
733          * @access public
734          * @var int
735          */
736         var $queried_object_id;
737
738         /**
739          * Get post database query.
740          *
741          * @since 2.0.1
742          * @access public
743          * @var string
744          */
745         var $request;
746
747         /**
748          * List of posts.
749          *
750          * @since 1.5.0
751          * @access public
752          * @var array
753          */
754         var $posts;
755
756         /**
757          * The amount of posts for the current query.
758          *
759          * @since 1.5.0
760          * @access public
761          * @var int
762          */
763         var $post_count = 0;
764
765         /**
766          * Index of the current item in the loop.
767          *
768          * @since 1.5.0
769          * @access public
770          * @var int
771          */
772         var $current_post = -1;
773
774         /**
775          * Whether the loop has started and the caller is in the loop.
776          *
777          * @since 2.0.0
778          * @access public
779          * @var bool
780          */
781         var $in_the_loop = false;
782
783         /**
784          * The current post ID.
785          *
786          * @since 1.5.0
787          * @access public
788          * @var int
789          */
790         var $post;
791
792         /**
793          * The list of comments for current post.
794          *
795          * @since 2.2.0
796          * @access public
797          * @var array
798          */
799         var $comments;
800
801         /**
802          * The amount of comments for the posts.
803          *
804          * @since 2.2.0
805          * @access public
806          * @var int
807          */
808         var $comment_count = 0;
809
810         /**
811          * The index of the comment in the comment loop.
812          *
813          * @since 2.2.0
814          * @access public
815          * @var int
816          */
817         var $current_comment = -1;
818
819         /**
820          * Current comment ID.
821          *
822          * @since 2.2.0
823          * @access public
824          * @var int
825          */
826         var $comment;
827
828         /**
829          * Amount of posts if limit clause was not used.
830          *
831          * @since 2.1.0
832          * @access public
833          * @var int
834          */
835         var $found_posts = 0;
836
837         /**
838          * The amount of pages.
839          *
840          * @since 2.1.0
841          * @access public
842          * @var int
843          */
844         var $max_num_pages = 0;
845
846         /**
847          * The amount of comment pages.
848          *
849          * @since 2.7.0
850          * @access public
851          * @var int
852          */
853         var $max_num_comment_pages = 0;
854
855         /**
856          * Set if query is single post.
857          *
858          * @since 1.5.0
859          * @access public
860          * @var bool
861          */
862         var $is_single = false;
863
864         /**
865          * Set if query is preview of blog.
866          *
867          * @since 2.0.0
868          * @access public
869          * @var bool
870          */
871         var $is_preview = false;
872
873         /**
874          * Set if query returns a page.
875          *
876          * @since 1.5.0
877          * @access public
878          * @var bool
879          */
880         var $is_page = false;
881
882         /**
883          * Set if query is an archive list.
884          *
885          * @since 1.5.0
886          * @access public
887          * @var bool
888          */
889         var $is_archive = false;
890
891         /**
892          * Set if query is part of a date.
893          *
894          * @since 1.5.0
895          * @access public
896          * @var bool
897          */
898         var $is_date = false;
899
900         /**
901          * Set if query contains a year.
902          *
903          * @since 1.5.0
904          * @access public
905          * @var bool
906          */
907         var $is_year = false;
908
909         /**
910          * Set if query contains a month.
911          *
912          * @since 1.5.0
913          * @access public
914          * @var bool
915          */
916         var $is_month = false;
917
918         /**
919          * Set if query contains a day.
920          *
921          * @since 1.5.0
922          * @access public
923          * @var bool
924          */
925         var $is_day = false;
926
927         /**
928          * Set if query contains time.
929          *
930          * @since 1.5.0
931          * @access public
932          * @var bool
933          */
934         var $is_time = false;
935
936         /**
937          * Set if query contains an author.
938          *
939          * @since 1.5.0
940          * @access public
941          * @var bool
942          */
943         var $is_author = false;
944
945         /**
946          * Set if query contains category.
947          *
948          * @since 1.5.0
949          * @access public
950          * @var bool
951          */
952         var $is_category = false;
953
954         /**
955          * Set if query contains tag.
956          *
957          * @since 2.3.0
958          * @access public
959          * @var bool
960          */
961         var $is_tag = false;
962
963         /**
964          * Set if query contains taxonomy.
965          *
966          * @since 2.5.0
967          * @access public
968          * @var bool
969          */
970         var $is_tax = false;
971
972         /**
973          * Set if query was part of a search result.
974          *
975          * @since 1.5.0
976          * @access public
977          * @var bool
978          */
979         var $is_search = false;
980
981         /**
982          * Set if query is feed display.
983          *
984          * @since 1.5.0
985          * @access public
986          * @var bool
987          */
988         var $is_feed = false;
989
990         /**
991          * Set if query is comment feed display.
992          *
993          * @since 2.2.0
994          * @access public
995          * @var bool
996          */
997         var $is_comment_feed = false;
998
999         /**
1000          * Set if query is trackback.
1001          *
1002          * @since 1.5.0
1003          * @access public
1004          * @var bool
1005          */
1006         var $is_trackback = false;
1007
1008         /**
1009          * Set if query is blog homepage.
1010          *
1011          * @since 1.5.0
1012          * @access public
1013          * @var bool
1014          */
1015         var $is_home = false;
1016
1017         /**
1018          * Set if query couldn't found anything.
1019          *
1020          * @since 1.5.0
1021          * @access public
1022          * @var bool
1023          */
1024         var $is_404 = false;
1025
1026         /**
1027          * Set if query is within comments popup window.
1028          *
1029          * @since 1.5.0
1030          * @access public
1031          * @var bool
1032          */
1033         var $is_comments_popup = false;
1034
1035         /**
1036          * Set if query is part of administration page.
1037          *
1038          * @since 1.5.0
1039          * @access public
1040          * @var bool
1041          */
1042         var $is_admin = false;
1043
1044         /**
1045          * Set if query is an attachment.
1046          *
1047          * @since 2.0.0
1048          * @access public
1049          * @var bool
1050          */
1051         var $is_attachment = false;
1052
1053         /**
1054          * Set if is single, is a page, or is an attachment.
1055          *
1056          * @since 2.1.0
1057          * @access public
1058          * @var bool
1059          */
1060         var $is_singular = false;
1061
1062         /**
1063          * Set if query is for robots.
1064          *
1065          * @since 2.1.0
1066          * @access public
1067          * @var bool
1068          */
1069         var $is_robots = false;
1070
1071         /**
1072          * Set if query contains posts.
1073          *
1074          * Basically, the homepage if the option isn't set for the static homepage.
1075          *
1076          * @since 2.1.0
1077          * @access public
1078          * @var bool
1079          */
1080         var $is_posts_page = false;
1081
1082         /**
1083          * Resets query flags to false.
1084          *
1085          * The query flags are what page info WordPress was able to figure out.
1086          *
1087          * @since 2.0.0
1088          * @access private
1089          */
1090         function init_query_flags() {
1091                 $this->is_single = false;
1092                 $this->is_page = false;
1093                 $this->is_archive = false;
1094                 $this->is_date = false;
1095                 $this->is_year = false;
1096                 $this->is_month = false;
1097                 $this->is_day = false;
1098                 $this->is_time = false;
1099                 $this->is_author = false;
1100                 $this->is_category = false;
1101                 $this->is_tag = false;
1102                 $this->is_tax = false;
1103                 $this->is_search = false;
1104                 $this->is_feed = false;
1105                 $this->is_comment_feed = false;
1106                 $this->is_trackback = false;
1107                 $this->is_home = false;
1108                 $this->is_404 = false;
1109                 $this->is_paged = false;
1110                 $this->is_admin = false;
1111                 $this->is_attachment = false;
1112                 $this->is_singular = false;
1113                 $this->is_robots = false;
1114                 $this->is_posts_page = false;
1115         }
1116
1117         /**
1118          * Initiates object properties and sets default values.
1119          *
1120          * @since 1.5.0
1121          * @access public
1122          */
1123         function init () {
1124                 unset($this->posts);
1125                 unset($this->query);
1126                 $this->query_vars = array();
1127                 unset($this->queried_object);
1128                 unset($this->queried_object_id);
1129                 $this->post_count = 0;
1130                 $this->current_post = -1;
1131                 $this->in_the_loop = false;
1132
1133                 $this->init_query_flags();
1134         }
1135
1136         /**
1137          * Reparse the query vars.
1138          *
1139          * @since 1.5.0
1140          * @access public
1141          */
1142         function parse_query_vars() {
1143                 $this->parse_query('');
1144         }
1145
1146         /**
1147          * Fills in the query variables, which do not exist within the parameter.
1148          *
1149          * @since 2.1.0
1150          * @access public
1151          *
1152          * @param array $array Defined query variables.
1153          * @return array Complete query variables with undefined ones filled in empty.
1154          */
1155         function fill_query_vars($array) {
1156                 $keys = array(
1157                         'error'
1158                         , 'm'
1159                         , 'p'
1160                         , 'post_parent'
1161                         , 'subpost'
1162                         , 'subpost_id'
1163                         , 'attachment'
1164                         , 'attachment_id'
1165                         , 'name'
1166                         , 'hour'
1167                         , 'static'
1168                         , 'pagename'
1169                         , 'page_id'
1170                         , 'second'
1171                         , 'minute'
1172                         , 'hour'
1173                         , 'day'
1174                         , 'monthnum'
1175                         , 'year'
1176                         , 'w'
1177                         , 'category_name'
1178                         , 'tag'
1179                         , 'cat'
1180                         , 'tag_id'
1181                         , 'author_name'
1182                         , 'feed'
1183                         , 'tb'
1184                         , 'paged'
1185                         , 'comments_popup'
1186                         , 'meta_key'
1187                         , 'meta_value'
1188                         , 'preview'
1189                 );
1190
1191                 foreach ($keys as $key) {
1192                         if ( !isset($array[$key]))
1193                                 $array[$key] = '';
1194                 }
1195
1196                 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1197                         'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
1198
1199                 foreach ( $array_keys as $key ) {
1200                         if ( !isset($array[$key]))
1201                                 $array[$key] = array();
1202                 }
1203                 return $array;
1204         }
1205
1206         /**
1207          * Parse a query string and set query type booleans.
1208          *
1209          * @since 1.5.0
1210          * @access public
1211          *
1212          * @param string|array $query
1213          */
1214         function parse_query ($query) {
1215                 if ( !empty($query) || !isset($this->query) ) {
1216                         $this->init();
1217                         if ( is_array($query) )
1218                                 $this->query_vars = $query;
1219                         else
1220                                 parse_str($query, $this->query_vars);
1221                         $this->query = $query;
1222                 }
1223
1224                 $this->query_vars = $this->fill_query_vars($this->query_vars);
1225                 $qv = &$this->query_vars;
1226
1227                 if ( ! empty($qv['robots']) )
1228                         $this->is_robots = true;
1229
1230                 $qv['p'] =  absint($qv['p']);
1231                 $qv['page_id'] =  absint($qv['page_id']);
1232                 $qv['year'] = absint($qv['year']);
1233                 $qv['monthnum'] = absint($qv['monthnum']);
1234                 $qv['day'] = absint($qv['day']);
1235                 $qv['w'] = absint($qv['w']);
1236                 $qv['m'] = absint($qv['m']);
1237                 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1238                 $qv['pagename'] = trim( $qv['pagename'] );
1239                 $qv['name'] = trim( $qv['name'] );
1240                 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1241                 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1242                 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1243
1244                 // Compat.  Map subpost to attachment.
1245                 if ( '' != $qv['subpost'] )
1246                         $qv['attachment'] = $qv['subpost'];
1247                 if ( '' != $qv['subpost_id'] )
1248                         $qv['attachment_id'] = $qv['subpost_id'];
1249
1250                 $qv['attachment_id'] = absint($qv['attachment_id']);
1251
1252                 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1253                         $this->is_single = true;
1254                         $this->is_attachment = true;
1255                 } elseif ( '' != $qv['name'] ) {
1256                         $this->is_single = true;
1257                 } elseif ( $qv['p'] ) {
1258                         $this->is_single = true;
1259                 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1260                         // If year, month, day, hour, minute, and second are set, a single
1261                         // post is being queried.
1262                         $this->is_single = true;
1263                 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1264                         $this->is_page = true;
1265                         $this->is_single = false;
1266                 } elseif ( !empty($qv['s']) ) {
1267                         $this->is_search = true;
1268                 } else {
1269                 // Look for archive queries.  Dates, categories, authors.
1270
1271                         if ( '' !== $qv['second'] ) {
1272                                 $this->is_time = true;
1273                                 $this->is_date = true;
1274                         }
1275
1276                         if ( '' !== $qv['minute'] ) {
1277                                 $this->is_time = true;
1278                                 $this->is_date = true;
1279                         }
1280
1281                         if ( '' !== $qv['hour'] ) {
1282                                 $this->is_time = true;
1283                                 $this->is_date = true;
1284                         }
1285
1286                         if ( $qv['day'] ) {
1287                                 if (! $this->is_date) {
1288                                         $this->is_day = true;
1289                                         $this->is_date = true;
1290                                 }
1291                         }
1292
1293                         if ( $qv['monthnum'] ) {
1294                                 if (! $this->is_date) {
1295                                         $this->is_month = true;
1296                                         $this->is_date = true;
1297                                 }
1298                         }
1299
1300                         if ( $qv['year'] ) {
1301                                 if (! $this->is_date) {
1302                                         $this->is_year = true;
1303                                         $this->is_date = true;
1304                                 }
1305                         }
1306
1307                         if ( $qv['m'] ) {
1308                                 $this->is_date = true;
1309                                 if (strlen($qv['m']) > 9) {
1310                                         $this->is_time = true;
1311                                 } else if (strlen($qv['m']) > 7) {
1312                                         $this->is_day = true;
1313                                 } else if (strlen($qv['m']) > 5) {
1314                                         $this->is_month = true;
1315                                 } else {
1316                                         $this->is_year = true;
1317                                 }
1318                         }
1319
1320                         if ('' != $qv['w']) {
1321                                 $this->is_date = true;
1322                         }
1323
1324                         if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
1325                                 $this->is_category = false;
1326                         } else {
1327                                 if (strpos($qv['cat'], '-') !== false) {
1328                                         $this->is_category = false;
1329                                 } else {
1330                                         $this->is_category = true;
1331                                 }
1332                         }
1333
1334                         if ( '' != $qv['category_name'] ) {
1335                                 $this->is_category = true;
1336                         }
1337
1338                         if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
1339                                 $qv['category__in'] = array();
1340                         } else {
1341                                 $qv['category__in'] = array_map('absint', $qv['category__in']);
1342                                 $this->is_category = true;
1343                         }
1344
1345                         if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
1346                                 $qv['category__not_in'] = array();
1347                         } else {
1348                                 $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
1349                         }
1350
1351                         if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
1352                                 $qv['category__and'] = array();
1353                         } else {
1354                                 $qv['category__and'] = array_map('absint', $qv['category__and']);
1355                                 $this->is_category = true;
1356                         }
1357
1358                         if (  '' != $qv['tag'] )
1359                                 $this->is_tag = true;
1360
1361                         $qv['tag_id'] = absint($qv['tag_id']);
1362                         if (  !empty($qv['tag_id']) )
1363                                 $this->is_tag = true;
1364
1365                         if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
1366                                 $qv['tag__in'] = array();
1367                         } else {
1368                                 $qv['tag__in'] = array_map('absint', $qv['tag__in']);
1369                                 $this->is_tag = true;
1370                         }
1371
1372                         if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
1373                                 $qv['tag__not_in'] = array();
1374                         } else {
1375                                 $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
1376                         }
1377
1378                         if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
1379                                 $qv['tag__and'] = array();
1380                         } else {
1381                                 $qv['tag__and'] = array_map('absint', $qv['tag__and']);
1382                                 $this->is_category = true;
1383                         }
1384
1385                         if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
1386                                 $qv['tag_slug__in'] = array();
1387                         } else {
1388                                 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
1389                                 $this->is_tag = true;
1390                         }
1391
1392                         if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
1393                                 $qv['tag_slug__and'] = array();
1394                         } else {
1395                                 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
1396                                 $this->is_tag = true;
1397                         }
1398
1399                         if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
1400                                 $this->is_tax = false;
1401                                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1402                                         if ( $t->query_var && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
1403                                                 $qv['taxonomy'] = $taxonomy;
1404                                                 $qv['term'] = $qv[$t->query_var];
1405                                                 $this->is_tax = true;
1406                                                 break;
1407                                         }
1408                                 }
1409                         } else {
1410                                 $this->is_tax = true;
1411                         }
1412
1413                         if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1414                                 $this->is_author = false;
1415                         } else {
1416                                 $this->is_author = true;
1417                         }
1418
1419                         if ( '' != $qv['author_name'] ) {
1420                                 $this->is_author = true;
1421                         }
1422
1423                         if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
1424                                 $this->is_archive = true;
1425                 }
1426
1427                 if ( '' != $qv['feed'] )
1428                         $this->is_feed = true;
1429
1430                 if ( '' != $qv['tb'] )
1431                         $this->is_trackback = true;
1432
1433                 if ( '' != $qv['paged'] )
1434                         $this->is_paged = true;
1435
1436                 if ( '' != $qv['comments_popup'] )
1437                         $this->is_comments_popup = true;
1438
1439                 // if we're previewing inside the write screen
1440                 if ('' != $qv['preview'])
1441                         $this->is_preview = true;
1442
1443                 if ( is_admin() )
1444                         $this->is_admin = true;
1445
1446                 if ( false !== strpos($qv['feed'], 'comments-') ) {
1447                         $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1448                         $qv['withcomments'] = 1;
1449                 }
1450
1451                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1452
1453                 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1454                         $this->is_comment_feed = true;
1455
1456                 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 ) )
1457                         $this->is_home = true;
1458
1459                 // Correct is_* for page_on_front and page_for_posts
1460                 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1461                         $this->is_page = true;
1462                         $this->is_home = false;
1463                         $qv['page_id'] = get_option('page_on_front');
1464                 }
1465
1466                 if ( '' != $qv['pagename'] ) {
1467                         $this->queried_object =& get_page_by_path($qv['pagename']);
1468                         if ( !empty($this->queried_object) )
1469                                 $this->queried_object_id = (int) $this->queried_object->ID;
1470                         else
1471                                 unset($this->queried_object);
1472
1473                         if  ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1474                                 $this->is_page = false;
1475                                 $this->is_home = true;
1476                                 $this->is_posts_page = true;
1477                         }
1478                 }
1479
1480                 if ( $qv['page_id'] ) {
1481                         if  ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1482                                 $this->is_page = false;
1483                                 $this->is_home = true;
1484                                 $this->is_posts_page = true;
1485                         }
1486                 }
1487
1488                 if ( !empty($qv['post_type']) )
1489                         $qv['post_type'] = sanitize_user($qv['post_type'], true);
1490
1491                 if ( !empty($qv['post_status']) )
1492                         $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1493
1494                 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1495                         $this->is_comment_feed = false;
1496
1497                 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1498                 // Done correcting is_* for page_on_front and page_for_posts
1499
1500                 if ('404' == $qv['error'])
1501                         $this->set_404();
1502
1503                 if ( !empty($query) )
1504                         do_action_ref_array('parse_query', array(&$this));
1505         }
1506
1507         /**
1508          * Sets the 404 property and saves whether query is feed.
1509          *
1510          * @since 2.0.0
1511          * @access public
1512          */
1513         function set_404() {
1514                 $is_feed = $this->is_feed;
1515
1516                 $this->init_query_flags();
1517                 $this->is_404 = true;
1518
1519                 $this->is_feed = $is_feed;
1520         }
1521
1522         /**
1523          * Retrieve query variable.
1524          *
1525          * @since 1.5.0
1526          * @access public
1527          *
1528          * @param string $query_var Query variable key.
1529          * @return mixed
1530          */
1531         function get($query_var) {
1532                 if (isset($this->query_vars[$query_var])) {
1533                         return $this->query_vars[$query_var];
1534                 }
1535
1536                 return '';
1537         }
1538
1539         /**
1540          * Set query variable.
1541          *
1542          * @since 1.5.0
1543          * @access public
1544          *
1545          * @param string $query_var Query variable key.
1546          * @param mixed $value Query variable value.
1547          */
1548         function set($query_var, $value) {
1549                 $this->query_vars[$query_var] = $value;
1550         }
1551
1552         /**
1553          * Retrieve the posts based on query variables.
1554          *
1555          * There are a few filters and actions that can be used to modify the post
1556          * database query.
1557          *
1558          * @since 1.5.0
1559          * @access public
1560          * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1561          *
1562          * @return array List of posts.
1563          */
1564         function &get_posts() {
1565                 global $wpdb, $user_ID;
1566
1567                 do_action_ref_array('pre_get_posts', array(&$this));
1568
1569                 // Shorthand.
1570                 $q = &$this->query_vars;
1571
1572                 $q = $this->fill_query_vars($q);
1573
1574                 // First let's clear some variables
1575                 $distinct = '';
1576                 $whichcat = '';
1577                 $whichauthor = '';
1578                 $whichmimetype = '';
1579                 $where = '';
1580                 $limits = '';
1581                 $join = '';
1582                 $search = '';
1583                 $groupby = '';
1584                 $fields = "$wpdb->posts.*";
1585                 $post_status_join = false;
1586                 $page = 1;
1587
1588                 if ( !isset($q['caller_get_posts']) )
1589                         $q['caller_get_posts'] = false;
1590
1591                 if ( !isset($q['suppress_filters']) )
1592                         $q['suppress_filters'] = false;
1593
1594                 if ( !isset($q['post_type']) ) {
1595                         if ( $this->is_search )
1596                                 $q['post_type'] = 'any';
1597                         else
1598                                 $q['post_type'] = 'post';
1599                 }
1600                 $post_type = $q['post_type'];
1601                 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1602                         $q['posts_per_page'] = get_option('posts_per_page');
1603                 if ( isset($q['showposts']) && $q['showposts'] ) {
1604                         $q['showposts'] = (int) $q['showposts'];
1605                         $q['posts_per_page'] = $q['showposts'];
1606                 }
1607                 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1608                         $q['posts_per_page'] = $q['posts_per_archive_page'];
1609                 if ( !isset($q['nopaging']) ) {
1610                         if ($q['posts_per_page'] == -1) {
1611                                 $q['nopaging'] = true;
1612                         } else {
1613                                 $q['nopaging'] = false;
1614                         }
1615                 }
1616                 if ( $this->is_feed ) {
1617                         $q['posts_per_page'] = get_option('posts_per_rss');
1618                         $q['nopaging'] = false;
1619                 }
1620                 $q['posts_per_page'] = (int) $q['posts_per_page'];
1621                 if ( $q['posts_per_page'] < -1 )
1622                         $q['posts_per_page'] = abs($q['posts_per_page']);
1623                 else if ( $q['posts_per_page'] == 0 )
1624                         $q['posts_per_page'] = 1;
1625
1626                 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
1627                         $q['comments_per_page'] = get_option('comments_per_page');
1628
1629                 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
1630                         $this->is_page = true;
1631                         $this->is_home = false;
1632                         $q['page_id'] = get_option('page_on_front');
1633                 }
1634
1635                 if (isset($q['page'])) {
1636                         $q['page'] = trim($q['page'], '/');
1637                         $q['page'] = absint($q['page']);
1638                 }
1639
1640                 // If a month is specified in the querystring, load that month
1641                 if ( $q['m'] ) {
1642                         $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
1643                         $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
1644                         if (strlen($q['m'])>5)
1645                                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
1646                         if (strlen($q['m'])>7)
1647                                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
1648                         if (strlen($q['m'])>9)
1649                                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
1650                         if (strlen($q['m'])>11)
1651                                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
1652                         if (strlen($q['m'])>13)
1653                                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
1654                 }
1655
1656                 if ( '' !== $q['hour'] )
1657                         $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
1658
1659                 if ( '' !== $q['minute'] )
1660                         $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
1661
1662                 if ( '' !== $q['second'] )
1663                         $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
1664
1665                 if ( $q['year'] )
1666                         $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
1667
1668                 if ( $q['monthnum'] )
1669                         $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
1670
1671                 if ( $q['day'] )
1672                         $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
1673
1674                 if ('' != $q['name']) {
1675                         $q['name'] = sanitize_title($q['name']);
1676                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
1677                 } else if ('' != $q['pagename']) {
1678                         if ( isset($this->queried_object_id) )
1679                                 $reqpage = $this->queried_object_id;
1680                         else {
1681                                 $reqpage = get_page_by_path($q['pagename']);
1682                                 if ( !empty($reqpage) )
1683                                         $reqpage = $reqpage->ID;
1684                                 else
1685                                         $reqpage = 0;
1686                         }
1687
1688                         $page_for_posts = get_option('page_for_posts');
1689                         if  ( ('page' != get_option('show_on_front') ) ||  empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
1690                                 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
1691                                 $page_paths = '/' . trim($q['pagename'], '/');
1692                                 $q['pagename'] = sanitize_title(basename($page_paths));
1693                                 $q['name'] = $q['pagename'];
1694                                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
1695                                 $reqpage_obj = get_page($reqpage);
1696                                 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
1697                                         $this->is_attachment = true;
1698                                         $this->is_page = true;
1699                                         $q['attachment_id'] = $reqpage;
1700                                 }
1701                         }
1702                 } elseif ('' != $q['attachment']) {
1703                         $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
1704                         $attach_paths = '/' . trim($q['attachment'], '/');
1705                         $q['attachment'] = sanitize_title(basename($attach_paths));
1706                         $q['name'] = $q['attachment'];
1707                         $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
1708                 }
1709
1710                 if ( $q['w'] )
1711                         $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
1712
1713                 if ( intval($q['comments_popup']) )
1714                         $q['p'] = absint($q['comments_popup']);
1715
1716                 // If an attachment is requested by number, let it supercede any post number.
1717                 if ( $q['attachment_id'] )
1718                         $q['p'] = absint($q['attachment_id']);
1719
1720                 // If a post number is specified, load that post
1721                 if ( $q['p'] ) {
1722                         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
1723                 } elseif ( $q['post__in'] ) {
1724                         $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
1725                         $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
1726                 } elseif ( $q['post__not_in'] ) {
1727                         $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
1728                         $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
1729                 }
1730
1731                 if ( is_numeric($q['post_parent']) )
1732                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
1733
1734                 if ( $q['page_id'] ) {
1735                         if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
1736                                 $q['p'] = $q['page_id'];
1737                                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
1738                         }
1739                 }
1740
1741                 // If a search pattern is specified, load the posts that match
1742                 if ( !empty($q['s']) ) {
1743                         // added slashes screw with quote grouping when done early, so done later
1744                         $q['s'] = stripslashes($q['s']);
1745                         if ( !empty($q['sentence']) ) {
1746                                 $q['search_terms'] = array($q['s']);
1747                         } else {
1748                                 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
1749                                 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
1750                         }
1751                         $n = !empty($q['exact']) ? '' : '%';
1752                         $searchand = '';
1753                         foreach( (array) $q['search_terms'] as $term) {
1754                                 $term = addslashes_gpc($term);
1755                                 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
1756                                 $searchand = ' AND ';
1757                         }
1758                         $term = $wpdb->escape($q['s']);
1759                         if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
1760                                 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
1761
1762                         if ( !empty($search) ) {
1763                                 $search = " AND ({$search}) ";
1764                                 if ( !is_user_logged_in() )
1765                                         $search .= " AND ($wpdb->posts.post_password = '') ";
1766                         }
1767                 }
1768
1769                 // Category stuff
1770
1771                 if ( empty($q['cat']) || ($q['cat'] == '0') ||
1772                                 // Bypass cat checks if fetching specific posts
1773                                 $this->is_singular ) {
1774                         $whichcat = '';
1775                 } else {
1776                         $q['cat'] = ''.urldecode($q['cat']).'';
1777                         $q['cat'] = addslashes_gpc($q['cat']);
1778                         $cat_array = preg_split('/[,\s]+/', $q['cat']);
1779                         $q['cat'] = '';
1780                         $req_cats = array();
1781                         foreach ( (array) $cat_array as $cat ) {
1782                                 $cat = intval($cat);
1783                                 $req_cats[] = $cat;
1784                                 $in = ($cat > 0);
1785                                 $cat = abs($cat);
1786                                 if ( $in ) {
1787                                         $q['category__in'][] = $cat;
1788                                         $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
1789                                 } else {
1790                                         $q['category__not_in'][] = $cat;
1791                                         $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
1792                                 }
1793                         }
1794                         $q['cat'] = implode(',', $req_cats);
1795                 }
1796
1797                 if ( !empty($q['category__in']) ) {
1798                         $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) ";
1799                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1800                         $include_cats = "'" . implode("', '", $q['category__in']) . "'";
1801                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
1802                 }
1803
1804                 if ( !empty($q['category__not_in']) ) {
1805                         if ( $wpdb->has_cap( 'subqueries' ) ) {
1806                                 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
1807                                 $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) )";
1808                         } else {
1809                                 $ids = get_objects_in_term($q['category__not_in'], 'category');
1810                                 if ( !is_wp_error($ids) && is_array($ids) && count($ids) > 0 )
1811                                         $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
1812                         }
1813                 }
1814
1815                 // Category stuff for nice URLs
1816                 if ( '' != $q['category_name'] && !$this->is_singular ) {
1817                         $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
1818                         $reqcat = get_category_by_path($q['category_name']);
1819                         $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
1820                         $cat_paths = '/' . trim($q['category_name'], '/');
1821                         $q['category_name'] = sanitize_title(basename($cat_paths));
1822
1823                         $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
1824                         $q['category_name'] = sanitize_title(basename($cat_paths));
1825                         $cat_paths = explode('/', $cat_paths);
1826                         $cat_path = '';
1827                         foreach ( (array) $cat_paths as $pathdir )
1828                                 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
1829
1830                         //if we don't match the entire hierarchy fallback on just matching the nicename
1831                         if ( empty($reqcat) )
1832                                 $reqcat = get_category_by_path($q['category_name'], false);
1833
1834                         if ( !empty($reqcat) )
1835                                 $reqcat = $reqcat->term_id;
1836                         else
1837                                 $reqcat = 0;
1838
1839                         $q['cat'] = $reqcat;
1840
1841                         $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) ";
1842                         $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1843                         $in_cats = array($q['cat']);
1844                         $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
1845                         $in_cats = "'" . implode("', '", $in_cats) . "'";
1846                         $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
1847                         $groupby = "{$wpdb->posts}.ID";
1848                 }
1849
1850                 // Tags
1851                 if ( '' != $q['tag'] ) {
1852                         if ( strpos($q['tag'], ',') !== false ) {
1853                                 $tags = preg_split('/[,\s]+/', $q['tag']);
1854                                 foreach ( (array) $tags as $tag ) {
1855                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1856                                         $q['tag_slug__in'][] = $tag;
1857                                 }
1858                         } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
1859                                 $tags = preg_split('/[+\s]+/', $q['tag']);
1860                                 foreach ( (array) $tags as $tag ) {
1861                                         $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1862                                         $q['tag_slug__and'][] = $tag;
1863                                 }
1864                         } else {
1865                                 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1866                                 $q['tag_slug__in'][] = $q['tag'];
1867                         }
1868                 }
1869
1870                 if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
1871                         $groupby = "{$wpdb->posts}.ID";
1872                 }
1873
1874                 if ( !empty($q['tag__in']) && empty($q['cat']) ) {
1875                         $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) ";
1876                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1877                         $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1878                         $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1879                         $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
1880                         if ( !empty($reqtag) )
1881                                 $q['tag_id'] = $reqtag['term_id'];
1882                 }
1883
1884                 if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
1885                         $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) ";
1886                         $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1887                         $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1888                         $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1889                         $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
1890                         if ( !empty($reqtag) )
1891                                 $q['tag_id'] = $reqtag->term_id;
1892                 }
1893
1894                 if ( !empty($q['tag__not_in']) ) {
1895                         if ( $wpdb->has_cap( 'subqueries' ) ) {
1896                                 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
1897                                 $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) )";
1898                         } else {
1899                                 $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
1900                                 if ( !is_wp_error($ids) && is_array($ids) && count($ids) > 0 )
1901                                         $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
1902                         }
1903                 }
1904
1905                 // Tag and slug intersections.
1906                 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
1907                 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
1908                 foreach ($intersections as $item => $taxonomy) {
1909                         if ( empty($q[$item]) ) continue;
1910                         if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
1911
1912                         if ( $item != 'category__and' ) {
1913                                 $reqtag = is_term( $q[$item][0], 'post_tag' );
1914                                 if ( !empty($reqtag) )
1915                                         $q['tag_id'] = $reqtag['term_id'];
1916                         }
1917
1918                         if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
1919                                 $taxonomy_field = 'slug';
1920                         else
1921                                 $taxonomy_field = 'term_id';
1922
1923                         $q[$item] = array_unique($q[$item]);
1924                         $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)";
1925                         $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
1926                         if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
1927                                 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
1928                         }
1929                         $post_ids = $wpdb->get_col($tsql);
1930
1931                         if ( count($post_ids) )
1932                                 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1933                         else {
1934                                 $whichcat = " AND 0 = 1";
1935                                 break;
1936                         }
1937                 }
1938
1939                 // Taxonomies
1940                 if ( $this->is_tax ) {
1941                         if ( '' != $q['taxonomy'] ) {
1942                                 $taxonomy = $q['taxonomy'];
1943                                 $tt[$taxonomy] = $q['term'];
1944                                 $terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
1945                         } else {
1946                                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1947                                         if ( $t->query_var && '' != $q[$t->query_var] ) {
1948                                                 $terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var]));
1949                                                 if ( !is_wp_error($terms) )
1950                                                         break;
1951                                         }
1952                                 }
1953                         }
1954                         if ( is_wp_error($terms) || empty($terms) ) {
1955                                 $whichcat = " AND 0 ";
1956                         } else {
1957                                 foreach ( $terms as $term )
1958                                         $term_ids[] = $term->term_id;
1959                                 $post_ids = get_objects_in_term($term_ids, $taxonomy);
1960                                 if ( !is_wp_error($post_ids) && count($post_ids) ) {
1961                                         $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1962                                         $post_type = 'any';
1963                                         $q['post_status'] = 'publish';
1964                                         $post_status_join = true;
1965                                 } else {
1966                                         $whichcat = " AND 0 ";
1967                                 }
1968                         }
1969                 }
1970
1971                 // Author/user stuff
1972
1973                 if ( empty($q['author']) || ($q['author'] == '0') ) {
1974                         $whichauthor='';
1975                 } else {
1976                         $q['author'] = ''.urldecode($q['author']).'';
1977                         $q['author'] = addslashes_gpc($q['author']);
1978                         if (strpos($q['author'], '-') !== false) {
1979                                 $eq = '!=';
1980                                 $andor = 'AND';
1981                                 $q['author'] = explode('-', $q['author']);
1982                                 $q['author'] = '' . absint($q['author'][1]);
1983                         } else {
1984                                 $eq = '=';
1985                                 $andor = 'OR';
1986                         }
1987                         $author_array = preg_split('/[,\s]+/', $q['author']);
1988                         $whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]);
1989                         for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
1990                                 $whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]);
1991                         }
1992                         $whichauthor .= ')';
1993                 }
1994
1995                 // Author stuff for nice URLs
1996
1997                 if ('' != $q['author_name']) {
1998                         if (strpos($q['author_name'], '/') !== false) {
1999                                 $q['author_name'] = explode('/',$q['author_name']);
2000                                 if ($q['author_name'][count($q['author_name'])-1]) {
2001                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
2002                                 } else {
2003                                         $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
2004                                 }
2005                         }
2006                         $q['author_name'] = sanitize_title($q['author_name']);
2007                         $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
2008                         $q['author'] = get_user_by('slug', $q['author_name']);
2009                         if ( $q['author'] )
2010                                 $q['author'] = $q['author']->ID;
2011                         $whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')';
2012                 }
2013
2014                 // MIME-Type stuff for attachment browsing
2015
2016                 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
2017                         $whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
2018
2019                 $where .= $search.$whichcat.$whichauthor.$whichmimetype;
2020
2021                 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2022                         $q['order'] = 'DESC';
2023
2024                 // Order by
2025                 if ( empty($q['orderby']) ) {
2026                         $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
2027                 } elseif ( 'none' == $q['orderby'] ) {
2028                         $q['orderby'] = '';
2029                 } else {
2030                         // Used to filter values
2031                         $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand');
2032                         if ( !empty($q['meta_key']) ) {
2033                                 $allowed_keys[] = $q['meta_key'];
2034                                 $allowed_keys[] = 'meta_value';
2035                         }
2036                         $q['orderby'] = urldecode($q['orderby']);
2037                         $q['orderby'] = addslashes_gpc($q['orderby']);
2038                         $orderby_array = explode(' ',$q['orderby']);
2039                         if ( empty($orderby_array) )
2040                                 $orderby_array[] = $q['orderby'];
2041                         $q['orderby'] = '';
2042                         for ($i = 0; $i < count($orderby_array); $i++) {
2043                                 // Only allow certain values for safety
2044                                 $orderby = $orderby_array[$i];
2045                                 switch ($orderby) {
2046                                         case 'menu_order':
2047                                                 break;
2048                                         case 'ID':
2049                                                 $orderby = "$wpdb->posts.ID";
2050                                                 break;
2051                                         case 'rand':
2052                                                 $orderby = 'RAND()';
2053                                                 break;
2054                                         case $q['meta_key']:
2055                                         case 'meta_value':
2056                                                 $orderby = "$wpdb->postmeta.meta_value";
2057                                                 break;
2058                                         default:
2059                                                 $orderby = "$wpdb->posts.post_" . $orderby;
2060                                 }
2061                                 if ( in_array($orderby_array[$i], $allowed_keys) )
2062                                         $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
2063                         }
2064                         // append ASC or DESC at the end
2065                         if ( !empty($q['orderby']))
2066                                 $q['orderby'] .= " {$q['order']}";
2067
2068                         if ( empty($q['orderby']) )
2069                                 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
2070                 }
2071
2072                 if ( 'any' == $post_type ) {
2073                         $where .= " AND $wpdb->posts.post_type != 'revision'";
2074                 } elseif ( $this->is_attachment ) {
2075                         $where .= " AND $wpdb->posts.post_type = 'attachment'";
2076                 } elseif ($this->is_page) {
2077                         $where .= " AND $wpdb->posts.post_type = 'page'";
2078                 } elseif ($this->is_single) {
2079                         $where .= " AND $wpdb->posts.post_type = 'post'";
2080                 } else {
2081                         $where .= " AND $wpdb->posts.post_type = '$post_type'";
2082                 }
2083
2084                 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
2085                         $statuswheres = array();
2086                         $q_status = explode(',', $q['post_status']);
2087                         $r_status = array();
2088                         $p_status = array();
2089                         if ( in_array( 'draft'  , $q_status ) )
2090                                 $r_status[] = "$wpdb->posts.post_status = 'draft'";
2091                         if ( in_array( 'pending', $q_status ) )
2092                                 $r_status[] = "$wpdb->posts.post_status = 'pending'";
2093                         if ( in_array( 'future' , $q_status ) )
2094                                 $r_status[] = "$wpdb->posts.post_status = 'future'";
2095                         if ( in_array( 'inherit' , $q_status ) )
2096                                 $r_status[] = "$wpdb->posts.post_status = 'inherit'";
2097                         if ( in_array( 'private', $q_status ) )
2098                                 $p_status[] = "$wpdb->posts.post_status = 'private'";
2099                         if ( in_array( 'publish', $q_status ) )
2100                                 $r_status[] = "$wpdb->posts.post_status = 'publish'";
2101
2102                         if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2103                                 $r_status = array_merge($r_status, $p_status);
2104                                 unset($p_status);
2105                         }
2106
2107                         if ( !empty($r_status) ) {
2108                                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") )
2109                                         $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
2110                                 else
2111                                         $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2112                         }
2113                         if ( !empty($p_status) ) {
2114                                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") )
2115                                         $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
2116                                 else
2117                                         $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2118                         }
2119                         if ( $post_status_join ) {
2120                                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2121                                 foreach ( $statuswheres as $index => $statuswhere )
2122                                         $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2123                         }
2124                         foreach ( $statuswheres as $statuswhere )
2125                                 $where .= " AND $statuswhere";
2126                 } elseif ( !$this->is_singular ) {
2127                         $where .= " AND ($wpdb->posts.post_status = 'publish'";
2128
2129                         if ( is_admin() )
2130                                 $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
2131
2132                         if ( is_user_logged_in() ) {
2133                                 $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
2134                         }
2135
2136                         $where .= ')';
2137                 }
2138
2139                 // postmeta queries
2140                 if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
2141                         $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
2142                 if ( ! empty($q['meta_key']) )
2143                         $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
2144                 if ( ! empty($q['meta_value']) ) {
2145                         if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
2146                                 $q['meta_compare'] = '=';
2147
2148                         $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
2149                 }
2150
2151                 // Apply filters on where and join prior to paging so that any
2152                 // manipulations to them are reflected in the paging by day queries.
2153                 if ( !$q['suppress_filters'] ) {
2154                         $where = apply_filters('posts_where', $where);
2155                         $join = apply_filters('posts_join', $join);
2156                 }
2157
2158                 // Paging
2159                 if ( empty($q['nopaging']) && !$this->is_singular ) {
2160                         $page = absint($q['paged']);
2161                         if (empty($page)) {
2162                                 $page = 1;
2163                         }
2164
2165                         if ( empty($q['offset']) ) {
2166                                 $pgstrt = '';
2167                                 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2168                                 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
2169                         } else { // we're ignoring $page and using 'offset'
2170                                 $q['offset'] = absint($q['offset']);
2171                                 $pgstrt = $q['offset'] . ', ';
2172                                 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2173                         }
2174                 }
2175
2176                 // Comments feeds
2177                 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2178                         if ( $this->is_archive || $this->is_search ) {
2179                                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2180                                 $cwhere = "WHERE comment_approved = '1' $where";
2181                                 $cgroupby = "$wpdb->comments.comment_id";
2182                         } else { // Other non singular e.g. front
2183                                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2184                                 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2185                                 $cgroupby = '';
2186                         }
2187
2188                         if ( !$q['suppress_filters'] ) {
2189                                 $cjoin = apply_filters('comment_feed_join', $cjoin);
2190                                 $cwhere = apply_filters('comment_feed_where', $cwhere);
2191                                 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
2192                                 $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
2193                                 $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
2194                         }
2195                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2196                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2197
2198                         $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2199                         $this->comment_count = count($this->comments);
2200
2201                         $post_ids = array();
2202
2203                         foreach ($this->comments as $comment)
2204                                 $post_ids[] = (int) $comment->comment_post_ID;
2205
2206                         $post_ids = join(',', $post_ids);
2207                         $join = '';
2208                         if ( $post_ids )
2209                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2210                         else
2211                                 $where = "AND 0";
2212                 }
2213
2214                 $orderby = $q['orderby'];
2215
2216                 // Apply post-paging filters on where and join.  Only plugins that
2217                 // manipulate paging queries should use these hooks.
2218                 if ( !$q['suppress_filters'] ) {
2219                         $where = apply_filters('posts_where_paged', $where);
2220                         $groupby = apply_filters('posts_groupby', $groupby);
2221                         $join = apply_filters('posts_join_paged', $join);
2222                         $orderby = apply_filters('posts_orderby', $orderby);
2223                         $distinct = apply_filters('posts_distinct', $distinct);
2224                         $limits = apply_filters( 'post_limits', $limits );
2225
2226                         $fields = apply_filters('posts_fields', $fields);
2227                 }
2228
2229                 // Announce current selection parameters.  For use by caching plugins.
2230                 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2231
2232                 // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
2233                 if ( !$q['suppress_filters'] ) {
2234                         $where = apply_filters('posts_where_request', $where);
2235                         $groupby = apply_filters('posts_groupby_request', $groupby);
2236                         $join = apply_filters('posts_join_request', $join);
2237                         $orderby = apply_filters('posts_orderby_request', $orderby);
2238                         $distinct = apply_filters('posts_distinct_request', $distinct);
2239                         $fields = apply_filters('posts_fields_request', $fields);
2240                         $limits = apply_filters( 'post_limits_request', $limits );
2241                 }
2242
2243                 if ( ! empty($groupby) )
2244                         $groupby = 'GROUP BY ' . $groupby;
2245                 if ( !empty( $orderby ) )
2246                         $orderby = 'ORDER BY ' . $orderby;
2247                 $found_rows = '';
2248                 if ( !empty($limits) )
2249                         $found_rows = 'SQL_CALC_FOUND_ROWS';
2250
2251                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2252                 if ( !$q['suppress_filters'] )
2253                         $this->request = apply_filters('posts_request', $this->request);
2254
2255                 $this->posts = $wpdb->get_results($this->request);
2256                 // Raw results filter.  Prior to status checks.
2257                 if ( !$q['suppress_filters'] )
2258                         $this->posts = apply_filters('posts_results', $this->posts);
2259
2260                 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2261                         $cjoin = apply_filters('comment_feed_join', '');
2262                         $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
2263                         $cgroupby = apply_filters('comment_feed_groupby', '');
2264                         $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2265                         $corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
2266                         $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2267                         $climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
2268                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2269                         $this->comments = $wpdb->get_results($comments_request);
2270                         $this->comment_count = count($this->comments);
2271                 }
2272
2273                 if ( !empty($limits) ) {
2274                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
2275                         $this->found_posts = $wpdb->get_var( $found_posts_query );
2276                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
2277                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
2278                 }
2279
2280                 // Check post status to determine if post should be displayed.
2281                 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2282                         $status = get_post_status($this->posts[0]);
2283                         //$type = get_post_type($this->posts[0]);
2284                         if ( ('publish' != $status) ) {
2285                                 if ( ! is_user_logged_in() ) {
2286                                         // User must be logged in to view unpublished posts.
2287                                         $this->posts = array();
2288                                 } else {
2289                                         if  (in_array($status, array('draft', 'pending')) ) {
2290                                                 // User must have edit permissions on the draft to preview.
2291                                                 if (! current_user_can('edit_post', $this->posts[0]->ID)) {
2292                                                         $this->posts = array();
2293                                                 } else {
2294                                                         $this->is_preview = true;
2295                                                         $this->posts[0]->post_date = current_time('mysql');
2296                                                 }
2297                                         }  else if ('future' == $status) {
2298                                                 $this->is_preview = true;
2299                                                 if (!current_user_can('edit_post', $this->posts[0]->ID)) {
2300                                                         $this->posts = array ( );
2301                                                 }
2302                                         } else {
2303                                                 if (! current_user_can('read_post', $this->posts[0]->ID))
2304                                                         $this->posts = array();
2305                                         }
2306                                 }
2307                         }
2308
2309                         if ( $this->is_preview && current_user_can( "edit_{$post_type}", $this->posts[0]->ID ) )
2310                                 $this->posts[0] = apply_filters('the_preview', $this->posts[0]);
2311                 }
2312
2313                 // Put sticky posts at the top of the posts array
2314                 $sticky_posts = get_option('sticky_posts');
2315                 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
2316                         $num_posts = count($this->posts);
2317                         $sticky_offset = 0;
2318                         // Loop over posts and relocate stickies to the front.
2319                         for ( $i = 0; $i < $num_posts; $i++ ) {
2320                                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2321                                         $sticky_post = $this->posts[$i];
2322                                         // Remove sticky from current position
2323                                         array_splice($this->posts, $i, 1);
2324                                         // Move to front, after other stickies
2325                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2326                                         // Increment the sticky offset.  The next sticky will be placed at this offset.
2327                                         $sticky_offset++;
2328                                         // Remove post from sticky posts array
2329                                         $offset = array_search($sticky_post->ID, $sticky_posts);
2330                                         array_splice($sticky_posts, $offset, 1);
2331                                 }
2332                         }
2333
2334                         // Fetch sticky posts that weren't in the query results
2335                         if ( !empty($sticky_posts) ) {
2336                                 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
2337                                 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in)" );
2338                                 /** @todo Make sure post is published or viewable by the current user */
2339                                 foreach ( $stickies as $sticky_post ) {
2340                                         if ( 'publish' != $sticky_post->post_status )
2341                                                 continue;
2342                                                 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2343                                                 $sticky_offset++;
2344                                 }
2345                         }
2346                 }
2347
2348                 if ( !$q['suppress_filters'] )
2349                         $this->posts = apply_filters('the_posts', $this->posts);
2350
2351                 update_post_caches($this->posts);
2352
2353                 $this->post_count = count($this->posts);
2354                 if ($this->post_count > 0) {
2355                         $this->post = $this->posts[0];
2356                 }
2357
2358                 return $this->posts;
2359         }
2360
2361         /**
2362          * Setup the next post and iterate current post index.
2363          *
2364          * @since 1.5.0
2365          * @access public
2366          *
2367          * @return object Next post.
2368          */
2369         function next_post() {
2370
2371                 $this->current_post++;
2372
2373                 $this->post = $this->posts[$this->current_post];
2374                 return $this->post;
2375         }
2376
2377         /**
2378          * Sets up the current post.
2379          *
2380          * Retrieves the next post, sets up the post, sets the 'in the loop'
2381          * property to true.
2382          *
2383          * @since 1.5.0
2384          * @access public
2385          * @uses $post
2386          * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2387          */
2388         function the_post() {
2389                 global $post;
2390                 $this->in_the_loop = true;
2391
2392                 if ( $this->current_post == -1 ) // loop has just started
2393                         do_action_ref_array('loop_start', array(&$this));
2394
2395                 $post = $this->next_post();
2396                 setup_postdata($post);
2397         }
2398
2399         /**
2400          * Whether there are more posts available in the loop.
2401          *
2402          * Calls action 'loop_end', when the loop is complete.
2403          *
2404          * @since 1.5.0
2405          * @access public
2406          * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2407          *
2408          * @return bool True if posts are available, false if end of loop.
2409          */
2410         function have_posts() {
2411                 if ($this->current_post + 1 < $this->post_count) {
2412                         return true;
2413                 } elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
2414                         do_action_ref_array('loop_end', array(&$this));
2415                         // Do some cleaning up after the loop
2416                         $this->rewind_posts();
2417                 }
2418
2419                 $this->in_the_loop = false;
2420                 return false;
2421         }
2422
2423         /**
2424          * Rewind the posts and reset post index.
2425          *
2426          * @since 1.5.0
2427          * @access public
2428          */
2429         function rewind_posts() {
2430                 $this->current_post = -1;
2431                 if ($this->post_count > 0) {
2432                         $this->post = $this->posts[0];
2433                 }
2434         }
2435
2436         /**
2437          * Iterate current comment index and return comment object.
2438          *
2439          * @since 2.2.0
2440          * @access public
2441          *
2442          * @return object Comment object.
2443          */
2444         function next_comment() {
2445                 $this->current_comment++;
2446
2447                 $this->comment = $this->comments[$this->current_comment];
2448                 return $this->comment;
2449         }
2450
2451         /**
2452          * Sets up the current comment.
2453          *
2454          * @since 2.2.0
2455          * @access public
2456          * @global object $comment Current comment.
2457          * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2458          */
2459         function the_comment() {
2460                 global $comment;
2461
2462                 $comment = $this->next_comment();
2463
2464                 if ($this->current_comment == 0) {
2465                         do_action('comment_loop_start');
2466                 }
2467         }
2468
2469         /**
2470          * Whether there are more comments available.
2471          *
2472          * Automatically rewinds comments when finished.
2473          *
2474          * @since 2.2.0
2475          * @access public
2476          *
2477          * @return bool True, if more comments. False, if no more posts.
2478          */
2479         function have_comments() {
2480                 if ($this->current_comment + 1 < $this->comment_count) {
2481                         return true;
2482                 } elseif ($this->current_comment + 1 == $this->comment_count) {
2483                         $this->rewind_comments();
2484                 }
2485
2486                 return false;
2487         }
2488
2489         /**
2490          * Rewind the comments, resets the comment index and comment to first.
2491          *
2492          * @since 2.2.0
2493          * @access public
2494          */
2495         function rewind_comments() {
2496                 $this->current_comment = -1;
2497                 if ($this->comment_count > 0) {
2498                         $this->comment = $this->comments[0];
2499                 }
2500         }
2501
2502         /**
2503          * Sets up the WordPress query by parsing query string.
2504          *
2505          * @since 1.5.0
2506          * @access public
2507          *
2508          * @param string $query URL query string.
2509          * @return array List of posts.
2510          */
2511         function &query($query) {
2512                 $this->parse_query($query);
2513                 return $this->get_posts();
2514         }
2515
2516         /**
2517          * Retrieve queried object.
2518          *
2519          * If queried object is not set, then the queried object will be set from
2520          * the category, tag, taxonomy, posts page, single post, page, or author
2521          * query variable. After it is set up, it will be returned.
2522          *
2523          * @since 1.5.0
2524          * @access public
2525          *
2526          * @return object
2527          */
2528         function get_queried_object() {
2529                 if (isset($this->queried_object)) {
2530                         return $this->queried_object;
2531                 }
2532
2533                 $this->queried_object = NULL;
2534                 $this->queried_object_id = 0;
2535
2536                 if ($this->is_category) {
2537                         $cat = $this->get('cat');
2538                         $category = &get_category($cat);
2539                         if ( is_wp_error( $category ) )
2540                                 return NULL;
2541                         $this->queried_object = &$category;
2542                         $this->queried_object_id = (int) $cat;
2543                 } else if ($this->is_tag) {
2544                         $tag_id = $this->get('tag_id');
2545                         $tag = &get_term($tag_id, 'post_tag');
2546                         if ( is_wp_error( $tag ) )
2547                                 return NULL;
2548                         $this->queried_object = &$tag;
2549                         $this->queried_object_id = (int) $tag_id;
2550                 } else if ($this->is_tax) {
2551                         $tax = $this->get('taxonomy');
2552                         $slug = $this->get('term');
2553                         $term = &get_terms($tax, array('slug'=>$slug));
2554                         if ( is_wp_error($term) || empty($term) )
2555                                 return NULL;
2556                         $term = $term[0];
2557                         $this->queried_object = $term;
2558                         $this->queried_object_id = $term->term_id;
2559                 } else if ($this->is_posts_page) {
2560                         $this->queried_object = & get_page(get_option('page_for_posts'));
2561                         $this->queried_object_id = (int) $this->queried_object->ID;
2562                 } else if ($this->is_single) {
2563                         $this->queried_object = $this->post;
2564                         $this->queried_object_id = (int) $this->post->ID;
2565                 } else if ($this->is_page) {
2566                         $this->queried_object = $this->post;
2567                         $this->queried_object_id = (int) $this->post->ID;
2568                 } else if ($this->is_author) {
2569                         $author_id = (int) $this->get('author');
2570                         $author = get_userdata($author_id);
2571                         $this->queried_object = $author;
2572                         $this->queried_object_id = $author_id;
2573                 }
2574
2575                 return $this->queried_object;
2576         }
2577
2578         /**
2579          * Retrieve ID of the current queried object.
2580          *
2581          * @since 1.5.0
2582          * @access public
2583          *
2584          * @return int
2585          */
2586         function get_queried_object_id() {
2587                 $this->get_queried_object();
2588
2589                 if (isset($this->queried_object_id)) {
2590                         return $this->queried_object_id;
2591                 }
2592
2593                 return 0;
2594         }
2595
2596         /**
2597          * PHP4 type constructor.
2598          *
2599          * Sets up the WordPress query, if parameter is not empty.
2600          *
2601          * @since 1.5.0
2602          * @access public
2603          *
2604          * @param string $query URL query string.
2605          * @return WP_Query
2606          */
2607         function WP_Query ($query = '') {
2608                 if (! empty($query)) {
2609                         $this->query($query);
2610                 }
2611         }
2612 }
2613
2614 /**
2615  * Redirect old slugs to the correct permalink.
2616  *
2617  * Attempts to find the current slug from the past slugs.
2618  *
2619  * @since 2.1.0
2620  * @uses $wp_query
2621  * @uses $wpdb
2622  *
2623  * @return null If no link is found, null is returned.
2624  */
2625 function wp_old_slug_redirect () {
2626         global $wp_query;
2627         if ( is_404() && '' != $wp_query->query_vars['name'] ) :
2628                 global $wpdb;
2629
2630                 $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'] . "'";
2631
2632                 // if year, monthnum, or day have been specified, make our query more precise
2633                 // just in case there are multiple identical _wp_old_slug values
2634                 if ( '' != $wp_query->query_vars['year'] )
2635                         $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
2636                 if ( '' != $wp_query->query_vars['monthnum'] )
2637                         $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
2638                 if ( '' != $wp_query->query_vars['day'] )
2639                         $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
2640
2641                 $id = (int) $wpdb->get_var($query);
2642
2643                 if ( !$id )
2644                         return;
2645
2646                 $link = get_permalink($id);
2647
2648                 if ( !$link )
2649                         return;
2650
2651                 wp_redirect($link, '301'); // Permanent redirect
2652                 exit;
2653         endif;
2654 }
2655
2656 /**
2657  * Setup global post data.
2658  *
2659  * @since 1.5.0
2660  *
2661  * @param object $post Post data.
2662  * @uses do_action_ref_array() Calls 'the_post'
2663  * @return bool True when finished.
2664  */
2665 function setup_postdata($post) {
2666         global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
2667
2668         $id = (int) $post->ID;
2669
2670         $authordata = get_userdata($post->post_author);
2671
2672         $day = mysql2date('d.m.y', $post->post_date, false);
2673         $currentmonth = mysql2date('m', $post->post_date, false);
2674         $numpages = 1;
2675         $page = get_query_var('page');
2676         if ( !$page )
2677                 $page = 1;
2678         if ( is_single() || is_page() || is_feed() )
2679                 $more = 1;
2680         $content = $post->post_content;
2681         if ( strpos( $content, '<!--nextpage-->' ) ) {
2682                 if ( $page > 1 )
2683                         $more = 1;
2684                 $multipage = 1;
2685                 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
2686                 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
2687                 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
2688                 $pages = explode('<!--nextpage-->', $content);
2689                 $numpages = count($pages);
2690         } else {
2691                 $pages[0] = $post->post_content;
2692                 $multipage = 0;
2693         }
2694
2695         do_action_ref_array('the_post', array(&$post));
2696         
2697         return true;
2698 }
2699
2700 ?>