[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

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

title

Body

[close]

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

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


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7