| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 random content. 705 * 706 * True for a blog home or archive request that includes the `random` query variable, 707 * for example `example.com/random/` or `example.com/random/category/news/`. Search, 708 * feed, trackback, robots.txt, favicon and sitemap requests are never for random content. 709 * 710 * @since 7.2.0 711 * 712 * @see wp_is_random_content_query() 713 * @global WP_Query $wp_query WordPress Query object. 714 * 715 * @return bool Whether the query is for random content. 716 */ 717 function is_random(): bool { 718 global $wp_query; 719 720 if ( ! isset( $wp_query ) ) { 721 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.2.0' ); 722 return false; 723 } 724 725 return $wp_query->is_random(); 726 } 727 728 /** 729 * Determines whether the query is for a search. 730 * 731 * For more information on this and similar theme functions, check out 732 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 733 * Conditional Tags} article in the Theme Developer Handbook. 734 * 735 * @since 1.5.0 736 * 737 * @global WP_Query $wp_query WordPress Query object. 738 * 739 * @return bool Whether the query is for a search. 740 */ 741 function is_search() { 742 global $wp_query; 743 744 if ( ! isset( $wp_query ) ) { 745 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 746 return false; 747 } 748 749 return $wp_query->is_search(); 750 } 751 752 /** 753 * Determines whether the query is for an existing single post. 754 * 755 * Works for any post type, except attachments and pages 756 * 757 * If the $post parameter is specified, this function will additionally 758 * check if the query is for one of the Posts specified. 759 * 760 * For more information on this and similar theme functions, check out 761 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 762 * Conditional Tags} article in the Theme Developer Handbook. 763 * 764 * @since 1.5.0 765 * 766 * @see is_page() 767 * @see is_singular() 768 * @global WP_Query $wp_query WordPress Query object. 769 * 770 * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such 771 * to check against. Default empty. 772 * @return bool Whether the query is for an existing single post. 773 */ 774 function is_single( $post = '' ) { 775 global $wp_query; 776 777 if ( ! isset( $wp_query ) ) { 778 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 779 return false; 780 } 781 782 return $wp_query->is_single( $post ); 783 } 784 785 /** 786 * Determines whether the query is for an existing single post of any post type 787 * (post, attachment, page, custom post types). 788 * 789 * If the $post_types parameter is specified, this function will additionally 790 * check if the query is for one of the Posts Types specified. 791 * 792 * For more information on this and similar theme functions, check out 793 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 794 * Conditional Tags} article in the Theme Developer Handbook. 795 * 796 * @since 1.5.0 797 * 798 * @see is_page() 799 * @see is_single() 800 * @global WP_Query $wp_query WordPress Query object. 801 * 802 * @param string|string[] $post_types Optional. Post type or array of post types 803 * to check against. Default empty. 804 * @return bool Whether the query is for an existing single post 805 * or any of the given post types. 806 */ 807 function is_singular( $post_types = '' ) { 808 global $wp_query; 809 810 if ( ! isset( $wp_query ) ) { 811 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 812 return false; 813 } 814 815 return $wp_query->is_singular( $post_types ); 816 } 817 818 /** 819 * Determines whether the query is for a specific time. 820 * 821 * For more information on this and similar theme functions, check out 822 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 823 * Conditional Tags} article in the Theme Developer Handbook. 824 * 825 * @since 1.5.0 826 * 827 * @global WP_Query $wp_query WordPress Query object. 828 * 829 * @return bool Whether the query is for a specific time. 830 */ 831 function is_time() { 832 global $wp_query; 833 834 if ( ! isset( $wp_query ) ) { 835 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 836 return false; 837 } 838 839 return $wp_query->is_time(); 840 } 841 842 /** 843 * Determines whether the query is for a trackback endpoint call. 844 * 845 * For more information on this and similar theme functions, check out 846 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 847 * Conditional Tags} article in the Theme Developer Handbook. 848 * 849 * @since 1.5.0 850 * 851 * @global WP_Query $wp_query WordPress Query object. 852 * 853 * @return bool Whether the query is for a trackback endpoint call. 854 */ 855 function is_trackback() { 856 global $wp_query; 857 858 if ( ! isset( $wp_query ) ) { 859 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 860 return false; 861 } 862 863 return $wp_query->is_trackback(); 864 } 865 866 /** 867 * Determines whether the query is for an existing year archive. 868 * 869 * For more information on this and similar theme functions, check out 870 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 871 * Conditional Tags} article in the Theme Developer Handbook. 872 * 873 * @since 1.5.0 874 * 875 * @global WP_Query $wp_query WordPress Query object. 876 * 877 * @return bool Whether the query is for an existing year archive. 878 */ 879 function is_year() { 880 global $wp_query; 881 882 if ( ! isset( $wp_query ) ) { 883 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 884 return false; 885 } 886 887 return $wp_query->is_year(); 888 } 889 890 /** 891 * Determines whether the query has resulted in a 404 (returns no results). 892 * 893 * For more information on this and similar theme functions, check out 894 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 895 * Conditional Tags} article in the Theme Developer Handbook. 896 * 897 * @since 1.5.0 898 * 899 * @global WP_Query $wp_query WordPress Query object. 900 * 901 * @return bool Whether the query is a 404 error. 902 */ 903 function is_404() { 904 global $wp_query; 905 906 if ( ! isset( $wp_query ) ) { 907 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 908 return false; 909 } 910 911 return $wp_query->is_404(); 912 } 913 914 /** 915 * Is the query for an embedded post? 916 * 917 * @since 4.4.0 918 * 919 * @global WP_Query $wp_query WordPress Query object. 920 * 921 * @return bool Whether the query is for an embedded post. 922 */ 923 function is_embed() { 924 global $wp_query; 925 926 if ( ! isset( $wp_query ) ) { 927 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' ); 928 return false; 929 } 930 931 return $wp_query->is_embed(); 932 } 933 934 /** 935 * Determines whether the query is the main query. 936 * 937 * For more information on this and similar theme functions, check out 938 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 939 * Conditional Tags} article in the Theme Developer Handbook. 940 * 941 * @since 3.3.0 942 * 943 * @global WP_Query $wp_query WordPress Query object. 944 * 945 * @return bool Whether the query is the main query. 946 */ 947 function is_main_query() { 948 global $wp_query; 949 950 if ( ! isset( $wp_query ) ) { 951 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' ); 952 return false; 953 } 954 955 if ( 'pre_get_posts' === current_filter() ) { 956 _doing_it_wrong( 957 __FUNCTION__, 958 sprintf( 959 /* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */ 960 __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ), 961 '<code>pre_get_posts</code>', 962 '<code>WP_Query->is_main_query()</code>', 963 '<code>is_main_query()</code>', 964 __( 'https://developer.wordpress.org/reference/functions/is_main_query/' ) 965 ), 966 '3.7.0' 967 ); 968 } 969 970 return $wp_query->is_main_query(); 971 } 972 973 /* 974 * The Loop. Post loop control. 975 */ 976 977 /** 978 * Determines whether current WordPress query has posts to loop over. 979 * 980 * @since 1.5.0 981 * 982 * @global WP_Query $wp_query WordPress Query object. 983 * 984 * @phpstan-impure 985 * 986 * @return bool True if posts are available, false if end of the loop. 987 */ 988 function have_posts() { 989 global $wp_query; 990 991 if ( ! isset( $wp_query ) ) { 992 return false; 993 } 994 995 return $wp_query->have_posts(); 996 } 997 998 /** 999 * Determines whether the caller is in the Loop. 1000 * 1001 * For more information on this and similar theme functions, check out 1002 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 1003 * Conditional Tags} article in the Theme Developer Handbook. 1004 * 1005 * @since 2.0.0 1006 * 1007 * @global WP_Query $wp_query WordPress Query object. 1008 * 1009 * @return bool True if caller is within loop, false if loop hasn't started or ended. 1010 */ 1011 function in_the_loop() { 1012 global $wp_query; 1013 1014 if ( ! isset( $wp_query ) ) { 1015 return false; 1016 } 1017 1018 return $wp_query->in_the_loop; 1019 } 1020 1021 /** 1022 * Rewind the loop posts. 1023 * 1024 * @since 1.5.0 1025 * 1026 * @global WP_Query $wp_query WordPress Query object. 1027 */ 1028 function rewind_posts() { 1029 global $wp_query; 1030 1031 if ( ! isset( $wp_query ) ) { 1032 return; 1033 } 1034 1035 $wp_query->rewind_posts(); 1036 } 1037 1038 /** 1039 * Iterate the post index in the loop. 1040 * 1041 * @since 1.5.0 1042 * 1043 * @global WP_Query $wp_query WordPress Query object. 1044 */ 1045 function the_post() { 1046 global $wp_query; 1047 1048 if ( ! isset( $wp_query ) ) { 1049 return; 1050 } 1051 1052 $wp_query->the_post(); 1053 } 1054 1055 /* 1056 * Comments loop. 1057 */ 1058 1059 /** 1060 * Determines whether current WordPress query has comments to loop over. 1061 * 1062 * @since 2.2.0 1063 * 1064 * @global WP_Query $wp_query WordPress Query object. 1065 * 1066 * @phpstan-impure 1067 * 1068 * @return bool True if comments are available, false if no more comments. 1069 */ 1070 function have_comments() { 1071 global $wp_query; 1072 1073 if ( ! isset( $wp_query ) ) { 1074 return false; 1075 } 1076 1077 return $wp_query->have_comments(); 1078 } 1079 1080 /** 1081 * Iterate comment index in the comment loop. 1082 * 1083 * @since 2.2.0 1084 * 1085 * @global WP_Query $wp_query WordPress Query object. 1086 */ 1087 function the_comment() { 1088 global $wp_query; 1089 1090 if ( ! isset( $wp_query ) ) { 1091 return; 1092 } 1093 1094 $wp_query->the_comment(); 1095 } 1096 1097 /** 1098 * Redirect old slugs to the correct permalink. 1099 * 1100 * Attempts to find the current slug from the past slugs. 1101 * 1102 * @since 2.1.0 1103 */ 1104 function wp_old_slug_redirect() { 1105 if ( is_404() && '' !== get_query_var( 'name' ) ) { 1106 // Guess the current post type based on the query vars. 1107 if ( get_query_var( 'post_type' ) ) { 1108 $post_type = get_query_var( 'post_type' ); 1109 } elseif ( get_query_var( 'attachment' ) ) { 1110 $post_type = 'attachment'; 1111 } elseif ( get_query_var( 'pagename' ) ) { 1112 $post_type = 'page'; 1113 } else { 1114 $post_type = 'post'; 1115 } 1116 1117 if ( is_array( $post_type ) ) { 1118 if ( count( $post_type ) > 1 ) { 1119 return; 1120 } 1121 $post_type = reset( $post_type ); 1122 } 1123 1124 // Do not attempt redirect for hierarchical post types. 1125 if ( is_post_type_hierarchical( $post_type ) ) { 1126 return; 1127 } 1128 1129 $id = _find_post_by_old_slug( $post_type ); 1130 1131 if ( ! $id ) { 1132 $id = _find_post_by_old_date( $post_type ); 1133 } 1134 1135 /** 1136 * Filters the old slug redirect post ID. 1137 * 1138 * @since 4.9.3 1139 * 1140 * @param int $id The redirect post ID. 1141 */ 1142 $id = apply_filters( 'old_slug_redirect_post_id', $id ); 1143 1144 if ( ! $id ) { 1145 return; 1146 } 1147 1148 $link = get_permalink( $id ); 1149 1150 if ( get_query_var( 'paged' ) > 1 ) { 1151 $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) ); 1152 } elseif ( is_embed() ) { 1153 $link = user_trailingslashit( trailingslashit( $link ) . 'embed' ); 1154 } 1155 1156 /** 1157 * Filters the old slug redirect URL. 1158 * 1159 * @since 4.4.0 1160 * 1161 * @param string $link The redirect URL. 1162 */ 1163 $link = apply_filters( 'old_slug_redirect_url', $link ); 1164 1165 if ( ! $link ) { 1166 return; 1167 } 1168 1169 wp_redirect( $link, 301 ); // Permanent redirect. 1170 exit; 1171 } 1172 } 1173 1174 /** 1175 * Find the post ID for redirecting an old slug. 1176 * 1177 * @since 4.9.3 1178 * @access private 1179 * 1180 * @see wp_old_slug_redirect() 1181 * @global wpdb $wpdb WordPress database abstraction object. 1182 * 1183 * @param string $post_type The current post type based on the query vars. 1184 * @return int The Post ID. 1185 */ 1186 function _find_post_by_old_slug( $post_type ) { 1187 global $wpdb; 1188 1189 $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' ) ); 1190 1191 /* 1192 * If year, monthnum, or day have been specified, make our query more precise 1193 * just in case there are multiple identical _wp_old_slug values. 1194 */ 1195 if ( get_query_var( 'year' ) ) { 1196 $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); 1197 } 1198 if ( get_query_var( 'monthnum' ) ) { 1199 $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); 1200 } 1201 if ( get_query_var( 'day' ) ) { 1202 $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); 1203 } 1204 1205 $key = md5( $query ); 1206 $last_changed = wp_cache_get_last_changed( 'posts' ); 1207 $cache_key = "find_post_by_old_slug:$key"; 1208 $cache = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); 1209 if ( false !== $cache ) { 1210 $id = $cache; 1211 } else { 1212 $id = (int) $wpdb->get_var( $query ); 1213 wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed ); 1214 } 1215 1216 return $id; 1217 } 1218 1219 /** 1220 * Find the post ID for redirecting an old date. 1221 * 1222 * @since 4.9.3 1223 * @access private 1224 * 1225 * @see wp_old_slug_redirect() 1226 * @global wpdb $wpdb WordPress database abstraction object. 1227 * 1228 * @param string $post_type The current post type based on the query vars. 1229 * @return int The Post ID. 1230 */ 1231 function _find_post_by_old_date( $post_type ) { 1232 global $wpdb; 1233 1234 $date_query = ''; 1235 if ( get_query_var( 'year' ) ) { 1236 $date_query .= $wpdb->prepare( ' AND YEAR(pm_date.meta_value) = %d', get_query_var( 'year' ) ); 1237 } 1238 if ( get_query_var( 'monthnum' ) ) { 1239 $date_query .= $wpdb->prepare( ' AND MONTH(pm_date.meta_value) = %d', get_query_var( 'monthnum' ) ); 1240 } 1241 if ( get_query_var( 'day' ) ) { 1242 $date_query .= $wpdb->prepare( ' AND DAYOFMONTH(pm_date.meta_value) = %d', get_query_var( 'day' ) ); 1243 } 1244 1245 $id = 0; 1246 if ( $date_query ) { 1247 $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' ) ); 1248 $key = md5( $query ); 1249 $last_changed = wp_cache_get_last_changed( 'posts' ); 1250 $cache_key = "find_post_by_old_date:$key"; 1251 $cache = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); 1252 if ( false !== $cache ) { 1253 $id = $cache; 1254 } else { 1255 $id = (int) $wpdb->get_var( $query ); 1256 if ( ! $id ) { 1257 // Check to see if an old slug matches the old date. 1258 $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' ) ) ); 1259 } 1260 wp_cache_set_salted( $cache_key, $id, 'post-queries', $last_changed ); 1261 } 1262 } 1263 1264 return $id; 1265 } 1266 1267 /** 1268 * Determines whether random content redirects are enabled. 1269 * 1270 * @since 7.2.0 1271 * 1272 * @return bool True if random content redirects are enabled, false otherwise. 1273 */ 1274 function wp_is_random_content_redirect_enabled(): bool { 1275 /** 1276 * Filters whether random content redirects are enabled. 1277 * 1278 * Random content redirects add rewrite rules for URLs such as `/random/`, so the 1279 * rewrite rules need to be flushed after changing the value returned by this filter. 1280 * 1281 * @since 7.2.0 1282 * 1283 * @param bool $enabled Whether random content redirects are enabled. Default true. 1284 */ 1285 return (bool) apply_filters( 'wp_enable_random_content_redirect', true ); 1286 } 1287 1288 /** 1289 * Determines whether a query is a request for random content. 1290 * 1291 * A random content request is a front end, main query for the blog home or an archive 1292 * that includes the `random` query variable and at least one `randomable` post type. 1293 * For example `example.com/random/` or `example.com/random/category/news/`. 1294 * 1295 * Singular (including embeds), search, feed, trackback, robots.txt, favicon and sitemap 1296 * requests are never random content requests, nor are requests using an HTTP method 1297 * other than GET or HEAD. Search 1298 * requests are excluded as selecting a random post from search results would run an 1299 * uncacheable search query that orders every matching post randomly. Search requests 1300 * that include the `random` query variable are redirected to the search results 1301 * by {@see WP::handle_random()}. 1302 * 1303 * @since 7.2.0 1304 * 1305 * @param WP_Query $query The query to check. 1306 * @return bool True if the query is a request for random content, false otherwise. 1307 */ 1308 function wp_is_random_content_query( WP_Query $query ): bool { 1309 if ( 1310 ! $query->is_random() 1311 || ! $query->is_main_query() 1312 || is_admin() 1313 || ! wp_is_random_content_redirect_enabled() 1314 ) { 1315 return false; 1316 } 1317 1318 $request_method = isset( $_SERVER['REQUEST_METHOD'] ) && is_string( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( $_SERVER['REQUEST_METHOD'] ) : ''; 1319 if ( 'GET' !== $request_method && 'HEAD' !== $request_method ) { 1320 return false; 1321 } 1322 1323 return array() !== wp_get_random_content_post_types( $query ); 1324 } 1325 1326 /** 1327 * Retrieves the post types eligible for selection by a random content request. 1328 * 1329 * These are the post types the query would otherwise return, limited to those 1330 * registered as `randomable`. 1331 * 1332 * @since 7.2.0 1333 * 1334 * @see register_post_type() 1335 * 1336 * @param WP_Query $query The query to retrieve the post types for. 1337 * @return string[] Post type names eligible for random selection. 1338 * 1339 * @phpstan-return list<string> 1340 */ 1341 function wp_get_random_content_post_types( WP_Query $query ): array { 1342 $post_types = $query->get( 'post_type' ); 1343 1344 if ( empty( $post_types ) && $query->is_tax() && $query->tax_query instanceof WP_Tax_Query ) { 1345 // Match WP_Query::get_posts(): searchable post types using any of the queried taxonomies. 1346 $taxonomies = array_keys( $query->tax_query->queried_terms ); 1347 $post_types = array(); 1348 1349 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $post_type ) { 1350 $object_taxonomies = 'attachment' === $post_type ? get_taxonomies_for_attachments() : get_object_taxonomies( $post_type ); 1351 if ( array_intersect( $taxonomies, $object_taxonomies ) ) { 1352 $post_types[] = $post_type; 1353 } 1354 } 1355 } elseif ( empty( $post_types ) && ! $query->is_search() ) { 1356 $post_types = array( 'post' ); 1357 } 1358 1359 if ( empty( $post_types ) || 'any' === $post_types ) { 1360 $post_types = get_post_types( array( 'exclude_from_search' => false ) ); 1361 } 1362 1363 $random_post_types = array(); 1364 1365 foreach ( (array) $post_types as $post_type ) { 1366 // Plugins may set invalid values on the `post_type` query variable. 1367 if ( ! is_string( $post_type ) ) { 1368 continue; 1369 } 1370 1371 $post_type_object = get_post_type_object( $post_type ); 1372 1373 if ( $post_type_object && $post_type_object->randomable ) { 1374 $random_post_types[] = $post_type_object->name; 1375 } 1376 } 1377 1378 return $random_post_types; 1379 } 1380 1381 /** 1382 * Limits the main query to posts eligible for random content requests. 1383 * 1384 * The main query is used to select the random post so the request runs the same 1385 * query, and the same filters, as the archive it is randomizing. As such, callbacks 1386 * on the {@see 'pre_get_posts'} action may modify the posts eligible for selection, 1387 * for example to limit the post types or exclude a category: 1388 * 1389 * add_action( 1390 * 'pre_get_posts', 1391 * static function ( WP_Query $query ) { 1392 * if ( wp_is_random_content_query( $query ) ) { 1393 * $query->set( 'category__not_in', array( 1 ) ); 1394 * } 1395 * } 1396 * ); 1397 * 1398 * Only randomable post types and published posts without a password are eligible. 1399 * The number and order of posts are enforced by {@see wp_random_content_post_limits()} and 1400 * {@see wp_random_content_posts_orderby()}, so callbacks modifying them on archives do not 1401 * affect the selection. 1402 * 1403 * The random post IDs the post is selected from are stored in the `random_sample_ids` 1404 * query variable, see {@see wp_get_random_content_sample_ids()}. 1405 * 1406 * Added to the {@see 'pre_get_posts'} action by {@see WP::handle_random()} while the 1407 * random content query runs, so it runs after other callbacks. 1408 * 1409 * @since 7.2.0 1410 * 1411 * @param WP_Query $query The WP_Query instance (passed by reference). 1412 */ 1413 function wp_random_content_pre_get_posts( $query ): void { 1414 if ( ! wp_is_random_content_query( $query ) ) { 1415 return; 1416 } 1417 1418 $query->set( 'post_type', wp_get_random_content_post_types( $query ) ); 1419 $query->set( 'post_status', 'publish' ); 1420 $query->set( 'has_password', false ); 1421 $query->set( 'random_sample_ids', wp_get_random_content_sample_ids() ); 1422 $query->set( 'ignore_sticky_posts', true ); 1423 $query->set( 'no_found_rows', true ); 1424 $query->set( 'update_post_meta_cache', false ); 1425 $query->set( 'update_post_term_cache', false ); 1426 // The order and number of posts are enforced by filters, which a suppressed query skips. 1427 $query->set( 'suppress_filters', false ); 1428 } 1429 1430 /** 1431 * Retrieves random post IDs for the random content query to select a post from. 1432 * 1433 * Sorting posts randomly requires the database to read every eligible post. Instead, 1434 * the random content query looks up 50 random post IDs, each by the primary key, and 1435 * selects one of those that is an eligible post. The cost of the lookup does not 1436 * depend on the number of posts, and as every post ID is equally likely to be looked 1437 * up, every eligible post is equally likely to be selected. This is the case whether 1438 * or not there are gaps between the IDs of posts. 1439 * 1440 * The IDs are between 1 and the highest post ID, which is stored in the object cache 1441 * for 10 minutes. If there are no more than 50 post IDs, all of them are returned. 1442 * 1443 * @since 7.2.0 1444 * 1445 * @global wpdb $wpdb WordPress database abstraction object. 1446 * 1447 * @return int[] Up to 50 distinct post IDs. Posts with the IDs may not exist. 1448 * 1449 * @phpstan-return non-empty-list<int> 1450 */ 1451 function wp_get_random_content_sample_ids(): array { 1452 global $wpdb; 1453 1454 $max_id = wp_cache_get( 'max_post_id', 'random-content' ); 1455 1456 // Some object cache drop-ins return integers as numeric strings, so is_int() cannot be used. 1457 if ( ! is_numeric( $max_id ) || $max_id < 0 ) { 1458 $max_id = (int) $wpdb->get_var( "SELECT MAX(ID) FROM {$wpdb->posts}" ); 1459 wp_cache_set( 'max_post_id', $max_id, 'random-content', 10 * MINUTE_IN_SECONDS ); 1460 } 1461 1462 $max_id = (int) $max_id; 1463 1464 if ( $max_id <= 50 ) { 1465 return range( 1, max( 1, $max_id ) ); 1466 } 1467 1468 $post_ids = array(); 1469 1470 while ( count( $post_ids ) < 50 ) { 1471 $post_ids[ wp_rand( 1, $max_id ) ] = true; 1472 } 1473 1474 return array_keys( $post_ids ); 1475 } 1476 1477 /** 1478 * Retrieves the SQL clause limiting the random content query to random post IDs. 1479 * 1480 * @since 7.2.0 1481 * @access private 1482 * 1483 * @global wpdb $wpdb WordPress database abstraction object. 1484 * 1485 * @param int[] $post_ids Post IDs. 1486 * @return string SQL clause. 1487 */ 1488 function _wp_random_content_sample_clause( array $post_ids ): string { 1489 global $wpdb; 1490 1491 return "{$wpdb->posts}.ID IN (" . implode( ',', array_map( 'intval', $post_ids ) ) . ')'; 1492 } 1493 1494 /** 1495 * Limits the random content query to the random post IDs. 1496 * 1497 * Added to the {@see 'posts_where'} filter by {@see WP::handle_random()} while the random 1498 * content query runs. 1499 * 1500 * @since 7.2.0 1501 * 1502 * @see wp_get_random_content_sample_ids() 1503 * 1504 * @param string $where The WHERE clause of the query. 1505 * @param WP_Query $query The WP_Query instance. 1506 * @return string The WHERE clause of the query. 1507 */ 1508 function wp_random_content_posts_where( $where, WP_Query $query ): string { 1509 $sample_ids = $query->get( 'random_sample_ids' ); 1510 1511 if ( wp_is_random_content_query( $query ) && is_array( $sample_ids ) && $sample_ids ) { 1512 $where .= ' AND ' . _wp_random_content_sample_clause( $sample_ids ); 1513 } 1514 1515 return (string) $where; 1516 } 1517 1518 /** 1519 * Removes the order of the random content query. 1520 * 1521 * The random content query is limited to random post IDs, one of which is selected 1522 * once the query has run, see {@see wp_random_content_posts_pre_query()}. Ordering the 1523 * posts would require the database to sort them. 1524 * 1525 * Added to the {@see 'posts_orderby'} filter by {@see WP::handle_random()} while the random 1526 * content query runs. Enforcing the order in SQL prevents {@see 'pre_get_posts'} 1527 * callbacks setting the order of posts on archives from affecting the selection. 1528 * 1529 * @since 7.2.0 1530 * 1531 * @param string $orderby The ORDER BY clause of the query. 1532 * @param WP_Query $query The WP_Query instance. 1533 * @return string The ORDER BY clause of the query. 1534 */ 1535 function wp_random_content_posts_orderby( $orderby, WP_Query $query ): string { 1536 if ( wp_is_random_content_query( $query ) ) { 1537 return ''; 1538 } 1539 1540 return (string) $orderby; 1541 } 1542 1543 /** 1544 * Removes the limit of the random content query. 1545 * 1546 * The random content query is limited to random post IDs, one of which is selected 1547 * once the query has run, see {@see wp_random_content_posts_pre_query()}. 1548 * 1549 * Added to the {@see 'post_limits'} filter by {@see WP::handle_random()} while the random 1550 * content query runs. Enforcing the limit in SQL prevents {@see 'pre_get_posts'} 1551 * callbacks setting the number of posts, offset or page on archives from affecting 1552 * the selection. 1553 * 1554 * @since 7.2.0 1555 * 1556 * @param string $limits The LIMIT clause of the query. 1557 * @param WP_Query $query The WP_Query instance. 1558 * @return string The LIMIT clause of the query. 1559 */ 1560 function wp_random_content_post_limits( $limits, WP_Query $query ): string { 1561 if ( wp_is_random_content_query( $query ) ) { 1562 return ''; 1563 } 1564 1565 return (string) $limits; 1566 } 1567 1568 /** 1569 * Limits the random content query to selecting post IDs. 1570 * 1571 * Added to the {@see 'posts_fields'} filter by {@see WP::handle_random()} while the random 1572 * content query runs. 1573 * 1574 * @since 7.2.0 1575 * 1576 * @global wpdb $wpdb WordPress database abstraction object. 1577 * 1578 * @param string $fields The SELECT clause of the query. 1579 * @param WP_Query $query The WP_Query instance. 1580 * @return string The SELECT clause of the query. 1581 */ 1582 function wp_random_content_posts_fields( $fields, WP_Query $query ): string { 1583 global $wpdb; 1584 1585 if ( wp_is_random_content_query( $query ) ) { 1586 return "{$wpdb->posts}.ID"; 1587 } 1588 1589 return (string) $fields; 1590 } 1591 1592 /** 1593 * Selects the random post from the eligible posts among random post IDs. 1594 * 1595 * The random content query is limited to 50 random post IDs, see 1596 * {@see wp_get_random_content_sample_ids()}, and one of the posts it selects is chosen 1597 * at random. If none of the IDs are eligible posts, the query is run again with other 1598 * random post IDs, up to three times in total. On sites where most post IDs are 1599 * eligible posts, a post is nearly always selected by the first query. 1600 * 1601 * If no post is selected, eligible posts are too sparse among the post IDs to be found 1602 * this way, for example a small category on a site with many posts. A pool of up to 1603 * 1,000 randomly ordered eligible posts is then selected, which is the whole archive 1604 * if it has no more posts than that, and their IDs are stored in the object cache for 1605 * 10 minutes. While the pool is cached, the post is selected from it without querying 1606 * the database for the eligible posts. Selecting the pool requires the database to 1607 * read every eligible post, so sites with a persistent object cache do so at most once 1608 * every 10 minutes for each such archive. 1609 * 1610 * The pool is selected without excluding password protected posts, which are skipped 1611 * when the post is selected from it, so the database can read the eligible posts from 1612 * an index rather than reading each post in full. 1613 * 1614 * Only one request selects the pool at a time. Once the pool is 10 minutes old, the 1615 * first request to need it selects the next pool and, until it has, other requests 1616 * select from the previous pool, which is kept for an hour. If there is no previous 1617 * pool, as the archive has not been requested recently, concurrent requests each 1618 * select a pool. 1619 * 1620 * The pool is keyed by the SQL of the random content query, excluding the random post 1621 * IDs, so each archive, and each variation of an archive produced by 1622 * {@see 'pre_get_posts'} callbacks or query filters, has its own pool. 1623 * 1624 * As the pool may be up to 10 minutes old, posts that have since been unpublished, 1625 * deleted, password protected or changed to another post type are skipped. If none of 1626 * its posts are eligible, a post is selected as if there was no pool. Other changes, 1627 * such as a post being published or removed from a term, are reflected once the pool 1628 * expires. This includes the empty pool of an archive without eligible posts, which 1629 * is cached so the archive is not queried for every request. 1630 * 1631 * Added to the {@see 'posts_pre_query'} filter by {@see WP::handle_random()} while the 1632 * random content query runs. The database is only read from, never written to. 1633 * 1634 * @since 7.2.0 1635 * 1636 * @global wpdb $wpdb WordPress database abstraction object. 1637 * 1638 * @param WP_Post[]|int[]|null $posts Posts short-circuiting the query, or null to run the query. 1639 * @param WP_Query $query The WP_Query instance. 1640 * @return WP_Post[]|int[]|null The selected post, an empty array if no post is eligible, 1641 * or the unmodified value if the query is not for random content. 1642 */ 1643 function wp_random_content_posts_pre_query( $posts, WP_Query $query ) { 1644 global $wpdb; 1645 1646 if ( null !== $posts || ! wp_is_random_content_query( $query ) || ! is_string( $query->request ) ) { 1647 return $posts; 1648 } 1649 1650 // The placeholder escape string is unique to each request, see wpdb::placeholder_escape(). 1651 $request = $wpdb->remove_placeholder_escape( $query->request ); 1652 $sample_ids = $query->get( 'random_sample_ids' ); 1653 $sample_clause = is_array( $sample_ids ) && $sample_ids ? _wp_random_content_sample_clause( $sample_ids ) : ''; 1654 1655 /* 1656 * The random post IDs can only be replaced, to look up other IDs or to select the 1657 * pool, if another callback has not modified their clause. 1658 */ 1659 $can_resample = '' !== $sample_clause && 1 === substr_count( $request, $sample_clause ); 1660 $cache_key = $can_resample ? 'pool:' . md5( str_replace( $sample_clause, '', $request ) ) : ''; 1661 $post_types = (array) $query->get( 'post_type' ); 1662 1663 /** 1664 * Selects a random eligible post from post IDs. 1665 * 1666 * @param array<int|string> $post_ids Post IDs. 1667 * @return WP_Post|null The selected post, or null if none of the posts are eligible. 1668 */ 1669 $select_post = static function ( array $post_ids ) use ( $post_types ): ?WP_Post { 1670 shuffle( $post_ids ); 1671 1672 foreach ( $post_ids as $post_id ) { 1673 $post = get_post( (int) $post_id ); 1674 1675 if ( 1676 $post instanceof WP_Post 1677 && 'publish' === $post->post_status 1678 && '' === $post->post_password 1679 && in_array( $post->post_type, $post_types, true ) 1680 ) { 1681 return $post; 1682 } 1683 } 1684 1685 return null; 1686 }; 1687 1688 // The pool selected by an earlier request, once it is more than 10 minutes old. 1689 $stale_pool = null; 1690 1691 if ( $can_resample ) { 1692 $cached = wp_cache_get( $cache_key, 'random-content' ); 1693 1694 if ( is_array( $cached ) && isset( $cached['post_ids'], $cached['expires'] ) && is_array( $cached['post_ids'] ) ) { 1695 if ( $cached['expires'] > time() ) { 1696 // The archive had no eligible posts when the pool was selected. 1697 if ( array() === $cached['post_ids'] ) { 1698 return array(); 1699 } 1700 1701 $post = $select_post( $cached['post_ids'] ); 1702 1703 if ( null !== $post ) { 1704 return array( $post ); 1705 } 1706 } else { 1707 $stale_pool = $cached['post_ids']; 1708 } 1709 } 1710 } 1711 1712 // Look up random post IDs until one of them is an eligible post. 1713 for ( $attempt = 1; $attempt <= 3; $attempt++ ) { 1714 if ( $attempt > 1 ) { 1715 $request = str_replace( 1716 $sample_clause, 1717 _wp_random_content_sample_clause( wp_get_random_content_sample_ids() ), 1718 $wpdb->remove_placeholder_escape( $query->request ) 1719 ); 1720 } 1721 1722 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts(). 1723 $post = $select_post( $wpdb->get_col( $request ) ); 1724 1725 if ( null !== $post ) { 1726 return array( $post ); 1727 } 1728 1729 if ( ! $can_resample ) { 1730 return array(); 1731 } 1732 } 1733 1734 /* 1735 * Eligible posts are sparse among the post IDs, so select a pool of them. 1736 * 1737 * Only one request selects the pool at a time. While it does, other requests select 1738 * from the previous pool, if there is one, rather than each reading every eligible 1739 * post. wp_cache_add() only stores the lock if another request does not hold it. 1740 */ 1741 $lock_key = $cache_key . ':lock'; 1742 $has_lock = wp_cache_add( $lock_key, 1, 'random-content', MINUTE_IN_SECONDS ); 1743 1744 if ( ! $has_lock && null !== $stale_pool ) { 1745 $post = $select_post( $stale_pool ); 1746 1747 if ( null !== $post ) { 1748 return array( $post ); 1749 } 1750 1751 if ( array() === $stale_pool ) { 1752 return array(); 1753 } 1754 } 1755 1756 $request = str_replace( $sample_clause, '1=1', $wpdb->remove_placeholder_escape( $query->request ) ); 1757 1758 /* 1759 * The post password is not indexed, so excluding password protected posts in SQL 1760 * requires the database to read the full row of every eligible post. They are 1761 * excluded when the post is selected from the pool instead, which allows the pool 1762 * to be selected from an index. 1763 */ 1764 $password_clause = " AND {$wpdb->posts}.post_password = ''"; 1765 1766 if ( 1 === substr_count( $request, $password_clause ) ) { 1767 $request = str_replace( $password_clause, '', $request ); 1768 } 1769 1770 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The request is prepared by WP_Query::get_posts(). 1771 $pool = array_map( 'intval', $wpdb->get_col( $request . ' ORDER BY RAND() LIMIT 1000' ) ); 1772 $post = $select_post( $pool ); 1773 1774 /* 1775 * A pool of fewer than 1,000 posts is the whole archive. If none of its posts are 1776 * eligible, as they are all password protected, the archive has no eligible posts. 1777 */ 1778 if ( null === $post && count( $pool ) < 1000 ) { 1779 $pool = array(); 1780 } 1781 1782 /* 1783 * The pool is used for 10 minutes. It is stored for an hour, so that requests can 1784 * select from it while the next pool is being selected. 1785 */ 1786 wp_cache_set( 1787 $cache_key, 1788 array( 1789 'post_ids' => $pool, 1790 'expires' => time() + 10 * MINUTE_IN_SECONDS, 1791 ), 1792 'random-content', 1793 HOUR_IN_SECONDS 1794 ); 1795 1796 if ( $has_lock ) { 1797 wp_cache_delete( $lock_key, 'random-content' ); 1798 } 1799 1800 return null === $post ? array() : array( $post ); 1801 } 1802 1803 /** 1804 * Redirects random content requests to the randomly selected post. 1805 * 1806 * Added to the {@see 'template_redirect'} action at priority 100 by 1807 * {@see WP::handle_random()} once the main query has selected the random post, so the 1808 * redirect is sent after the request is set up and the {@see 'wp'} action has fired. 1809 * The headers preventing the redirect being cached or indexed are sent with it. 1810 * 1811 * The post is read from the main query rather than the current query, so the redirect 1812 * is still sent when an earlier callback replaces the current query, for example by 1813 * calling {@see query_posts()}. 1814 * 1815 * The request ends once a post is selected. The redirect is to the post's permalink 1816 * as returned by {@see get_permalink()}, including a permalink filtered to be on 1817 * another site. 1818 * 1819 * If no post is selected, for example on an empty archive, {@see WP::handle_random()} 1820 * handles the request as a 404. 1821 * 1822 * @since 7.2.0 1823 * 1824 * @global WP_Query $wp_the_query WordPress main Query object. 1825 */ 1826 function wp_random_content_redirect(): void { 1827 global $wp_the_query; 1828 1829 if ( 1830 ! $wp_the_query instanceof WP_Query 1831 || ! wp_is_random_content_query( $wp_the_query ) 1832 || ! $wp_the_query->post instanceof WP_Post 1833 ) { 1834 return; 1835 } 1836 1837 nocache_headers(); 1838 1839 if ( ! headers_sent() ) { 1840 header( 'X-Robots-Tag: noindex, follow' ); 1841 } 1842 1843 $location = get_permalink( $wp_the_query->post ); 1844 1845 wp_redirect( $location, 302 ); 1846 exit; 1847 } 1848 1849 /** 1850 * Flushes the rewrite rules when a page or post at the random content base changes. 1851 * 1852 * A published page at the random base, for example `example.com/random/`, takes 1853 * precedence over random content redirects. So does a published post when the 1854 * permalink structure places posts at the same URL, for example `/%postname%/`, 1855 * see {@see WP_Rewrite::post_permalinks_share_random_base()}. As the rewrite rules account 1856 * for the page or post, they need to be regenerated when it is published, 1857 * unpublished, renamed, moved or deleted. 1858 * 1859 * Runs on the {@see 'wp_after_insert_post'} and {@see 'after_delete_post'} actions. 1860 * Third-party code may fire these actions with unexpected values, so the parameters are 1861 * not type declared. When `$post` is omitted it is looked up from `$post_id`. Passing 1862 * anything other than a `WP_Post` as `$post`, or a post that cannot be looked up, is 1863 * incorrect usage and nothing is flushed. `$post_before` is only considered on 1864 * `wp_after_insert_post`, where passing anything other than a `WP_Post` or null is 1865 * incorrect usage and the value is ignored. 1866 * 1867 * @since 7.2.0 1868 * 1869 * @see WP_Rewrite::random_rewrite_rules() 1870 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. 1871 * 1872 * @param int $post_id Post ID. 1873 * @param WP_Post|null $post Optional. Post object. Looked up from `$post_id` when null. Default null. 1874 * @param bool $update Optional. Whether this is an existing post being updated. Default false. 1875 * @param WP_Post|null $post_before Optional. Post object before the update, null for new posts. Default null. 1876 */ 1877 function wp_random_content_flush_rewrite_rules_for_post( $post_id, $post = null, $update = false, $post_before = null ): void { 1878 global $wp_rewrite; 1879 1880 // Only look up a real ID: get_post() falls back to the global post for an empty value. 1881 if ( null === $post && (int) $post_id > 0 ) { 1882 $post = get_post( (int) $post_id ); 1883 } 1884 1885 if ( ! $post instanceof WP_Post ) { 1886 _doing_it_wrong( 1887 __FUNCTION__, 1888 sprintf( 1889 /* translators: 1: $post, 2: WP_Post */ 1890 __( 'The %1$s argument must be a %2$s object.' ), 1891 '<code>$post</code>', 1892 '<code>WP_Post</code>' 1893 ), 1894 '7.2.0' 1895 ); 1896 return; 1897 } 1898 1899 $posts = array( $post ); 1900 1901 // Only wp_after_insert_post passes the previous post object, which matters when it was renamed or moved. 1902 if ( 'wp_after_insert_post' === current_action() ) { 1903 if ( $post_before instanceof WP_Post ) { 1904 $posts[] = $post_before; 1905 } elseif ( null !== $post_before ) { 1906 _doing_it_wrong( 1907 __FUNCTION__, 1908 sprintf( 1909 /* translators: 1: $post_before, 2: WP_Post */ 1910 __( 'The %1$s argument must be a %2$s object or null.' ), 1911 '<code>$post_before</code>', 1912 '<code>WP_Post</code>' 1913 ), 1914 '7.2.0' 1915 ); 1916 } 1917 } 1918 1919 if ( ! $wp_rewrite->using_permalinks() || ! wp_is_random_content_redirect_enabled() ) { 1920 return; 1921 } 1922 1923 foreach ( $posts as $page ) { 1924 if ( 1925 $wp_rewrite->get_random_base() === $page->post_name 1926 && ( 1927 ( 'page' === $page->post_type && 0 === (int) $page->post_parent ) 1928 || ( 'post' === $page->post_type && $wp_rewrite->post_permalinks_share_random_base() ) 1929 ) 1930 ) { 1931 flush_rewrite_rules( false ); 1932 return; 1933 } 1934 } 1935 } 1936 1937 /** 1938 * Set up global post data. 1939 * 1940 * @since 1.5.0 1941 * @since 4.4.0 Added the ability to pass a post ID to `$post`. 1942 * 1943 * @global WP_Query $wp_query WordPress Query object. 1944 * 1945 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 1946 * @return bool True when finished. 1947 */ 1948 function setup_postdata( $post ) { 1949 global $wp_query; 1950 1951 if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) { 1952 return $wp_query->setup_postdata( $post ); 1953 } 1954 1955 return false; 1956 } 1957 1958 /** 1959 * Generates post data. 1960 * 1961 * @since 5.2.0 1962 * 1963 * @global WP_Query $wp_query WordPress Query object. 1964 * 1965 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 1966 * @return array|false Elements of post, or false on failure. 1967 */ 1968 function generate_postdata( $post ) { 1969 global $wp_query; 1970 1971 if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) { 1972 return $wp_query->generate_postdata( $post ); 1973 } 1974 1975 return false; 1976 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Dec 4 08:20:01 2026 | Cross-referenced by PHPXref |