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