[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 the user is on. It
   6   * also provides functionality for getting URL query information.
   7   *
   8   * @link https://developer.wordpress.org/themes/basics/the-loop/ More information on The Loop.
   9   *
  10   * @package WordPress
  11   * @subpackage Query
  12   */
  13  
  14  /**
  15   * Retrieves the value of a query variable in the WP_Query class.
  16   *
  17   * @since 1.5.0
  18   * @since 3.9.0 The `$default_value` argument was introduced.
  19   *
  20   * @global WP_Query $wp_query WordPress Query object.
  21   *
  22   * @param string $query_var     The variable key to retrieve.
  23   * @param mixed  $default_value Optional. Value to return if the query variable is not set.
  24   *                              Default empty string.
  25   * @return mixed Contents of the query variable.
  26   */
  27  function get_query_var( $query_var, $default_value = '' ) {
  28      global $wp_query;
  29      return $wp_query->get( $query_var, $default_value );
  30  }
  31  
  32  /**
  33   * Retrieves the currently queried object.
  34   *
  35   * Wrapper for WP_Query::get_queried_object().
  36   *
  37   * @since 3.1.0
  38   *
  39   * @global WP_Query $wp_query WordPress Query object.
  40   *
  41   * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.
  42   */
  43  function get_queried_object() {
  44      global $wp_query;
  45      return $wp_query->get_queried_object();
  46  }
  47  
  48  /**
  49   * Retrieves the ID of the currently queried object.
  50   *
  51   * Wrapper for WP_Query::get_queried_object_id().
  52   *
  53   * @since 3.1.0
  54   *
  55   * @global WP_Query $wp_query WordPress Query object.
  56   *
  57   * @return int ID of the queried object.
  58   */
  59  function get_queried_object_id() {
  60      global $wp_query;
  61      return $wp_query->get_queried_object_id();
  62  }
  63  
  64  /**
  65   * Sets the value of a query variable in the WP_Query class.
  66   *
  67   * @since 2.2.0
  68   *
  69   * @global WP_Query $wp_query WordPress Query object.
  70   *
  71   * @param string $query_var Query variable key.
  72   * @param mixed  $value     Query variable value.
  73   */
  74  function set_query_var( $query_var, $value ) {
  75      global $wp_query;
  76      $wp_query->set( $query_var, $value );
  77  }
  78  
  79  /**
  80   * Sets up The Loop with query parameters.
  81   *
  82   * Note: This function will completely override the main query and isn't intended for use
  83   * by plugins or themes. Its overly-simplistic approach to modifying the main query can be
  84   * problematic and should be avoided wherever possible. In most cases, there are better,
  85   * more performant options for modifying the main query such as via the {@see 'pre_get_posts'}
  86   * action within WP_Query.
  87   *
  88   * This must not be used within the WordPress Loop.
  89   *
  90   * @since 1.5.0
  91   *
  92   * @global WP_Query $wp_query WordPress Query object.
  93   *
  94   * @param array|string $query Array or string of WP_Query arguments.
  95   * @return WP_Post[]|int[] Array of post objects or post IDs.
  96   */
  97  function query_posts( $query ) {
  98      $GLOBALS['wp_query'] = new WP_Query();
  99      return $GLOBALS['wp_query']->query( $query );
 100  }
 101  
 102  /**
 103   * Destroys the previous query and sets up a new query.
 104   *
 105   * This should be used after query_posts() and before another query_posts().
 106   * This will remove obscure bugs that occur when the previous WP_Query object
 107   * is not destroyed properly before another is set up.
 108   *
 109   * @since 2.3.0
 110   *
 111   * @global WP_Query $wp_query     WordPress Query object.
 112   * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
 113   */
 114  function wp_reset_query() {
 115      $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
 116      wp_reset_postdata();
 117  }
 118  
 119  /**
 120   * After looping through a separate query, this function restores
 121   * the $post global to the current post in the main query.
 122   *
 123   * @since 3.0.0
 124   *
 125   * @global WP_Query $wp_query WordPress Query object.
 126   */
 127  function wp_reset_postdata() {
 128      global $wp_query;
 129  
 130      if ( isset( $wp_query ) ) {
 131          $wp_query->reset_postdata();
 132      }
 133  }
 134  
 135  /*
 136   * Query type checks.
 137   */
 138  
 139  /**
 140   * Determines whether the query is for an existing archive page.
 141   *
 142   * Archive pages include category, tag, author, date, custom post type,
 143   * and custom taxonomy based archives.
 144   *
 145   * For more information on this and similar theme functions, check out
 146   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 147   * Conditional Tags} article in the Theme Developer Handbook.
 148   *
 149   * @since 1.5.0
 150   *
 151   * @see is_category()
 152   * @see is_tag()
 153   * @see is_author()
 154   * @see is_date()
 155   * @see is_post_type_archive()
 156   * @see is_tax()
 157   * @global WP_Query $wp_query WordPress Query object.
 158   *
 159   * @return bool Whether the query is for an existing archive page.
 160   */
 161  function is_archive() {
 162      global $wp_query;
 163  
 164      if ( ! isset( $wp_query ) ) {
 165          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 166          return false;
 167      }
 168  
 169      return $wp_query->is_archive();
 170  }
 171  
 172  /**
 173   * Determines whether the query is for an existing post type archive page.
 174   *
 175   * For more information on this and similar theme functions, check out
 176   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 177   * Conditional Tags} article in the Theme Developer Handbook.
 178   *
 179   * @since 3.1.0
 180   *
 181   * @global WP_Query $wp_query WordPress Query object.
 182   *
 183   * @param string|string[] $post_types Optional. Post type or array of posts types
 184   *                                    to check against. Default empty.
 185   * @return bool Whether the query is for an existing post type archive page.
 186   */
 187  function is_post_type_archive( $post_types = '' ) {
 188      global $wp_query;
 189  
 190      if ( ! isset( $wp_query ) ) {
 191          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 192          return false;
 193      }
 194  
 195      return $wp_query->is_post_type_archive( $post_types );
 196  }
 197  
 198  /**
 199   * Determines whether the query is for an existing attachment page.
 200   *
 201   * For more information on this and similar theme functions, check out
 202   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 203   * Conditional Tags} article in the Theme Developer Handbook.
 204   *
 205   * @since 2.0.0
 206   *
 207   * @global WP_Query $wp_query WordPress Query object.
 208   *
 209   * @param int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such
 210   *                                              to check against. Default empty.
 211   * @return bool Whether the query is for an existing attachment page.
 212   */
 213  function is_attachment( $attachment = '' ) {
 214      global $wp_query;
 215  
 216      if ( ! isset( $wp_query ) ) {
 217          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 218          return false;
 219      }
 220  
 221      return $wp_query->is_attachment( $attachment );
 222  }
 223  
 224  /**
 225   * Determines whether the query is for an existing author archive page.
 226   *
 227   * If the $author parameter is specified, this function will additionally
 228   * check if the query is for one of the authors specified.
 229   *
 230   * For more information on this and similar theme functions, check out
 231   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 232   * Conditional Tags} article in the Theme Developer Handbook.
 233   *
 234   * @since 1.5.0
 235   *
 236   * @global WP_Query $wp_query WordPress Query object.
 237   *
 238   * @param int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such
 239   *                                          to check against. Default empty.
 240   * @return bool Whether the query is for an existing author archive page.
 241   */
 242  function is_author( $author = '' ) {
 243      global $wp_query;
 244  
 245      if ( ! isset( $wp_query ) ) {
 246          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 247          return false;
 248      }
 249  
 250      return $wp_query->is_author( $author );
 251  }
 252  
 253  /**
 254   * Determines whether the query is for an existing category archive page.
 255   *
 256   * If the $category parameter is specified, this function will additionally
 257   * check if the query is for one of the categories specified.
 258   *
 259   * For more information on this and similar theme functions, check out
 260   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 261   * Conditional Tags} article in the Theme Developer Handbook.
 262   *
 263   * @since 1.5.0
 264   *
 265   * @global WP_Query $wp_query WordPress Query object.
 266   *
 267   * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such
 268   *                                            to check against. Default empty.
 269   * @return bool Whether the query is for an existing category archive page.
 270   */
 271  function is_category( $category = '' ) {
 272      global $wp_query;
 273  
 274      if ( ! isset( $wp_query ) ) {
 275          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 276          return false;
 277      }
 278  
 279      return $wp_query->is_category( $category );
 280  }
 281  
 282  /**
 283   * Determines whether the query is for an existing tag archive page.
 284   *
 285   * If the $tag parameter is specified, this function will additionally
 286   * check if the query is for one of the tags specified.
 287   *
 288   * For more information on this and similar theme functions, check out
 289   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 290   * Conditional Tags} article in the Theme Developer Handbook.
 291   *
 292   * @since 2.3.0
 293   *
 294   * @global WP_Query $wp_query WordPress Query object.
 295   *
 296   * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such
 297   *                                       to check against. Default empty.
 298   * @return bool Whether the query is for an existing tag archive page.
 299   */
 300  function is_tag( $tag = '' ) {
 301      global $wp_query;
 302  
 303      if ( ! isset( $wp_query ) ) {
 304          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 305          return false;
 306      }
 307  
 308      return $wp_query->is_tag( $tag );
 309  }
 310  
 311  /**
 312   * Determines whether the query is for an existing custom taxonomy archive page.
 313   *
 314   * If the $taxonomy parameter is specified, this function will additionally
 315   * check if the query is for that specific $taxonomy.
 316   *
 317   * If the $term parameter is specified in addition to the $taxonomy parameter,
 318   * this function will additionally check if the query is for one of the terms
 319   * specified.
 320   *
 321   * For more information on this and similar theme functions, check out
 322   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 323   * Conditional Tags} article in the Theme Developer Handbook.
 324   *
 325   * @since 2.5.0
 326   *
 327   * @global WP_Query $wp_query WordPress Query object.
 328   *
 329   * @param string|string[]           $taxonomy Optional. Taxonomy slug or slugs to check against.
 330   *                                            Default empty.
 331   * @param int|string|int[]|string[] $term     Optional. Term ID, name, slug, or array of such
 332   *                                            to check against. Default empty.
 333   * @return bool Whether the query is for an existing custom taxonomy archive page.
 334   *              True for custom taxonomy archive pages, false for built-in taxonomies
 335   *              (category and tag archives).
 336   */
 337  function is_tax( $taxonomy = '', $term = '' ) {
 338      global $wp_query;
 339  
 340      if ( ! isset( $wp_query ) ) {
 341          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 342          return false;
 343      }
 344  
 345      return $wp_query->is_tax( $taxonomy, $term );
 346  }
 347  
 348  /**
 349   * Determines whether the query is for an existing date archive.
 350   *
 351   * For more information on this and similar theme functions, check out
 352   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 353   * Conditional Tags} article in the Theme Developer Handbook.
 354   *
 355   * @since 1.5.0
 356   *
 357   * @global WP_Query $wp_query WordPress Query object.
 358   *
 359   * @return bool Whether the query is for an existing date archive.
 360   */
 361  function is_date() {
 362      global $wp_query;
 363  
 364      if ( ! isset( $wp_query ) ) {
 365          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 366          return false;
 367      }
 368  
 369      return $wp_query->is_date();
 370  }
 371  
 372  /**
 373   * Determines whether the query is for an existing day archive.
 374   *
 375   * A conditional check to test whether the page is a date-based archive page displaying posts for the current day.
 376   *
 377   * For more information on this and similar theme functions, check out
 378   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 379   * Conditional Tags} article in the Theme Developer Handbook.
 380   *
 381   * @since 1.5.0
 382   *
 383   * @global WP_Query $wp_query WordPress Query object.
 384   *
 385   * @return bool Whether the query is for an existing day archive.
 386   */
 387  function is_day() {
 388      global $wp_query;
 389  
 390      if ( ! isset( $wp_query ) ) {
 391          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 392          return false;
 393      }
 394  
 395      return $wp_query->is_day();
 396  }
 397  
 398  /**
 399   * Determines whether the query is for a feed.
 400   *
 401   * For more information on this and similar theme functions, check out
 402   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 403   * Conditional Tags} article in the Theme Developer Handbook.
 404   *
 405   * @since 1.5.0
 406   *
 407   * @global WP_Query $wp_query WordPress Query object.
 408   *
 409   * @param string|string[] $feeds Optional. Feed type or array of feed types
 410   *                                         to check against. Default empty.
 411   * @return bool Whether the query is for a feed.
 412   */
 413  function is_feed( $feeds = '' ) {
 414      global $wp_query;
 415  
 416      if ( ! isset( $wp_query ) ) {
 417          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 418          return false;
 419      }
 420  
 421      return $wp_query->is_feed( $feeds );
 422  }
 423  
 424  /**
 425   * Is the query for a comments feed?
 426   *
 427   * @since 3.0.0
 428   *
 429   * @global WP_Query $wp_query WordPress Query object.
 430   *
 431   * @return bool Whether the query is for a comments feed.
 432   */
 433  function is_comment_feed() {
 434      global $wp_query;
 435  
 436      if ( ! isset( $wp_query ) ) {
 437          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 438          return false;
 439      }
 440  
 441      return $wp_query->is_comment_feed();
 442  }
 443  
 444  /**
 445   * Determines whether the query is for the front page of the site.
 446   *
 447   * This is for what is displayed at your site's main URL.
 448   *
 449   * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
 450   *
 451   * If you set a static page for the front page of your site, this function will return
 452   * true when viewing that page.
 453   *
 454   * Otherwise the same as {@see is_home()}.
 455   *
 456   * For more information on this and similar theme functions, check out
 457   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 458   * Conditional Tags} article in the Theme Developer Handbook.
 459   *
 460   * @since 2.5.0
 461   *
 462   * @global WP_Query $wp_query WordPress Query object.
 463   *
 464   * @return bool Whether the query is for the front page of the site.
 465   */
 466  function is_front_page() {
 467      global $wp_query;
 468  
 469      if ( ! isset( $wp_query ) ) {
 470          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 471          return false;
 472      }
 473  
 474      return $wp_query->is_front_page();
 475  }
 476  
 477  /**
 478   * Determines whether the query is for the blog homepage.
 479   *
 480   * The blog homepage is the page that shows the time-based blog content of the site.
 481   *
 482   * is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front'
 483   * and 'page_for_posts'.
 484   *
 485   * If a static page is set for the front page of the site, this function will return true only
 486   * on the page you set as the "Posts page".
 487   *
 488   * For more information on this and similar theme functions, check out
 489   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 490   * Conditional Tags} article in the Theme Developer Handbook.
 491   *
 492   * @since 1.5.0
 493   *
 494   * @see is_front_page()
 495   * @global WP_Query $wp_query WordPress Query object.
 496   *
 497   * @return bool Whether the query is for the blog homepage.
 498   */
 499  function is_home() {
 500      global $wp_query;
 501  
 502      if ( ! isset( $wp_query ) ) {
 503          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 504          return false;
 505      }
 506  
 507      return $wp_query->is_home();
 508  }
 509  
 510  /**
 511   * Determines whether the query is for the Privacy Policy page.
 512   *
 513   * The Privacy Policy page is the page that shows the Privacy Policy content of the site.
 514   *
 515   * is_privacy_policy() is dependent on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'.
 516   *
 517   * This function will return true only on the page you set as the "Privacy Policy page".
 518   *
 519   * For more information on this and similar theme functions, check out
 520   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 521   * Conditional Tags} article in the Theme Developer Handbook.
 522   *
 523   * @since 5.2.0
 524   *
 525   * @global WP_Query $wp_query WordPress Query object.
 526   *
 527   * @return bool Whether the query is for the Privacy Policy page.
 528   */
 529  function is_privacy_policy() {
 530      global $wp_query;
 531  
 532      if ( ! isset( $wp_query ) ) {
 533          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 534          return false;
 535      }
 536  
 537      return $wp_query->is_privacy_policy();
 538  }
 539  
 540  /**
 541   * Determines whether the query is for an existing month archive.
 542   *
 543   * For more information on this and similar theme functions, check out
 544   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 545   * Conditional Tags} article in the Theme Developer Handbook.
 546   *
 547   * @since 1.5.0
 548   *
 549   * @global WP_Query $wp_query WordPress Query object.
 550   *
 551   * @return bool Whether the query is for an existing month archive.
 552   */
 553  function is_month() {
 554      global $wp_query;
 555  
 556      if ( ! isset( $wp_query ) ) {
 557          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 558          return false;
 559      }
 560  
 561      return $wp_query->is_month();
 562  }
 563  
 564  /**
 565   * Determines whether the query is for an existing single page.
 566   *
 567   * If the $page parameter is specified, this function will additionally
 568   * check if the query is for one of the pages specified.
 569   *
 570   * For more information on this and similar theme functions, check out
 571   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 572   * Conditional Tags} article in the Theme Developer Handbook.
 573   *
 574   * @since 1.5.0
 575   *
 576   * @see is_single()
 577   * @see is_singular()
 578   * @global WP_Query $wp_query WordPress Query object.
 579   *
 580   * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such
 581   *                                        to check against. Default empty.
 582   * @return bool Whether the query is for an existing single page.
 583   */
 584  function is_page( $page = '' ) {
 585      global $wp_query;
 586  
 587      if ( ! isset( $wp_query ) ) {
 588          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 589          return false;
 590      }
 591  
 592      return $wp_query->is_page( $page );
 593  }
 594  
 595  /**
 596   * Determines whether the query is for a paged result and not for the first page.
 597   *
 598   * For more information on this and similar theme functions, check out
 599   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 600   * Conditional Tags} article in the Theme Developer Handbook.
 601   *
 602   * @since 1.5.0
 603   *
 604   * @global WP_Query $wp_query WordPress Query object.
 605   *
 606   * @return bool Whether the query is for a paged result.
 607   */
 608  function is_paged() {
 609      global $wp_query;
 610  
 611      if ( ! isset( $wp_query ) ) {
 612          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 613          return false;
 614      }
 615  
 616      return $wp_query->is_paged();
 617  }
 618  
 619  /**
 620   * Determines whether the query is for a post or page preview.
 621   *
 622   * For more information on this and similar theme functions, check out
 623   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 624   * Conditional Tags} article in the Theme Developer Handbook.
 625   *
 626   * @since 2.0.0
 627   *
 628   * @global WP_Query $wp_query WordPress Query object.
 629   *
 630   * @return bool Whether the query is for a post or page preview.
 631   */
 632  function is_preview() {
 633      global $wp_query;
 634  
 635      if ( ! isset( $wp_query ) ) {
 636          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 637          return false;
 638      }
 639  
 640      return $wp_query->is_preview();
 641  }
 642  
 643  /**
 644   * Is the query for the robots.txt file?
 645   *
 646   * @since 2.1.0
 647   *
 648   * @global WP_Query $wp_query WordPress Query object.
 649   *
 650   * @return bool Whether the query is for the robots.txt file.
 651   */
 652  function is_robots() {
 653      global $wp_query;
 654  
 655      if ( ! isset( $wp_query ) ) {
 656          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 657          return false;
 658      }
 659  
 660      return $wp_query->is_robots();
 661  }
 662  
 663  /**
 664   * Is the query for the favicon.ico file?
 665   *
 666   * @since 5.4.0
 667   *
 668   * @global WP_Query $wp_query WordPress Query object.
 669   *
 670   * @return bool Whether the query is for the favicon.ico file.
 671   */
 672  function is_favicon() {
 673      global $wp_query;
 674  
 675      if ( ! isset( $wp_query ) ) {
 676          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 677          return false;
 678      }
 679  
 680      return $wp_query->is_favicon();
 681  }
 682  
 683  /**
 684   * Is the query for a sitemap?
 685   *
 686   * @since 7.1.0
 687   *
 688   * @global WP_Query $wp_query WordPress Query object.
 689   *
 690   * @return bool Whether the query is for a sitemap.
 691   */
 692  function is_sitemap(): bool {
 693      global $wp_query;
 694  
 695      if ( ! isset( $wp_query ) ) {
 696          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.1.0' );
 697          return false;
 698      }
 699  
 700      return $wp_query->is_sitemap();
 701  }
 702  
 703  /**
 704   * Determines whether the query is for a search.
 705   *
 706   * For more information on this and similar theme functions, check out
 707   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 708   * Conditional Tags} article in the Theme Developer Handbook.
 709   *
 710   * @since 1.5.0
 711   *
 712   * @global WP_Query $wp_query WordPress Query object.
 713   *
 714   * @return bool Whether the query is for a search.
 715   */
 716  function is_search() {
 717      global $wp_query;
 718  
 719      if ( ! isset( $wp_query ) ) {
 720          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 721          return false;
 722      }
 723  
 724      return $wp_query->is_search();
 725  }
 726  
 727  /**
 728   * Determines whether the query is for an existing single post.
 729   *
 730   * Works for any post type, except attachments and pages
 731   *
 732   * If the $post parameter is specified, this function will additionally
 733   * check if the query is for one of the Posts specified.
 734   *
 735   * For more information on this and similar theme functions, check out
 736   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 737   * Conditional Tags} article in the Theme Developer Handbook.
 738   *
 739   * @since 1.5.0
 740   *
 741   * @see is_page()
 742   * @see is_singular()
 743   * @global WP_Query $wp_query WordPress Query object.
 744   *
 745   * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
 746   *                                        to check against. Default empty.
 747   * @return bool Whether the query is for an existing single post.
 748   */
 749  function is_single( $post = '' ) {
 750      global $wp_query;
 751  
 752      if ( ! isset( $wp_query ) ) {
 753          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 754          return false;
 755      }
 756  
 757      return $wp_query->is_single( $post );
 758  }
 759  
 760  /**
 761   * Determines whether the query is for an existing single post of any post type
 762   * (post, attachment, page, custom post types).
 763   *
 764   * If the $post_types parameter is specified, this function will additionally
 765   * check if the query is for one of the Posts Types specified.
 766   *
 767   * For more information on this and similar theme functions, check out
 768   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 769   * Conditional Tags} article in the Theme Developer Handbook.
 770   *
 771   * @since 1.5.0
 772   *
 773   * @see is_page()
 774   * @see is_single()
 775   * @global WP_Query $wp_query WordPress Query object.
 776   *
 777   * @param string|string[] $post_types Optional. Post type or array of post types
 778   *                                    to check against. Default empty.
 779   * @return bool Whether the query is for an existing single post
 780   *              or any of the given post types.
 781   */
 782  function is_singular( $post_types = '' ) {
 783      global $wp_query;
 784  
 785      if ( ! isset( $wp_query ) ) {
 786          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 787          return false;
 788      }
 789  
 790      return $wp_query->is_singular( $post_types );
 791  }
 792  
 793  /**
 794   * Determines whether the query is for a specific time.
 795   *
 796   * For more information on this and similar theme functions, check out
 797   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 798   * Conditional Tags} article in the Theme Developer Handbook.
 799   *
 800   * @since 1.5.0
 801   *
 802   * @global WP_Query $wp_query WordPress Query object.
 803   *
 804   * @return bool Whether the query is for a specific time.
 805   */
 806  function is_time() {
 807      global $wp_query;
 808  
 809      if ( ! isset( $wp_query ) ) {
 810          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 811          return false;
 812      }
 813  
 814      return $wp_query->is_time();
 815  }
 816  
 817  /**
 818   * Determines whether the query is for a trackback endpoint call.
 819   *
 820   * For more information on this and similar theme functions, check out
 821   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 822   * Conditional Tags} article in the Theme Developer Handbook.
 823   *
 824   * @since 1.5.0
 825   *
 826   * @global WP_Query $wp_query WordPress Query object.
 827   *
 828   * @return bool Whether the query is for a trackback endpoint call.
 829   */
 830  function is_trackback() {
 831      global $wp_query;
 832  
 833      if ( ! isset( $wp_query ) ) {
 834          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 835          return false;
 836      }
 837  
 838      return $wp_query->is_trackback();
 839  }
 840  
 841  /**
 842   * Determines whether the query is for an existing year archive.
 843   *
 844   * For more information on this and similar theme functions, check out
 845   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 846   * Conditional Tags} article in the Theme Developer Handbook.
 847   *
 848   * @since 1.5.0
 849   *
 850   * @global WP_Query $wp_query WordPress Query object.
 851   *
 852   * @return bool Whether the query is for an existing year archive.
 853   */
 854  function is_year() {
 855      global $wp_query;
 856  
 857      if ( ! isset( $wp_query ) ) {
 858          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 859          return false;
 860      }
 861  
 862      return $wp_query->is_year();
 863  }
 864  
 865  /**
 866   * Determines whether the query has resulted in a 404 (returns no results).
 867   *
 868   * For more information on this and similar theme functions, check out
 869   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 870   * Conditional Tags} article in the Theme Developer Handbook.
 871   *
 872   * @since 1.5.0
 873   *
 874   * @global WP_Query $wp_query WordPress Query object.
 875   *
 876   * @return bool Whether the query is a 404 error.
 877   */
 878  function is_404() {
 879      global $wp_query;
 880  
 881      if ( ! isset( $wp_query ) ) {
 882          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 883          return false;
 884      }
 885  
 886      return $wp_query->is_404();
 887  }
 888  
 889  /**
 890   * Is the query for an embedded post?
 891   *
 892   * @since 4.4.0
 893   *
 894   * @global WP_Query $wp_query WordPress Query object.
 895   *
 896   * @return bool Whether the query is for an embedded post.
 897   */
 898  function is_embed() {
 899      global $wp_query;
 900  
 901      if ( ! isset( $wp_query ) ) {
 902          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
 903          return false;
 904      }
 905  
 906      return $wp_query->is_embed();
 907  }
 908  
 909  /**
 910   * Determines whether the query is the main query.
 911   *
 912   * For more information on this and similar theme functions, check out
 913   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 914   * Conditional Tags} article in the Theme Developer Handbook.
 915   *
 916   * @since 3.3.0
 917   *
 918   * @global WP_Query $wp_query WordPress Query object.
 919   *
 920   * @return bool Whether the query is the main query.
 921   */
 922  function is_main_query() {
 923      global $wp_query;
 924  
 925      if ( ! isset( $wp_query ) ) {
 926          _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' );
 927          return false;
 928      }
 929  
 930      if ( 'pre_get_posts' === current_filter() ) {
 931          _doing_it_wrong(
 932              __FUNCTION__,
 933              sprintf(
 934                  /* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */
 935                  __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
 936                  '<code>pre_get_posts</code>',
 937                  '<code>WP_Query->is_main_query()</code>',
 938                  '<code>is_main_query()</code>',
 939                  __( 'https://developer.wordpress.org/reference/functions/is_main_query/' )
 940              ),
 941              '3.7.0'
 942          );
 943      }
 944  
 945      return $wp_query->is_main_query();
 946  }
 947  
 948  /*
 949   * The Loop. Post loop control.
 950   */
 951  
 952  /**
 953   * Determines whether current WordPress query has posts to loop over.
 954   *
 955   * @since 1.5.0
 956   *
 957   * @global WP_Query $wp_query WordPress Query object.
 958   *
 959   * @phpstan-impure
 960   *
 961   * @return bool True if posts are available, false if end of the loop.
 962   */
 963  function have_posts() {
 964      global $wp_query;
 965  
 966      if ( ! isset( $wp_query ) ) {
 967          return false;
 968      }
 969  
 970      return $wp_query->have_posts();
 971  }
 972  
 973  /**
 974   * Determines whether the caller is in the Loop.
 975   *
 976   * For more information on this and similar theme functions, check out
 977   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
 978   * Conditional Tags} article in the Theme Developer Handbook.
 979   *
 980   * @since 2.0.0
 981   *
 982   * @global WP_Query $wp_query WordPress Query object.
 983   *
 984   * @return bool True if caller is within loop, false if loop hasn't started or ended.
 985   */
 986  function in_the_loop() {
 987      global $wp_query;
 988  
 989      if ( ! isset( $wp_query ) ) {
 990          return false;
 991      }
 992  
 993      return $wp_query->in_the_loop;
 994  }
 995  
 996  /**
 997   * Rewind the loop posts.
 998   *
 999   * @since 1.5.0
1000   *
1001   * @global WP_Query $wp_query WordPress Query object.
1002   */
1003  function rewind_posts() {
1004      global $wp_query;
1005  
1006      if ( ! isset( $wp_query ) ) {
1007          return;
1008      }
1009  
1010      $wp_query->rewind_posts();
1011  }
1012  
1013  /**
1014   * Iterate the post index in the loop.
1015   *
1016   * @since 1.5.0
1017   *
1018   * @global WP_Query $wp_query WordPress Query object.
1019   */
1020  function the_post() {
1021      global $wp_query;
1022  
1023      if ( ! isset( $wp_query ) ) {
1024          return;
1025      }
1026  
1027      $wp_query->the_post();
1028  }
1029  
1030  /*
1031   * Comments loop.
1032   */
1033  
1034  /**
1035   * Determines whether current WordPress query has comments to loop over.
1036   *
1037   * @since 2.2.0
1038   *
1039   * @global WP_Query $wp_query WordPress Query object.
1040   *
1041   * @phpstan-impure
1042   *
1043   * @return bool True if comments are available, false if no more comments.
1044   */
1045  function have_comments() {
1046      global $wp_query;
1047  
1048      if ( ! isset( $wp_query ) ) {
1049          return false;
1050      }
1051  
1052      return $wp_query->have_comments();
1053  }
1054  
1055  /**
1056   * Iterate comment index in the comment loop.
1057   *
1058   * @since 2.2.0
1059   *
1060   * @global WP_Query $wp_query WordPress Query object.
1061   */
1062  function the_comment() {
1063      global $wp_query;
1064  
1065      if ( ! isset( $wp_query ) ) {
1066          return;
1067      }
1068  
1069      $wp_query->the_comment();
1070  }
1071  
1072  /**
1073   * Redirect old slugs to the correct permalink.
1074   *
1075   * Attempts to find the current slug from the past slugs.
1076   *
1077   * @since 2.1.0
1078   */
1079  function wp_old_slug_redirect() {
1080      if ( is_404() && '' !== get_query_var( 'name' ) ) {
1081          // Guess the current post type based on the query vars.
1082          if ( get_query_var( 'post_type' ) ) {
1083              $post_type = get_query_var( 'post_type' );
1084          } elseif ( get_query_var( 'attachment' ) ) {
1085              $post_type = 'attachment';
1086          } elseif ( get_query_var( 'pagename' ) ) {
1087              $post_type = 'page';
1088          } else {
1089              $post_type = 'post';
1090          }
1091  
1092          if ( is_array( $post_type ) ) {
1093              if ( count( $post_type ) > 1 ) {
1094                  return;
1095              }
1096              $post_type = reset( $post_type );
1097          }
1098  
1099          // Do not attempt redirect for hierarchical post types.
1100          if ( is_post_type_hierarchical( $post_type ) ) {
1101              return;
1102          }
1103  
1104          $id = _find_post_by_old_slug( $post_type );
1105  
1106          if ( ! $id ) {
1107              $id = _find_post_by_old_date( $post_type );
1108          }
1109  
1110          /**
1111           * Filters the old slug redirect post ID.
1112           *
1113           * @since 4.9.3
1114           *
1115           * @param int $id The redirect post ID.
1116           */
1117          $id = apply_filters( 'old_slug_redirect_post_id', $id );
1118  
1119          if ( ! $id ) {
1120              return;
1121          }
1122  
1123          $link = get_permalink( $id );
1124  
1125          if ( get_query_var( 'paged' ) > 1 ) {
1126              $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
1127          } elseif ( is_embed() ) {
1128              $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
1129          }
1130  
1131          /**
1132           * Filters the old slug redirect URL.
1133           *
1134           * @since 4.4.0
1135           *
1136           * @param string $link The redirect URL.
1137           */
1138          $link = apply_filters( 'old_slug_redirect_url', $link );
1139  
1140          if ( ! $link ) {
1141              return;
1142          }
1143  
1144          wp_redirect( $link, 301 ); // Permanent redirect.
1145          exit;
1146      }
1147  }
1148  
1149  /**
1150   * Find the post ID for redirecting an old slug.
1151   *
1152   * @since 4.9.3
1153   * @access private
1154   *
1155   * @see wp_old_slug_redirect()
1156   * @global wpdb $wpdb WordPress database abstraction object.
1157   *
1158   * @param string $post_type The current post type based on the query vars.
1159   * @return int The Post ID.
1160   */
1161  function _find_post_by_old_slug( $post_type ) {
1162      global $wpdb;
1163  
1164      $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
1165  
1166      /*
1167       * If year, monthnum, or day have been specified, make our query more precise
1168       * just in case there are multiple identical _wp_old_slug values.
1169       */
1170      if ( get_query_var( 'year' ) ) {
1171          $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
1172      }
1173      if ( get_query_var( 'monthnum' ) ) {
1174          $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
1175      }
1176      if ( get_query_var( 'day' ) ) {
1177          $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
1178      }
1179  
1180      $key          = md5( $query );
1181      $last_changed = wp_cache_get_last_changed( 'posts' );
1182      $cache_key    = "find_post_by_old_slug:$key";
1183      $cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
1184      if ( false !== $cache ) {
1185          $id = $cache;
1186      } else {
1187          $id = (int) $wpdb->get_var( $query );
1188          wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed );
1189      }
1190  
1191      return $id;
1192  }
1193  
1194  /**
1195   * Find the post ID for redirecting an old date.
1196   *
1197   * @since 4.9.3
1198   * @access private
1199   *
1200   * @see wp_old_slug_redirect()
1201   * @global wpdb $wpdb WordPress database abstraction object.
1202   *
1203   * @param string $post_type The current post type based on the query vars.
1204   * @return int The Post ID.
1205   */
1206  function _find_post_by_old_date( $post_type ) {
1207      global $wpdb;
1208  
1209      $date_query = '';
1210      if ( get_query_var( 'year' ) ) {
1211          $date_query .= $wpdb->prepare( ' AND YEAR(pm_date.meta_value) = %d', get_query_var( 'year' ) );
1212      }
1213      if ( get_query_var( 'monthnum' ) ) {
1214          $date_query .= $wpdb->prepare( ' AND MONTH(pm_date.meta_value) = %d', get_query_var( 'monthnum' ) );
1215      }
1216      if ( get_query_var( 'day' ) ) {
1217          $date_query .= $wpdb->prepare( ' AND DAYOFMONTH(pm_date.meta_value) = %d', get_query_var( 'day' ) );
1218      }
1219  
1220      $id = 0;
1221      if ( $date_query ) {
1222          $query        = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) );
1223          $key          = md5( $query );
1224          $last_changed = wp_cache_get_last_changed( 'posts' );
1225          $cache_key    = "find_post_by_old_date:$key";
1226          $cache        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
1227          if ( false !== $cache ) {
1228              $id = $cache;
1229          } else {
1230              $id = (int) $wpdb->get_var( $query );
1231              if ( ! $id ) {
1232                  // Check to see if an old slug matches the old date.
1233                  $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
1234              }
1235              wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed );
1236          }
1237      }
1238  
1239      return $id;
1240  }
1241  
1242  /**
1243   * Set up global post data.
1244   *
1245   * @since 1.5.0
1246   * @since 4.4.0 Added the ability to pass a post ID to `$post`.
1247   *
1248   * @global WP_Query $wp_query WordPress Query object.
1249   *
1250   * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
1251   * @return bool True when finished.
1252   */
1253  function setup_postdata( $post ) {
1254      global $wp_query;
1255  
1256      if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
1257          return $wp_query->setup_postdata( $post );
1258      }
1259  
1260      return false;
1261  }
1262  
1263  /**
1264   * Generates post data.
1265   *
1266   * @since 5.2.0
1267   *
1268   * @global WP_Query $wp_query WordPress Query object.
1269   *
1270   * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
1271   * @return array|false Elements of post, or false on failure.
1272   */
1273  function generate_postdata( $post ) {
1274      global $wp_query;
1275  
1276      if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
1277          return $wp_query->generate_postdata( $post );
1278      }
1279  
1280      return false;
1281  }


Generated : Sat Sep 5 08:20:28 2026 Cross-referenced by PHPXref