PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

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 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  }
  29  
  30  /*
  31   * Query type checks.
  32   */
  33  
  34  function is_admin () {
  35      global $wp_query;
  36  
  37      return ($wp_query->is_admin || (stripos($_SERVER['REQUEST_URI'], 'wp-admin/') !== false));
  38  }
  39  
  40  function is_archive () {
  41      global $wp_query;
  42  
  43      return $wp_query->is_archive;
  44  }
  45  
  46  function is_attachment () {
  47      global $wp_query;
  48  
  49      return $wp_query->is_attachment;
  50  }
  51  
  52  function is_author ($author = '') {
  53      global $wp_query;
  54  
  55      if ( !$wp_query->is_author )
  56          return false;
  57  
  58      if ( empty($author) )
  59          return true;
  60  
  61      $author_obj = $wp_query->get_queried_object();
  62  
  63      if ( $author == $author_obj->ID )
  64          return true;
  65      elseif ( $author == $author_obj->nickname )
  66          return true;
  67      elseif ( $author == $author_obj->user_nicename )
  68          return true;
  69  
  70      return false;
  71  }
  72  
  73  function is_category ($category = '') {
  74      global $wp_query;
  75  
  76      if ( !$wp_query->is_category )
  77          return false;
  78  
  79      if ( empty($category) )
  80          return true;
  81  
  82      $cat_obj = $wp_query->get_queried_object();
  83  
  84      if ( $category == $cat_obj->term_id )
  85          return true;
  86      else if ( $category == $cat_obj->name )
  87          return true;
  88      elseif ( $category == $cat_obj->slug )
  89          return true;
  90  
  91      return false;
  92  }
  93  
  94  function is_tag( $slug = '' ) {
  95      global $wp_query;
  96      if ( !$wp_query->is_tag )
  97          return false;
  98  
  99      if ( empty( $slug ) )
 100          return true;
 101  
 102      $tag_obj = $wp_query->get_queried_object();
 103      if ( $slug == $tag_obj->slug )
 104          return true;
 105      return false;
 106  }
 107  
 108  function is_comments_popup () {
 109      global $wp_query;
 110  
 111      return $wp_query->is_comments_popup;
 112  }
 113  
 114  function is_date () {
 115      global $wp_query;
 116  
 117      return $wp_query->is_date;
 118  }
 119  
 120  function is_day () {
 121      global $wp_query;
 122  
 123      return $wp_query->is_day;
 124  }
 125  
 126  function is_feed () {
 127      global $wp_query;
 128  
 129      return $wp_query->is_feed;
 130  }
 131  
 132  function is_home () {
 133      global $wp_query;
 134  
 135      return $wp_query->is_home;
 136  }
 137  
 138  function is_month () {
 139      global $wp_query;
 140  
 141      return $wp_query->is_month;
 142  }
 143  
 144  function is_page ($page = '') {
 145      global $wp_query;
 146  
 147      if ( !$wp_query->is_page )
 148          return false;
 149  
 150      if ( empty($page) )
 151          return true;
 152  
 153      $page_obj = $wp_query->get_queried_object();
 154  
 155      if ( $page == $page_obj->ID )
 156          return true;
 157      elseif ( $page == $page_obj->post_title )
 158          return true;
 159      else if ( $page == $page_obj->post_name )
 160          return true;
 161  
 162      return false;
 163  }
 164  
 165  function is_paged () {
 166      global $wp_query;
 167  
 168      return $wp_query->is_paged;
 169  }
 170  
 171  function is_plugin_page() {
 172      global $plugin_page;
 173  
 174      if ( isset($plugin_page) )
 175          return true;
 176  
 177      return false;
 178  }
 179  
 180  function is_preview() {
 181      global $wp_query;
 182  
 183      return $wp_query->is_preview;
 184  }
 185  
 186  function is_robots() {
 187      global $wp_query;
 188  
 189      return $wp_query->is_robots;
 190  }
 191  
 192  function is_search () {
 193      global $wp_query;
 194  
 195      return $wp_query->is_search;
 196  }
 197  
 198  function is_single ($post = '') {
 199      global $wp_query;
 200  
 201      if ( !$wp_query->is_single )
 202          return false;
 203  
 204      if ( empty( $post) )
 205          return true;
 206  
 207      $post_obj = $wp_query->get_queried_object();
 208  
 209      if ( $post == $post_obj->ID )
 210          return true;
 211      elseif ( $post == $post_obj->post_title )
 212          return true;
 213      elseif ( $post == $post_obj->post_name )
 214          return true;
 215  
 216      return false;
 217  }
 218  
 219  function is_singular() {
 220      global $wp_query;
 221  
 222      return $wp_query->is_singular;
 223  }
 224  
 225  function is_time () {
 226      global $wp_query;
 227  
 228      return $wp_query->is_time;
 229  }
 230  
 231  function is_trackback () {
 232      global $wp_query;
 233  
 234      return $wp_query->is_trackback;
 235  }
 236  
 237  function is_year () {
 238      global $wp_query;
 239  
 240      return $wp_query->is_year;
 241  }
 242  
 243  function is_404 () {
 244      global $wp_query;
 245  
 246      return $wp_query->is_404;
 247  }
 248  
 249  /*
 250   * The Loop.  Post loop control.
 251   */
 252  
 253  function have_posts() {
 254      global $wp_query;
 255  
 256      return $wp_query->have_posts();
 257  }
 258  
 259  function in_the_loop() {
 260      global $wp_query;
 261  
 262      return $wp_query->in_the_loop;
 263  }
 264  
 265  function rewind_posts() {
 266      global $wp_query;
 267  
 268      return $wp_query->rewind_posts();
 269  }
 270  
 271  function the_post() {
 272      global $wp_query;
 273  
 274      $wp_query->the_post();
 275  }
 276  
 277  /*
 278   * Comments loop.
 279   */
 280  
 281  function have_comments() {
 282      global $wp_query;
 283      return $wp_query->have_comments();
 284  }
 285  
 286  function the_comment() {
 287      global $wp_query;
 288      return $wp_query->the_comment();
 289  }
 290  
 291  /*
 292   * WP_Query
 293   */
 294  
 295  class WP_Query {
 296      var $query;
 297      var $query_vars = array();
 298      var $queried_object;
 299      var $queried_object_id;
 300      var $request;
 301  
 302      var $posts;
 303      var $post_count = 0;
 304      var $current_post = -1;
 305      var $in_the_loop = false;
 306      var $post;
 307  
 308      var $comments;
 309      var $comment_count = 0;
 310      var $current_comment = -1;
 311      var $comment;
 312  
 313      var $found_posts = 0;
 314      var $max_num_pages = 0;
 315  
 316      var $is_single = false;
 317      var $is_preview = false;
 318      var $is_page = false;
 319      var $is_archive = false;
 320      var $is_date = false;
 321      var $is_year = false;
 322      var $is_month = false;
 323      var $is_day = false;
 324      var $is_time = false;
 325      var $is_author = false;
 326      var $is_category = false;
 327      var $is_tag = false;
 328      var $is_search = false;
 329      var $is_feed = false;
 330      var $is_comment_feed = false;
 331      var $is_trackback = false;
 332      var $is_home = false;
 333      var $is_404 = false;
 334      var $is_comments_popup = false;
 335      var $is_admin = false;
 336      var $is_attachment = false;
 337      var $is_singular = false;
 338      var $is_robots = false;
 339      var $is_posts_page = false;
 340  
 341  	function init_query_flags() {
 342          $this->is_single = false;
 343          $this->is_page = false;
 344          $this->is_archive = false;
 345          $this->is_date = false;
 346          $this->is_year = false;
 347          $this->is_month = false;
 348          $this->is_day = false;
 349          $this->is_time = false;
 350          $this->is_author = false;
 351          $this->is_category = false;
 352          $this->is_tag = false;
 353          $this->is_search = false;
 354          $this->is_feed = false;
 355          $this->is_comment_feed = false;
 356          $this->is_trackback = false;
 357          $this->is_home = false;
 358          $this->is_404 = false;
 359          $this->is_paged = false;
 360          $this->is_admin = false;
 361          $this->is_attachment = false;
 362          $this->is_singular = false;
 363          $this->is_robots = false;
 364          $this->is_posts_page = false;
 365      }
 366  
 367  	function init () {
 368          unset($this->posts);
 369          unset($this->query);
 370          $this->query_vars = array();
 371          unset($this->queried_object);
 372          unset($this->queried_object_id);
 373          $this->post_count = 0;
 374          $this->current_post = -1;
 375          $this->in_the_loop = false;
 376  
 377          $this->init_query_flags();
 378      }
 379  
 380      // Reparse the query vars.
 381  	function parse_query_vars() {
 382          $this->parse_query('');
 383      }
 384  
 385  	function fill_query_vars($array) {
 386          $keys = array(
 387              'error'
 388              , 'm'
 389              , 'p'
 390              , 'subpost'
 391              , 'subpost_id'
 392              , 'attachment'
 393              , 'attachment_id'
 394              , 'name'
 395              , 'hour'
 396              , 'static'
 397              , 'pagename'
 398              , 'page_id'
 399              , 'second'
 400              , 'minute'
 401              , 'hour'
 402              , 'day'
 403              , 'monthnum'
 404              , 'year'
 405              , 'w'
 406              , 'category_name'
 407              , 'tag'
 408              , 'tag_id'
 409              , 'author_name'
 410              , 'feed'
 411              , 'tb'
 412              , 'paged'
 413              , 'comments_popup'
 414              , 'preview'
 415          );
 416  
 417          foreach ($keys as $key) {
 418              if ( !isset($array[$key]))
 419                  $array[$key] = '';
 420          }
 421  
 422          $array_keys = array('category__in', 'category__not_in', 'category__and',
 423              'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
 424  
 425          foreach ( $array_keys as $key ) {
 426              if ( !isset($array[$key]))
 427                  $array[$key] = array();
 428          }
 429          return $array;
 430      }
 431  
 432      // Parse a query string and set query type booleans.
 433  	function parse_query ($query) {
 434          if ( !empty($query) || !isset($this->query) ) {
 435              $this->init();
 436              if ( is_array($query) )
 437                  $this->query_vars = $query;
 438              else
 439                  parse_str($query, $this->query_vars);
 440              $this->query = $query;
 441          }
 442  
 443          $this->query_vars = $this->fill_query_vars($this->query_vars);
 444          $qv = &$this->query_vars;
 445  
 446          if ( ! empty($qv['robots']) )
 447              $this->is_robots = true;
 448  
 449          $qv['p'] =  (int) $qv['p'];
 450          $qv['page_id'] =  (int) $qv['page_id'];
 451          $qv['year'] = (int) $qv['year'];
 452          $qv['monthnum'] = (int) $qv['monthnum'];
 453          $qv['day'] = (int) $qv['day'];
 454          $qv['w'] = (int) $qv['w'];
 455          $qv['m'] =  (int) $qv['m'];
 456          if ( '' !== $qv['hour'] ) $qv['hour'] = (int) $qv['hour'];
 457          if ( '' !== $qv['minute'] ) $qv['minute'] = (int) $qv['minute'];
 458          if ( '' !== $qv['second'] ) $qv['second'] = (int) $qv['second'];
 459  
 460          // Compat.  Map subpost to attachment.
 461          if ( '' != $qv['subpost'] )
 462              $qv['attachment'] = $qv['subpost'];
 463          if ( '' != $qv['subpost_id'] )
 464              $qv['attachment_id'] = $qv['subpost_id'];
 465  
 466          $qv['attachment_id'] = (int) $qv['attachment_id'];
 467  
 468          if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
 469              $this->is_single = true;
 470              $this->is_attachment = true;
 471          } elseif ( '' != $qv['name'] ) {
 472              $this->is_single = true;
 473          } elseif ( $qv['p'] ) {
 474              $this->is_single = true;
 475          } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
 476              // If year, month, day, hour, minute, and second are set, a single
 477              // post is being queried.
 478              $this->is_single = true;
 479          } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
 480              $this->is_page = true;
 481              $this->is_single = false;
 482          } elseif ( !empty($qv['s']) ) {
 483              $this->is_search = true;
 484          } else {
 485          // Look for archive queries.  Dates, categories, authors.
 486  
 487              if ( '' !== $qv['second'] ) {
 488                  $this->is_time = true;
 489                  $this->is_date = true;
 490              }
 491  
 492              if ( '' !== $qv['minute'] ) {
 493                  $this->is_time = true;
 494                  $this->is_date = true;
 495              }
 496  
 497              if ( '' !== $qv['hour'] ) {
 498                  $this->is_time = true;
 499                  $this->is_date = true;
 500              }
 501  
 502              if ( $qv['day'] ) {
 503                  if (! $this->is_date) {
 504                      $this->is_day = true;
 505                      $this->is_date = true;
 506                  }
 507              }
 508  
 509              if ( $qv['monthnum'] ) {
 510                  if (! $this->is_date) {
 511                      $this->is_month = true;
 512                      $this->is_date = true;
 513                  }
 514              }
 515  
 516              if ( $qv['year'] ) {
 517                  if (! $this->is_date) {
 518                      $this->is_year = true;
 519                      $this->is_date = true;
 520                  }
 521              }
 522  
 523              if ( $qv['m'] ) {
 524                  $this->is_date = true;
 525                  if (strlen($qv['m']) > 9) {
 526                      $this->is_time = true;
 527                  } else if (strlen($qv['m']) > 7) {
 528                      $this->is_day = true;
 529                  } else if (strlen($qv['m']) > 5) {
 530                      $this->is_month = true;
 531                  } else {
 532                      $this->is_year = true;
 533                  }
 534              }
 535  
 536              if ('' != $qv['w']) {
 537                  $this->is_date = true;
 538              }
 539  
 540              if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
 541                  $this->is_category = false;
 542              } else {
 543                  if (strpos($qv['cat'], '-') !== false) {
 544                      $this->is_category = false;
 545                  } else {
 546                      $this->is_category = true;
 547                  }
 548              }
 549  
 550              if ( '' != $qv['category_name'] ) {
 551                  $this->is_category = true;
 552              }
 553  
 554              if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
 555                  $qv['category__in'] = array();
 556              } else {
 557                  $qv['category__in'] = array_map('intval', $qv['category__in']);
 558                  $this->is_category = true;
 559              }
 560  
 561              if ( !is_array($qv['category___not_in']) || empty($qv['category__not_in']) ) {
 562                  $qv['category__not_in'] = array();
 563              } else {
 564                  $qv['category__not_in'] = array_map('intval', $qv['category__not_in']);
 565              }
 566  
 567              if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
 568                  $qv['category__and'] = array();
 569              } else {
 570                  $qv['category__and'] = array_map('intval', $qv['category__and']);
 571                  $this->is_category = true;
 572              }
 573  
 574              if (  '' != $qv['tag'] )
 575                  $this->is_tag = true;
 576  
 577              $qv['tag_id'] = (int) $qv['tag_id'];
 578              if (  !empty($qv['tag_id']) )
 579                  $this->is_tag = true;
 580  
 581              if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
 582                  $qv['tag__in'] = array();
 583              } else {
 584                  $qv['tag__in'] = array_map('intval', $qv['tag__in']);
 585                  $this->is_tag = true;
 586              }
 587  
 588              if ( !is_array($qv['tag___not_in']) || empty($qv['tag__not_in']) ) {
 589                  $qv['tag__not_in'] = array();
 590              } else {
 591                  $qv['tag__not_in'] = array_map('intval', $qv['tag__not_in']);
 592              }
 593  
 594              if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
 595                  $qv['tag__and'] = array();
 596              } else {
 597                  $qv['tag__and'] = array_map('intval', $qv['tag__and']);
 598                  $this->is_category = true;
 599              }
 600  
 601              if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
 602                  $qv['tag_slug__in'] = array();
 603              } else {
 604                  $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
 605                  $this->is_tag = true;
 606              }