[ 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)=' .