[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-includes/ -> query.php (source)

   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 &query_posts($query) {
  14      global $wp_query;
  15      return $wp_query->query($query);
  16  }
  17  
  18  /*
  19   * Query type checks.
  20   */
  21  
  22  function is_admin () {
  23      global $wp_query;
  24  
  25      return ( $wp_query->is_admin || strstr($_SERVER['REQUEST_URI'], 'wp-admin/') );
  26  }
  27  
  28  function is_archive () {
  29      global $wp_query;
  30  
  31      return $wp_query->is_archive;
  32  }
  33  
  34  function is_attachment () {
  35      global $wp_query;
  36  
  37      return $wp_query->is_attachment;
  38  }
  39  
  40  function is_author ($author = '') {
  41      global $wp_query;
  42  
  43      if ( !$wp_query->is_author )
  44          return false;
  45  
  46      if ( empty($author) )
  47          return true;
  48  
  49      $author_obj = $wp_query->get_queried_object();
  50  
  51      if ( $author == $author_obj->ID )
  52          return true;
  53      elseif ( $author == $author_obj->nickname )
  54          return true;
  55      elseif ( $author == $author_obj->user_nicename )
  56          return true;
  57  
  58      return false;
  59  }
  60  
  61  function is_category ($category = '') {
  62      global $wp_query;
  63  
  64      if ( !$wp_query->is_category )
  65          return false;
  66  
  67      if ( empty($category) )
  68          return true;
  69  
  70      $cat_obj = $wp_query->get_queried_object();
  71  
  72      if ( $category == $cat_obj->cat_ID )
  73          return true;
  74      else if ( $category == $cat_obj->cat_name )
  75          return true;
  76      elseif ( $category == $cat_obj->category_nicename )
  77          return true;
  78  
  79      return false;
  80  }
  81  
  82  function is_comments_popup () {
  83      global $wp_query;
  84  
  85      return $wp_query->is_comments_popup;
  86  }
  87  
  88  function is_date () {
  89      global $wp_query;
  90  
  91      return $wp_query->is_date;
  92  }
  93  
  94  function is_day () {
  95      global $wp_query;
  96  
  97      return $wp_query->is_day;
  98  }
  99  
 100  function is_feed () {
 101      global $wp_query;
 102  
 103      return $wp_query->is_feed;
 104  }
 105  
 106  function is_home () {
 107      global $wp_query;
 108  
 109      return $wp_query->is_home;
 110  }
 111  
 112  function is_month () {
 113      global $wp_query;
 114  
 115      return $wp_query->is_month;
 116  }
 117  
 118  function is_page ($page = '') {
 119      global $wp_query;
 120  
 121      if ( !$wp_query->is_page )
 122          return false;
 123  
 124      if ( empty($page) )
 125          return true;
 126  
 127      $page_obj = $wp_query->get_queried_object();
 128  
 129      if ( $page == $page_obj->ID )
 130          return true;
 131      elseif ( $page == $page_obj->post_title )
 132          return true;
 133      else if ( $page == $page_obj->post_name )
 134          return true;
 135  
 136      return false;
 137  }
 138  
 139  function is_paged () {
 140      global $wp_query;
 141  
 142      return $wp_query->is_paged;
 143  }
 144  
 145  function is_plugin_page() {
 146      global $plugin_page;
 147  
 148      if ( isset($plugin_page) )
 149          return true;
 150  
 151      return false;
 152  }
 153  
 154  function is_preview() {
 155      global $wp_query;
 156  
 157      return $wp_query->is_preview;
 158  }
 159  
 160  function is_robots() {
 161      global $wp_query;
 162  
 163      return $wp_query->is_robots;
 164  }
 165  
 166  function is_search () {
 167      global $wp_query;
 168  
 169      return $wp_query->is_search;
 170  }
 171  
 172  function is_single ($post = '') {
 173      global $wp_query;
 174  
 175      if ( !$wp_query->is_single )
 176          return false;
 177  
 178      if ( empty( $post) )
 179          return true;
 180  
 181      $post_obj = $wp_query->get_queried_object();
 182  
 183      if ( $post == $post_obj->ID )
 184          return true;
 185      elseif ( $post == $post_obj->post_title )
 186          return true;
 187      elseif ( $post == $post_obj->post_name )
 188          return true;
 189  
 190      return false;
 191  }
 192  
 193  function is_time () {
 194      global $wp_query;
 195  
 196      return $wp_query->is_time;
 197  }
 198  
 199  function is_trackback () {
 200      global $wp_query;
 201  
 202      return $wp_query->is_trackback;
 203  }
 204  
 205  function is_year () {
 206      global $wp_query;
 207  
 208      return $wp_query->is_year;
 209  }
 210  
 211  function is_404 () {
 212      global $wp_query;
 213  
 214      return $wp_query->is_404;
 215  }
 216  
 217  /*
 218   * The Loop.  Post loop control.
 219   */
 220   
 221  function have_posts() {
 222      global $wp_query;
 223  
 224      return $wp_query->have_posts();
 225  }
 226  
 227  function in_the_loop() {
 228      global $wp_query;
 229  
 230      return $wp_query->in_the_loop;
 231  }
 232  
 233  function rewind_posts() {
 234      global $wp_query;
 235  
 236      return $wp_query->rewind_posts();
 237  }
 238  
 239  function the_post() {
 240      global $wp_query;
 241  
 242      $wp_query->the_post();
 243  }
 244  
 245  /*
 246   * WP_Query
 247   */
 248  
 249  class WP_Query {
 250      var $query;
 251      var $query_vars;
 252      var $queried_object;
 253      var $queried_object_id;
 254      var $request;
 255  
 256      var $posts;
 257      var $post_count = 0;
 258      var $current_post = -1;
 259      var $in_the_loop = false;
 260      var $post;
 261  
 262      var $is_single = false;
 263      var $is_preview = false;
 264      var $is_page = false;
 265      var $is_archive = false;
 266      var $is_date = false;
 267      var $is_year = false;
 268      var $is_month = false;
 269      var $is_day = false;
 270      var $is_time = false;
 271      var $is_author = false;
 272      var $is_category = false;
 273      var $is_search = false;
 274      var $is_feed = false;
 275      var $is_trackback = false;
 276      var $is_home = false;
 277      var $is_404 = false;
 278      var $is_comments_popup = false;
 279      var $is_admin = false;
 280      var $is_attachment = false;
 281      var $is_robots = false;
 282  
 283  	function init_query_flags() {
 284          $this->is_single = false;
 285          $this->is_page = false;
 286          $this->is_archive = false;
 287          $this->is_date = false;
 288          $this->is_year = false;
 289          $this->is_month = false;
 290          $this->is_day = false;
 291          $this->is_time = false;
 292          $this->is_author = false;
 293          $this->is_category = false;
 294          $this->is_search = false;
 295          $this->is_feed = false;
 296          $this->is_trackback = false;
 297          $this->is_home = false;
 298          $this->is_404 = false;
 299          $this->is_paged = false;
 300          $this->is_admin = false;
 301          $this->is_attachment = false;
 302          $this->is_robots = false;
 303      }
 304  
 305  	function init () {
 306          unset($this->posts);
 307          unset($this->query);
 308          unset($this->query_vars);
 309          unset($this->queried_object);
 310          unset($this->queried_object_id);
 311          $this->post_count = 0;
 312          $this->current_post = -1;
 313          $this->in_the_loop = false;
 314  
 315          $this->init_query_flags();
 316      }
 317  
 318      // Reparse the query vars.
 319  	function parse_query_vars() {
 320          $this->parse_query('');
 321      }
 322  
 323      // Parse a query string and set query type booleans.
 324  	function parse_query ($query) {
 325          if ( !empty($query) || !isset($this->query) ) {
 326              $this->init();
 327              parse_str($query, $qv);
 328              $this->query = $query;
 329              $this->query_vars = $qv;
 330          }
 331  
 332          if ( ! empty($qv['robots']) ) {
 333              $this->is_robots = true;
 334              return;
 335          }
 336  
 337          if ('404' == $qv['error']) {
 338              $this->is_404 = true;
 339              if ( !empty($query) ) {
 340                  do_action('parse_query', array(&$this));
 341              }
 342              return;
 343          }
 344  
 345          $qv['m'] =  (int) $qv['m'];
 346          $qv['p'] =  (int) $qv['p'];
 347  
 348          // Compat.  Map subpost to attachment.
 349          if ( '' != $qv['subpost'] )
 350              $qv['attachment'] = $qv['subpost'];
 351          if ( '' != $qv['subpost_id'] )
 352              $qv['attachment_id'] = $qv['subpost_id'];
 353  
 354          if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) {
 355              $this->is_single = true;
 356              $this->is_attachment = true;
 357          } elseif ('' != $qv['name']) {
 358              $this->is_single = true;
 359          } elseif ( $qv['p'] ) {
 360              $this->is_single = true;
 361          } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) {
 362              // If year, month, day, hour, minute, and second are set, a single 
 363              // post is being queried.        
 364              $this->is_single = true;
 365          } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) {
 366              $this->is_page = true;
 367              $this->is_single = false;
 368          } elseif (!empty($qv['s'])) {
 369              $this->is_search = true;
 370          } else {
 371          // Look for archive queries.  Dates, categories, authors.
 372  
 373              if ( (int) $qv['second']) {
 374                  $this->is_time = true;
 375                  $this->is_date = true;
 376              }
 377  
 378              if ( (int) $qv['minute']) {
 379                  $this->is_time = true;
 380                  $this->is_date = true;
 381              }
 382  
 383              if ( (int) $qv['hour']) {
 384                  $this->is_time = true;
 385                  $this->is_date = true;
 386              }
 387  
 388              if ( (int) $qv['day']) {
 389                  if (! $this->is_date) {
 390                      $this->is_day = true;
 391                      $this->is_date = true;
 392                  }
 393              }
 394  
 395              if ( (int)  $qv['monthnum']) {
 396                  if (! $this->is_date) {
 397                      $this->is_month = true;
 398                      $this->is_date = true;
 399                  }
 400              }
 401  
 402              if ( (int)  $qv['year']) {
 403                  if (! $this->is_date) {
 404                      $this->is_year = true;
 405                      $this->is_date = true;
 406                  }
 407              }
 408  
 409              if ( (int)  $qv['m']) {
 410                  $this->is_date = true;
 411                  if (strlen($qv['m']) > 9) {
 412                      $this->is_time = true;
 413                  } else if (strlen($qv['m']) > 7) {
 414                      $this->is_day = true;
 415                  } else if (strlen($qv['m']) > 5) {
 416                      $this->is_month = true;
 417                  } else {
 418                      $this->is_year = true;
 419                  }
 420              }
 421  
 422              if ('' != $qv['w']) {
 423                  $this->is_date = true;
 424              }
 425  
 426              if (empty($qv['cat']) || ($qv['cat'] == '0')) {
 427                  $this->is_category = false;
 428              } else {
 429                  if (stristr($qv['cat'],'-')) {
 430                      $this->is_category = false;
 431                  } else {
 432                      $this->is_category = true;
 433                  }
 434              }
 435  
 436              if ('' != $qv['category_name']) {
 437                  $this->is_category = true;
 438              }
 439              
 440              if ((empty($qv['author'])) || ($qv['author'] == '0')) {
 441                  $this->is_author = false;
 442              } else {
 443                  $this->is_author = true;
 444              }
 445  
 446              if ('' != $qv['author_name']) {
 447                  $this->is_author = true;
 448              }
 449  
 450              if ( ($this->is_date || $this->is_author || $this->is_category)) {
 451                  $this->is_archive = true;
 452              }
 453          }
 454  
 455          if ('' != $qv['feed']) {
 456              $this->is_feed = true;
 457          }
 458  
 459          if ('' != $qv['tb']) {
 460              $this->is_trackback = true;
 461          }
 462  
 463          if ('' != $qv['paged']) {
 464              $this->is_paged = true;
 465          }
 466  
 467          if ('' != $qv['comments_popup']) {
 468              $this->is_comments_popup = true;
 469          }
 470  
 471          //if we're previewing inside the write screen
 472          if ('' != $qv['preview']) {
 473              $this->is_preview = true;
 474          }
 475  
 476          if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
 477              $this->is_admin = true;
 478          }
 479  
 480          if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) {
 481              $this->is_home = true;
 482          }
 483  
 484          if ( !empty($query) ) {
 485              do_action('parse_query', array(&$this));
 486          }
 487      }
 488  
 489  	function set_404() {
 490          $this->init_query_flags();
 491          $this->is_404 = true;
 492      }
 493  
 494  	function get($query_var) {
 495          if (isset($this->query_vars[$query_var])) {
 496              return $this->query_vars[$query_var];
 497          }
 498  
 499          return '';
 500      }
 501  
 502  	function set($query_var, $value) {
 503          $this->query_vars[$query_var] = $value;
 504      }
 505  
 506      function &get_posts() {
 507          global $wpdb, $pagenow, $user_ID;
 508  
 509          do_action('pre_get_posts', array(&$this));
 510  
 511          // Shorthand.
 512          $q = &$this->query_vars;
 513  
 514          // First let's clear some variables
 515          $distinct = '';
 516          $whichcat = '';
 517          $whichauthor = '';
 518          $whichpage = '';
 519          $result = '';
 520          $where = '';
 521          $limits = '';
 522          $join = '';
 523  
 524          if ( !isset($q['post_type']) )
 525              $q['post_type'] = 'post';
 526          $post_type = $q['post_type'];
 527          if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
 528              $q['posts_per_page'] = get_settings('posts_per_page');
 529          if ( !isset($q['what_to_show']) )
 530              $q['what_to_show'] = get_settings('what_to_show');
 531          if ( isset($q['showposts']) && $q['showposts'] ) {
 532              $q['showposts'] = (int) $q['showposts'];
 533              $q['posts_per_page'] = $q['showposts'];
 534          }
 535          if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
 536              $q['posts_per_page'] = $q['posts_per_archive_page'];
 537          if ( !isset($q['nopaging']) ) {
 538              if ($q['posts_per_page'] == -1) {
 539                  $q['nopaging'] = true;
 540              } else {
 541                  $q['nopaging'] = false;
 542              }
 543          }
 544          if ( $this->is_feed ) {
 545              $q['posts_per_page'] = get_settings('posts_per_rss');
 546              $q['what_to_show'] = 'posts';
 547          }
 548  
 549          if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
 550              $this->is_page = true;
 551              $this->is_home = false;
 552              $q['page_id'] = get_option('page_on_front');
 553          }
 554  
 555          if (isset($q['page'])) {
 556              $q['page'] = trim($q['page'], '/');
 557              $q['page'] = (int) $q['page'];
 558              $q['page'] = abs($q['page']);
 559          }
 560  
 561          $add_hours = intval(get_settings('gmt_offset'));
 562          $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
 563          $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
 564  
 565          // If a month is specified in the querystring, load that month
 566          if ( (int) $q['m'] ) {
 567              $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
 568              $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
 569              if (strlen($q['m'])>5)
 570                  $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
 571              if (strlen($q['m'])>7)
 572                  $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
 573              if (strlen($q['m'])>9)
 574                  $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
 575              if (strlen($q['m'])>11)
 576                  $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
 577              if (strlen($q['m'])>13)
 578                  $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
 579          }
 580  
 581          if ( (int) $q['hour'] ) {
 582              $q['hour'] = '' . intval($q['hour']);
 583              $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
 584          }
 585  
 586          if ( (int) $q['minute'] ) {
 587              $q['minute'] = '' . intval($q['minute']);
 588              $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
 589          }
 590  
 591          if ( (int) $q['second'] ) {
 592              $q['second'] = '' . intval($q['second']);
 593              $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
 594          }
 595  
 596          if ( (int) $q['year'] ) {
 597              $q['year'] = '' . intval($q['year']);
 598              $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
 599          }
 600  
 601          if ( (int) $q['monthnum'] ) {
 602              $q['monthnum'] = '' . intval($q['monthnum']);
 603              $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
 604          }
 605  
 606          if ( (int) $q['day'] ) {
 607              $q['day'] = '' . intval($q['day']);
 608              $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
 609          }
 610  
 611          // Compat.  Map subpost to attachment.
 612          if ( '' != $q['subpost'] )
 613              $q['attachment'] = $q['subpost'];
 614          if ( '' != $q['subpost_id'] )
 615              $q['attachment_id'] = $q['subpost_id'];
 616  
 617          if ('' != $q['name']) {
 618              $q['name'] = sanitize_title($q['name']);
 619              $where .= " AND post_name = '" . $q['name'] . "'";
 620          } else if ('' != $q['pagename']) {
 621              $reqpage = get_page_by_path($q['pagename']);
 622              if ( !empty($reqpage) )
 623                  $reqpage = $reqpage->ID;
 624              else
 625                  $reqpage = 0;
 626  
 627              if  ( ('page' == get_option('show_on_front') ) && ( $reqpage == get_option('page_for_posts') ) ) {
 628                  $this->is_page = false;
 629                  $this->is_home = true;
 630              } else {
 631                  $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
 632                  $page_paths = '/' . trim($q['pagename'], '/');
 633                  $q['pagename'] = sanitize_title(basename($page_paths));
 634                  $q['name'] = $q['pagename'];
 635                  $where .= " AND (ID = '$reqpage')";
 636              }
 637          } elseif ('' != $q['attachment']) {
 638              $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
 639              $attach_paths = '/' . trim($q['attachment'], '/');
 640              $q['attachment'] = sanitize_title(basename($attach_paths));
 641              $q['name'] = $q['attachment'];
 642              $where .= " AND post_name = '" . $q['attachment'] . "'";
 643          }
 644  
 645          if ( (int) $q['w'] ) {
 646              $q['w'] = ''.intval($q['w']);
 647              $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
 648          }
 649  
 650          if ( intval($q['comments_popup']) )
 651              $q['p'] = intval($q['comments_popup']);
 652  
 653          // If a attachment is requested by number, let it supercede any post number.
 654          if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) )
 655              $q['p'] = (int) $q['attachment_id'];
 656  
 657          // If a post number is specified, load that post
 658          if (($q['p'] != '') && intval($q['p']) != 0) {
 659              $q['p'] =  (int) $q['p'];
 660              $where = ' AND ID = ' . $q['p'];
 661          }
 662  
 663          if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) {
 664              $q['page_id'] = intval($q['page_id']);
 665              if  ( ('page' == get_option('show_on_front') ) && ( $q['page_id'] == get_option('page_for_posts') ) ) {
 666                  $this->is_page = false;
 667                  $this->is_home = true;
 668              } else {
 669                  $q['p'] = $q['page_id'];
 670                  $where = ' AND ID = '.$q['page_id'];
 671              }
 672          }
 673  
 674          // If a search pattern is specified, load the posts that match
 675          if (!empty($q['s'])) {
 676              $q['s'] = addslashes_gpc($q['s']);
 677              $search = ' AND (';
 678              $q['s'] = preg_replace('/, +/', ' ', $q['s']);
 679              $q['s'] = str_replace(',', ' ', $q['s']);
 680              $q['s'] = str_replace('"', ' ', $q['s']);
 681              $q['s'] = trim($q['s']);
 682              if ($q['exact']) {
 683                  $n = '';
 684              } else {
 685                  $n = '%';
 686              }
 687              if (!$q['sentence']) {
 688                  $s_array = explode(' ',$q['s']);
 689                  $q['search_terms'] = $s_array;
 690                  $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
 691                  for ( $i = 1; $i < count($s_array); $i = $i + 1) {
 692                      $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
 693                  }
 694                  $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')';
 695                  $search .= ')';
 696              } else {
 697                  $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))';
 698              }
 699          }
 700  
 701          // Category stuff
 702  
 703          if ((empty($q['cat'])) || ($q['cat'] == '0') || 
 704                  // Bypass cat checks if fetching specific posts
 705                  ( $this->is_single || $this->is_page )) {
 706              $whichcat='';
 707          } else {
 708              $q['cat'] = ''.urldecode($q['cat']).'';
 709              $q['cat'] = addslashes_gpc($q['cat']);
 710              $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
 711              $cat_array = preg_split('/[,\s]+/', $q['cat']);
 712              $in_cats = $out_cats = $out_posts = '';
 713              foreach ( $cat_array as $cat ) {
 714                  $cat = intval($cat);
 715                  $in = strstr($cat, '-') ? false : true;
 716                  $cat = trim($cat, '-');
 717                  if ( $in )
 718                      $in_cats .= "$cat, " . get_category_children($cat, '', ', ');
 719                  else
 720                      $out_cats .= "$cat, " . get_category_children($cat, '', ', ');                
 721              }
 722              $in_cats = substr($in_cats, 0, -2);
 723              $out_cats = substr($out_cats, 0, -2);
 724              if ( strlen($in_cats) > 0 )
 725                  $in_cats = " AND category_id IN ($in_cats)";
 726              if ( strlen($out_cats) > 0 ) {
 727                  $ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id IN ($out_cats)");
 728                  if ( is_array($ids) && count($ids > 0) ) {
 729                      foreach ( $ids as $id )
 730                          $out_posts .= "$id, ";
 731                      $out_posts = substr($out_posts, 0, -2);
 732                  }
 733                  if ( strlen($out_posts) > 0 )
 734                      $out_cats = " AND ID NOT IN ($out_posts)";
 735              }
 736              $whichcat = $in_cats . $out_cats;
 737              $distinct = 'DISTINCT';
 738          }
 739  
 740          // Category stuff for nice URIs
 741  
 742          global $cache_categories;
 743          if ('' != $q['category_name']) {
 744              $reqcat = get_category_by_path($q['category_name']);
 745              $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
 746              $cat_paths = '/' . trim($q['category_name'], '/');
 747              $q['category_name'] = sanitize_title(basename($cat_paths));
 748  
 749              $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
 750              $q['category_name'] = sanitize_title(basename($cat_paths));
 751              $cat_paths = explode('/', $cat_paths);
 752              foreach($cat_paths as $pathdir)
 753                  $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
 754  
 755              //if we don't match the entire hierarchy fallback on just matching the nicename
 756              if ( empty($reqcat) )
 757                  $reqcat = get_category_by_path($q['category_name'], false);
 758  
 759              if ( !empty($reqcat) )
 760                  $reqcat = $reqcat->cat_ID;
 761              else
 762                  $reqcat = 0;
 763  
 764              $q['cat'] = $reqcat;
 765                  
 766              $tables = ", $wpdb->post2cat, $wpdb->categories";
 767              $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
 768              $whichcat = " AND category_id IN ({$q['cat']}, ";
 769              $whichcat .= get_category_children($q['cat'], '', ', ');
 770              $whichcat = substr($whichcat, 0, -2);
 771              $whichcat .= ")";
 772              $distinct = 'DISTINCT';
 773          }
 774  
 775          // Author/user stuff
 776  
 777          if ((empty($q['author'])) || ($q['author'] == '0')) {
 778              $whichauthor='';
 779          } else {
 780              $q['author'] = ''.urldecode($q['author']).'';
 781              $q['author'] = addslashes_gpc($q['author']);
 782              if (stristr($q['author'], '-')) {
 783                  $eq = '!=';
 784                  $andor = 'AND';
 785                  $q['author'] = explode('-', $q['author']);
 786                  $q['author'] = ''.intval($q['author'][1]);
 787              } else {
 788                  $eq = '=';
 789                  $andor = 'OR';
 790              }
 791              $author_array = preg_split('/[,\s]+/', $q['author']);
 792              $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
 793              for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
 794                  $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
 795              }
 796              $whichauthor .= ')';
 797          }
 798  
 799          // Author stuff for nice URIs
 800  
 801          if ('' != $q['author_name']) {
 802              if (stristr($q['author_name'],'/')) {
 803                  $q['author_name'] = explode('/',$q['author_name']);
 804                  if ($q['author_name'][count($q['author_name'])-1]) {
 805                      $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
 806                  } else {
 807                      $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
 808                  }
 809              }
 810              $q['author_name'] = sanitize_title($q['author_name']);
 811              $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
 812              $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
 813          }
 814  
 815          $where .= $search.$whichcat.$whichauthor;
 816  
 817          if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) {
 818              $q['order']='DESC';
 819          }
 820  
 821          // Order by
 822          if (empty($q['orderby'])) {
 823              $q['orderby'] = 'post_date '.$q['order'];
 824          } else {
 825              // Used to filter values
 826              $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');
 827              $q['orderby'] = urldecode($q['orderby']);
 828              $q['orderby'] = addslashes_gpc($q['orderby']);
 829              $orderby_array = explode(' ',$q['orderby']);
 830              if ( empty($orderby_array) )
 831                  $orderby_array[] = $q['orderby'];
 832              $q['orderby'] = '';
 833              for ($i = 0; $i < count($orderby_array); $i++) {
 834                  // Only allow certain values for safety
 835                  $orderby = $orderby_array[$i];
 836                  if ( 'menu_order' != $orderby )
 837                      $orderby = 'post_' . $orderby;
 838                  if ( in_array($orderby_array[$i], $allowed_keys) )
 839                      $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}";
 840              }
 841              if ( empty($q['orderby']) )
 842                  $q['orderby'] = 'post_date '.$q['order'];
 843          }
 844  
 845          if ( $this->is_attachment ) {
 846              $where .= " AND (post_type = 'attachment')";
 847          } elseif ($this->is_page) {
 848              $where .= " AND (post_type = 'page')";
 849          } elseif ($this->is_single) {
 850              $where .= " AND (post_type = 'post')";
 851          } else {
 852              $where .= " AND (post_type = '$post_type' AND (post_status = 'publish'";
 853  
 854              if ( is_admin() )
 855                  $where .= " OR post_status = 'future' OR post_status = 'draft'";
 856      
 857              if ( is_user_logged_in() ) {
 858                  if ( 'post' == $post_type )
 859                      $cap = 'edit_private_posts';
 860                  else
 861                      $cap = 'edit_private_pages';
 862  
 863                  if ( current_user_can($cap) )
 864                      $where .= " OR post_status = 'private'";
 865                  else
 866                  $where .= " OR post_author = $user_ID AND post_status = 'private'";
 867              }
 868  
 869              $where .= '))';
 870          }
 871  
 872          // Apply filters on where and join prior to paging so that any
 873          // manipulations to them are reflected in the paging by day queries.
 874          $where = apply_filters('posts_where', $where);
 875          $join = apply_filters('posts_join', $join);
 876  
 877          // Paging
 878          if (empty($q['nopaging']) && ! $this->is_single && ! $this->is_page) {
 879              $page = abs(intval($q['paged']));
 880              if (empty($page)) {
 881                  $page = 1;
 882              }
 883  
 884              if (($q['what_to_show'] == 'posts')) {
 885                  $q['offset'] = abs(intval($q['offset']));
 886                  if ( empty($q['offset']) ) {
 887                      $pgstrt = '';
 888                      $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
 889                      $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
 890                  } else { // we're ignoring $page and using 'offset'
 891                      $pgstrt = $q['offset'] . ', ';
 892                      $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
 893                  }
 894              } elseif ($q['what_to_show'] == 'days') {
 895                  $startrow = $q['posts_per_page'] * (intval($page)-1);
 896                  $start_date = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $startrow,1");
 897                  $endrow = $startrow + $q['posts_per_page'] - 1;
 898                  $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1");
 899  
 900                  if ($page > 1) {
 901                      $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'";
 902                  } else {
 903                      $where .= " AND post_date >= '$end_date'";
 904                  }
 905              }
 906          }
 907  
 908          // Apply post-paging filters on where and join.  Only plugins that
 909          // manipulate paging queries should use these hooks.
 910          $where = apply_filters('posts_where_paged', $where);
 911          $groupby = '';
 912          $groupby = apply_filters('posts_groupby', $groupby);
 913          if ( ! empty($groupby) )
 914              $groupby = 'GROUP BY ' . $groupby;
 915          $join = apply_filters('posts_join_paged', $join);
 916          $orderby = apply_filters('posts_orderby', $q['orderby']);
 917          $distinct = apply_filters('posts_distinct', $distinct);
 918          $fields = apply_filters('posts_fields', "$wpdb->posts.*");
 919          $request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby ORDER BY $orderby $limits";
 920          $this->request = apply_filters('posts_request', $request);
 921  
 922          $this->posts = $wpdb->get_results($this->request);
 923  
 924          // Check post status to determine if post should be displayed.
 925          if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
 926              $status = get_post_status($this->posts[0]);
 927              //$type = get_post_type($this->posts[0]);
 928              if ( ('publish' != $status) ) {
 929                  if ( ! is_user_logged_in() ) {
 930                      // User must be logged in to view unpublished posts.
 931                      $this->posts = array();
 932                  } else {
 933                      if ('draft' == $status) {
 934                          // User must have edit permissions on the draft to preview.
 935                          if (! current_user_can('edit_post', $this->posts[0]->ID)) {
 936                              $this->posts = array();
 937                          } else {
 938                              $this->is_preview = true;
 939                              $this->posts[0]->post_date = current_time('mysql');
 940                          }
 941                      }  else if ('future' == $status) {
 942                          $this->is_preview = true;
 943                          if (!current_user_can('edit_post', $this->posts[0]->ID)) {
 944                              $this->posts = array ( );
 945                          }
 946                      } else {
 947                          if (! current_user_can('read_post', $this->posts[0]->ID))
 948                              $this->posts = array();
 949                      }
 950                  }
 951              }
 952          }
 953  
 954          update_post_caches($this->posts);
 955  
 956          $this->posts = apply_filters('the_posts', $this->posts);
 957          $this->post_count = count($this->posts);
 958          if ($this->post_count > 0) {
 959              $this->post = $this->posts[0];
 960          }
 961  
 962          return $this->posts;
 963      }
 964  
 965  	function next_post() {
 966          
 967          $this->current_post++;
 968  
 969          $this->post = $this->posts[$this->current_post];
 970          return $this->post;
 971      }
 972  
 973  	function the_post() {
 974          global $post;
 975          $this->in_the_loop = true;
 976          $post = $this->next_post();
 977          setup_postdata($post);
 978  
 979          if ( $this->current_post == 0 ) // loop has just started
 980              do_action('loop_start');
 981      }
 982  
 983  	function have_posts() {
 984          if ($this->current_post + 1 < $this->post_count) {
 985              return true;
 986          } elseif ($this->current_post + 1 == $this->post_count) {
 987              do_action('loop_end');
 988              // Do some cleaning up after the loop
 989              $this->rewind_posts();
 990          }
 991  
 992          $this->in_the_loop = false;
 993          return false;
 994      }
 995  
 996  	function rewind_posts() {
 997          $this->current_post = -1;
 998          if ($this->post_count > 0) {
 999              $this->post = $this->posts[0];
1000          }
1001      }
1002      
1003      function &query($query) {
1004          $this->parse_query($query);
1005          return $this->get_posts();
1006      }
1007  
1008  	function get_queried_object() {
1009          if (isset($this->queried_object)) {
1010              return $this->queried_object;
1011          }
1012  
1013          $this->queried_object = NULL;
1014          $this->queried_object_id = 0;
1015  
1016          if ($this->is_category) {
1017              $cat = $this->get('cat');
1018              $category = &get_category($cat);
1019              $this->queried_object = &$category;
1020              $this->queried_object_id = $cat;
1021          } else if ($this->is_single) {
1022              $this->queried_object = $this->post;
1023              $this->queried_object_id = $this->post->ID;
1024          } else if ($this->is_page) {
1025              $this->queried_object = $this->post;
1026              $this->queried_object_id = $this->post->ID;
1027          } else if ($this->is_author) {
1028              $author_id = $this->get('author');
1029              $author = get_userdata($author_id);
1030              $this->queried_object = $author;
1031              $this->queried_object_id = $author_id;
1032          }
1033  
1034          return $this->queried_object;
1035      }
1036  
1037  	function get_queried_object_id() {
1038          $this->get_queried_object();
1039  
1040          if (isset($this->queried_object_id)) {
1041              return $this->queried_object_id;
1042          }
1043  
1044          return 0;
1045      }
1046  
1047  	function WP_Query ($query = '') {
1048          if (! empty($query)) {
1049              $this->query($query);
1050          }
1051      }
1052  }
1053  
1054  //
1055  // Private helper functions
1056  //
1057  
1058  // Setup global post data.
1059  function setup_postdata($post) {
1060      global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query;
1061      global $pagenow;
1062  
1063      $id = $post->ID;
1064  
1065      $authordata = get_userdata($post->post_author);
1066  
1067      $day = mysql2date('d.m.y', $post->post_date);
1068      $currentmonth = mysql2date('m', $post->post_date);
1069      $numpages = 1;
1070      $page = get_query_var('page');
1071      if ( !$page )
1072          $page = 1;
1073      if ( is_single() || is_page() )
1074          $more = 1;
1075      $content = $post->post_content;
1076      if ( preg_match('/<!--nextpage-->/', $content) ) {
1077          if ( $page > 1 )
1078              $more = 1;
1079          $multipage = 1;
1080          $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
1081          $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
1082          $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
1083          $pages = explode('<!--nextpage-->', $content);
1084          $numpages = count($pages);
1085      } else {
1086          $pages[0] = $post->post_content;
1087          $multipage = 0;
1088      }
1089      return true;
1090  }
1091  
1092  ?>

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Sat Jul 15 11:57:04 2006 Courtesy of Taragana