home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / query.php < prev    next >
Encoding:
PHP Script  |  2008-08-06  |  48.7 KB  |  1,726 lines

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