| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Query API: WP_Query class 4 * 5 * @package WordPress 6 * @subpackage Query 7 * @since 4.7.0 8 */ 9 10 /** 11 * The WordPress Query class. 12 * 13 * @link https://developer.wordpress.org/reference/classes/wp_query/ 14 * 15 * @since 1.5.0 16 * @since 4.5.0 Removed the `$comments_popup` property. 17 */ 18 #[AllowDynamicProperties] 19 class WP_Query { 20 21 /** 22 * Query vars set by the user. 23 * 24 * @since 1.5.0 25 * @var ?array 26 */ 27 public $query; 28 29 /** 30 * Query vars, after parsing. 31 * 32 * @since 1.5.0 33 * @var array 34 */ 35 public $query_vars = array(); 36 37 /** 38 * Taxonomy query, as passed to get_tax_sql(). 39 * 40 * @since 3.1.0 41 * @var WP_Tax_Query|null A taxonomy query instance. 42 */ 43 public $tax_query; 44 45 /** 46 * Metadata query container. 47 * 48 * @since 3.2.0 49 * @var WP_Meta_Query A meta query instance. 50 */ 51 public $meta_query = false; 52 53 /** 54 * Date query container. 55 * 56 * @since 3.7.0 57 * @var WP_Date_Query A date query instance. 58 */ 59 public $date_query = false; 60 61 /** 62 * Holds the data for a single object that is queried. 63 * 64 * Holds the contents of a post, page, category, attachment. 65 * 66 * @since 1.5.0 67 * @var WP_Term|WP_Post_Type|WP_Post|WP_User|null 68 */ 69 public $queried_object; 70 71 /** 72 * The ID of the queried object. 73 * 74 * @since 1.5.0 75 * @var ?int 76 */ 77 public $queried_object_id; 78 79 /** 80 * SQL for the database query. 81 * 82 * @since 2.0.1 83 * @var ?string 84 */ 85 public $request; 86 87 /** 88 * Array of post objects or post IDs. 89 * 90 * @since 1.5.0 91 * @var WP_Post[]|int[]|null 92 */ 93 public $posts; 94 95 /** 96 * The number of posts for the current query. 97 * 98 * @since 1.5.0 99 * @var int 100 */ 101 public $post_count = 0; 102 103 /** 104 * Index of the current item in the loop. 105 * 106 * @since 1.5.0 107 * @var int 108 */ 109 public $current_post = -1; 110 111 /** 112 * Whether the caller is before the loop. 113 * 114 * @since 6.3.0 115 * @var bool 116 */ 117 public $before_loop = true; 118 119 /** 120 * Whether the loop has started and the caller is in the loop. 121 * 122 * @since 2.0.0 123 * @var bool 124 */ 125 public $in_the_loop = false; 126 127 /** 128 * The current post. 129 * 130 * This property does not get populated when the `fields` argument is set to 131 * `ids` or `id=>parent`. 132 * 133 * @since 1.5.0 134 * @var WP_Post|null 135 */ 136 public $post; 137 138 /** 139 * The list of comments for current post. 140 * 141 * @since 2.2.0 142 * @var ?WP_Comment[] 143 */ 144 public $comments; 145 146 /** 147 * The number of comments for the posts. 148 * 149 * @since 2.2.0 150 * @var int 151 */ 152 public $comment_count = 0; 153 154 /** 155 * The index of the comment in the comment loop. 156 * 157 * @since 2.2.0 158 * @var int 159 */ 160 public $current_comment = -1; 161 162 /** 163 * Current comment object. 164 * 165 * @since 2.2.0 166 * @var ?WP_Comment 167 */ 168 public $comment; 169 170 /** 171 * The number of found posts for the current query. 172 * 173 * If limit clause was not used, equals $post_count. 174 * 175 * @since 2.1.0 176 * @var int 177 */ 178 public $found_posts = 0; 179 180 /** 181 * The number of pages. 182 * 183 * @since 2.1.0 184 * @var int 185 */ 186 public $max_num_pages = 0; 187 188 /** 189 * The number of comment pages. 190 * 191 * @since 2.7.0 192 * @var int 193 */ 194 public $max_num_comment_pages = 0; 195 196 /** 197 * Signifies whether the current query is for a single post. 198 * 199 * @since 1.5.0 200 * @var bool 201 */ 202 public $is_single = false; 203 204 /** 205 * Signifies whether the current query is for a preview. 206 * 207 * @since 2.0.0 208 * @var bool 209 */ 210 public $is_preview = false; 211 212 /** 213 * Signifies whether the current query is for a page. 214 * 215 * @since 1.5.0 216 * @var bool 217 */ 218 public $is_page = false; 219 220 /** 221 * Signifies whether the current query is for an archive. 222 * 223 * @since 1.5.0 224 * @var bool 225 */ 226 public $is_archive = false; 227 228 /** 229 * Signifies whether the current query is for a date archive. 230 * 231 * @since 1.5.0 232 * @var bool 233 */ 234 public $is_date = false; 235 236 /** 237 * Signifies whether the current query is for a year archive. 238 * 239 * @since 1.5.0 240 * @var bool 241 */ 242 public $is_year = false; 243 244 /** 245 * Signifies whether the current query is for a month archive. 246 * 247 * @since 1.5.0 248 * @var bool 249 */ 250 public $is_month = false; 251 252 /** 253 * Signifies whether the current query is for a day archive. 254 * 255 * @since 1.5.0 256 * @var bool 257 */ 258 public $is_day = false; 259 260 /** 261 * Signifies whether the current query is for a specific time. 262 * 263 * @since 1.5.0 264 * @var bool 265 */ 266 public $is_time = false; 267 268 /** 269 * Signifies whether the current query is for an author archive. 270 * 271 * @since 1.5.0 272 * @var bool 273 */ 274 public $is_author = false; 275 276 /** 277 * Signifies whether the current query is for a category archive. 278 * 279 * @since 1.5.0 280 * @var bool 281 */ 282 public $is_category = false; 283 284 /** 285 * Signifies whether the current query is for a tag archive. 286 * 287 * @since 2.3.0 288 * @var bool 289 */ 290 public $is_tag = false; 291 292 /** 293 * Signifies whether the current query is for a taxonomy archive. 294 * 295 * @since 2.5.0 296 * @var bool 297 */ 298 public $is_tax = false; 299 300 /** 301 * Signifies whether the current query is for a search. 302 * 303 * @since 1.5.0 304 * @var bool 305 */ 306 public $is_search = false; 307 308 /** 309 * Signifies whether the current query is for a feed. 310 * 311 * @since 1.5.0 312 * @var bool 313 */ 314 public $is_feed = false; 315 316 /** 317 * Signifies whether the current query is for a comment feed. 318 * 319 * @since 2.2.0 320 * @var bool 321 */ 322 public $is_comment_feed = false; 323 324 /** 325 * Signifies whether the current query is for trackback endpoint call. 326 * 327 * @since 1.5.0 328 * @var bool 329 */ 330 public $is_trackback = false; 331 332 /** 333 * Signifies whether the current query is for the site homepage. 334 * 335 * @since 1.5.0 336 * @var bool 337 */ 338 public $is_home = false; 339 340 /** 341 * Signifies whether the current query is for the Privacy Policy page. 342 * 343 * @since 5.2.0 344 * @var bool 345 */ 346 public $is_privacy_policy = false; 347 348 /** 349 * Signifies whether the current query couldn't find anything. 350 * 351 * @since 1.5.0 352 * @var bool 353 */ 354 public $is_404 = false; 355 356 /** 357 * Signifies whether the current query is for an embed. 358 * 359 * @since 4.4.0 360 * @var bool 361 */ 362 public $is_embed = false; 363 364 /** 365 * Signifies whether the current query is for a paged result and not for the first page. 366 * 367 * @since 1.5.0 368 * @var bool 369 */ 370 public $is_paged = false; 371 372 /** 373 * Signifies whether the current query is for an administrative interface page. 374 * 375 * @since 1.5.0 376 * @var bool 377 */ 378 public $is_admin = false; 379 380 /** 381 * Signifies whether the current query is for an attachment page. 382 * 383 * @since 2.0.0 384 * @var bool 385 */ 386 public $is_attachment = false; 387 388 /** 389 * Signifies whether the current query is for an existing single post of any post type 390 * (post, attachment, page, custom post types). 391 * 392 * @since 2.1.0 393 * @var bool 394 */ 395 public $is_singular = false; 396 397 /** 398 * Signifies whether the current query is for the robots.txt file. 399 * 400 * @since 2.1.0 401 * @var bool 402 */ 403 public $is_robots = false; 404 405 /** 406 * Signifies whether the current query is for the favicon.ico file. 407 * 408 * @since 5.4.0 409 * @var bool 410 */ 411 public $is_favicon = false; 412 413 /** 414 * Signifies whether the current query is for a sitemap. 415 * 416 * @since 7.1.0 417 */ 418 public bool $is_sitemap = false; 419 420 /** 421 * Signifies whether the current query is for the page_for_posts page. 422 * 423 * Basically, the homepage if the option isn't set for the static homepage. 424 * 425 * @since 2.1.0 426 * @var bool 427 */ 428 public $is_posts_page = false; 429 430 /** 431 * Signifies whether the current query is for a post type archive. 432 * 433 * @since 3.1.0 434 * @var bool 435 */ 436 public $is_post_type_archive = false; 437 438 /** 439 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know 440 * whether we have to re-parse because something has changed 441 * 442 * @since 3.1.0 443 * @var bool|string 444 */ 445 private $query_vars_hash = false; 446 447 /** 448 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made 449 * via pre_get_posts hooks. 450 * 451 * @since 3.1.1 452 * @var bool 453 */ 454 private $query_vars_changed = true; 455 456 /** 457 * Set if post thumbnails are cached 458 * 459 * @since 3.2.0 460 * @var bool 461 */ 462 public $thumbnails_cached = false; 463 464 /** 465 * Controls whether an attachment query should include filenames or not. 466 * 467 * @since 6.0.3 468 * @var bool 469 */ 470 protected $allow_query_attachment_by_filename = false; 471 472 /** 473 * Cached list of search stopwords. 474 * 475 * @since 3.7.0 476 * @var ?array 477 */ 478 private $stopwords; 479 480 private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' ); 481 482 private $compat_methods = array( 'init_query_flags', 'parse_tax_query' ); 483 484 /** 485 * The cache key generated by the query. 486 * 487 * The cache key is generated by the method ::generate_cache_key() after the 488 * query has been normalized. 489 * 490 * @since 6.8.0 491 * @var string 492 */ 493 private $query_cache_key = ''; 494 495 /** 496 * Resets query flags to false. 497 * 498 * The query flags are what page info WordPress was able to figure out. 499 * 500 * @since 2.0.0 501 */ 502 private function init_query_flags() { 503 $this->is_single = false; 504 $this->is_preview = false; 505 $this->is_page = false; 506 $this->is_archive = false; 507 $this->is_date = false; 508 $this->is_year = false; 509 $this->is_month = false; 510 $this->is_day = false; 511 $this->is_time = false; 512 $this->is_author = false; 513 $this->is_category = false; 514 $this->is_tag = false; 515 $this->is_tax = false; 516 $this->is_search = false; 517 $this->is_feed = false; 518 $this->is_comment_feed = false; 519 $this->is_trackback = false; 520 $this->is_home = false; 521 $this->is_privacy_policy = false; 522 $this->is_404 = false; 523 $this->is_paged = false; 524 $this->is_admin = false; 525 $this->is_attachment = false; 526 $this->is_singular = false; 527 $this->is_robots = false; 528 $this->is_favicon = false; 529 $this->is_sitemap = false; 530 $this->is_posts_page = false; 531 $this->is_post_type_archive = false; 532 } 533 534 /** 535 * Initiates object properties and sets default values. 536 * 537 * @since 1.5.0 538 */ 539 public function init() { 540 unset( $this->posts ); 541 unset( $this->query ); 542 $this->query_vars = array(); 543 unset( $this->queried_object ); 544 unset( $this->queried_object_id ); 545 $this->post_count = 0; 546 $this->current_post = -1; 547 $this->in_the_loop = false; 548 $this->before_loop = true; 549 unset( $this->request ); 550 unset( $this->post ); 551 unset( $this->comments ); 552 unset( $this->comment ); 553 $this->comment_count = 0; 554 $this->current_comment = -1; 555 $this->found_posts = 0; 556 $this->max_num_pages = 0; 557 $this->max_num_comment_pages = 0; 558 559 $this->init_query_flags(); 560 } 561 562 /** 563 * Reparses the query vars. 564 * 565 * @since 1.5.0 566 */ 567 public function parse_query_vars() { 568 $this->parse_query(); 569 } 570 571 /** 572 * Fills in the query variables, which do not exist within the parameter. 573 * 574 * @since 2.1.0 575 * @since 4.5.0 Removed the `comments_popup` public query variable. 576 * 577 * @param array $query_vars Defined query variables. 578 * @return array Complete query variables with undefined ones filled in empty. 579 */ 580 public function fill_query_vars( $query_vars ) { 581 $keys = array( 582 'error', 583 'm', 584 'p', 585 'post_parent', 586 'subpost', 587 'subpost_id', 588 'attachment', 589 'attachment_id', 590 'name', 591 'pagename', 592 'page_id', 593 'second', 594 'minute', 595 'hour', 596 'day', 597 'monthnum', 598 'year', 599 'w', 600 'category_name', 601 'tag', 602 'cat', 603 'tag_id', 604 'author', 605 'author_name', 606 'feed', 607 'tb', 608 'paged', 609 'meta_key', 610 'meta_value', 611 'preview', 612 's', 613 'sentence', 614 'title', 615 'fields', 616 'menu_order', 617 'embed', 618 ); 619 620 foreach ( $keys as $key ) { 621 if ( ! isset( $query_vars[ $key ] ) ) { 622 $query_vars[ $key ] = ''; 623 } 624 } 625 626 $array_keys = array( 627 'category__in', 628 'category__not_in', 629 'category__and', 630 'post__in', 631 'post__not_in', 632 'post_name__in', 633 'tag__in', 634 'tag__not_in', 635 'tag__and', 636 'tag_slug__in', 637 'tag_slug__and', 638 'post_parent__in', 639 'post_parent__not_in', 640 'author__in', 641 'author__not_in', 642 'search_columns', 643 ); 644 645 foreach ( $array_keys as $key ) { 646 if ( ! isset( $query_vars[ $key ] ) ) { 647 $query_vars[ $key ] = array(); 648 } 649 } 650 651 return $query_vars; 652 } 653 654 /** 655 * Parses a query string and sets query type booleans. 656 * 657 * @since 1.5.0 658 * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's 659 * array key to `$orderby`. 660 * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded 661 * search terms, by prepending a hyphen. 662 * @since 4.5.0 Removed the `$comments_popup` parameter. 663 * Introduced the `$comment_status` and `$ping_status` parameters. 664 * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts. 665 * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument. 666 * @since 4.9.0 Introduced the `$comment_count` parameter. 667 * @since 5.1.0 Introduced the `$meta_compare_key` parameter. 668 * @since 5.3.0 Introduced the `$meta_type_key` parameter. 669 * @since 6.1.0 Introduced the `$update_menu_item_cache` parameter. 670 * @since 6.2.0 Introduced the `$search_columns` parameter. 671 * 672 * @param string|array $query { 673 * Optional. Array or string of Query parameters. 674 * 675 * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type. 676 * @type int|string $author Author ID, or comma-separated list of IDs. 677 * @type string $author_name User 'user_nicename'. 678 * @type int[] $author__in An array of author IDs to query from. 679 * @type int[] $author__not_in An array of author IDs not to query from. 680 * @type bool $cache_results Whether to cache post information. Default true. 681 * @type int|string $cat Category ID or comma-separated list of IDs (this or any children). 682 * @type int[] $category__and An array of category IDs (AND in). 683 * @type int[] $category__in An array of category IDs (OR in, no children). 684 * @type int[] $category__not_in An array of category IDs (NOT in). 685 * @type string $category_name Use category slug (not name, this or any children). 686 * @type array|int $comment_count Filter results by comment count. Provide an integer to match 687 * comment count exactly. Provide an array with integer 'value' 688 * and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to 689 * compare against comment_count in a specific way. 690 * @type string $comment_status Comment status. 691 * @type int $comments_per_page The number of comments to return per page. 692 * Default 'comments_per_page' option. 693 * @type array $date_query An associative array of WP_Date_Query arguments. 694 * See WP_Date_Query::__construct(). 695 * @type int $day Day of the month. Default empty. Accepts numbers 1-31. 696 * @type bool $exact Whether to search by exact keyword. Default false. 697 * @type string $fields Post fields to query for. Accepts: 698 * - '' Returns an array of complete post objects (`WP_Post[]`). 699 * - 'ids' Returns an array of post IDs (`int[]`). 700 * - 'id=>parent' Returns an associative array of parent post IDs, 701 * keyed by post ID (`int[]`). 702 * Default ''. 703 * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23. 704 * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false 705 * excludes stickies from 'post__in'. Accepts 1|true, 0|false. 706 * Default false. 707 * @type int $m Combination YearMonth. Accepts any four-digit year and month 708 * numbers 01-12. Default empty. 709 * @type string|string[] $meta_key Meta key or keys to filter by. 710 * @type string|string[] $meta_value Meta value or values to filter by. 711 * @type string $meta_compare MySQL operator used for comparing the meta value. 712 * See WP_Meta_Query::__construct() for accepted values and default value. 713 * @type string $meta_compare_key MySQL operator used for comparing the meta key. 714 * See WP_Meta_Query::__construct() for accepted values and default value. 715 * @type string $meta_type MySQL data type that the meta_value column will be CAST to for comparisons. 716 * See WP_Meta_Query::__construct() for accepted values and default value. 717 * @type string $meta_type_key MySQL data type that the meta_key column will be CAST to for comparisons. 718 * See WP_Meta_Query::__construct() for accepted values and default value. 719 * @type array $meta_query An associative array of WP_Meta_Query arguments. 720 * See WP_Meta_Query::__construct() for accepted values. 721 * @type int $menu_order The menu order of the posts. 722 * @type int $minute Minute of the hour. Default empty. Accepts numbers 0-59. 723 * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12. 724 * @type string $name Post slug. 725 * @type bool $nopaging Show all posts (true) or paginate (false). Default false. 726 * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve 727 * performance. Default false. 728 * @type int $offset The number of posts to offset before retrieval. 729 * @type string $order Designates ascending or descending order of posts. Default 'DESC'. 730 * Accepts 'ASC', 'DESC'. 731 * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be passed. 732 * To use 'meta_value', or 'meta_value_num', 'meta_key=keyname' must be 733 * also be defined. To sort by a specific `$meta_query` clause, use that 734 * clause's array key. Accepts: 735 * - 'none' 736 * - 'name' 737 * - 'author' 738 * - 'date' 739 * - 'title' 740 * - 'modified' 741 * - 'menu_order' 742 * - 'parent' 743 * - 'ID' 744 * - 'rand' 745 * - 'relevance' 746 * - 'RAND(x)' (where 'x' is an integer seed value) 747 * - 'comment_count' 748 * - 'meta_value' 749 * - 'meta_value_num' 750 * - 'post__in' 751 * - 'post_name__in' 752 * - 'post_parent__in' 753 * - The array keys of `$meta_query`. 754 * Default is 'date', except when a search is being performed, when 755 * the default is 'relevance'. 756 * @type int $p Post ID. 757 * @type int $page Show the number of posts that would show up on page X of a 758 * static front page. 759 * @type int $paged The number of the current page. 760 * @type int $page_id Page ID. 761 * @type string $pagename Page slug. 762 * @type string $perm Show posts if user has the appropriate capability. 763 * @type string $ping_status Ping status. 764 * @type int[] $post__in An array of post IDs to retrieve, sticky posts will be included. 765 * @type int[] $post__not_in An array of post IDs not to retrieve. Note: a string of comma- 766 * separated IDs will NOT work. 767 * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type. 768 * @type string[] $post_name__in An array of post slugs that results must match. 769 * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve 770 * top-level pages. 771 * @type int[] $post_parent__in An array containing parent page IDs to query child pages from. 772 * @type int[] $post_parent__not_in An array containing parent page IDs not to query child pages from. 773 * @type string|string[] $post_type A post type slug (string) or array of post type slugs. 774 * Default 'any' if using 'tax_query'. 775 * @type string|string[] $post_status A post status (string) or array of post statuses. 776 * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts. 777 * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides 778 * 'posts_per_page' when is_archive(), or is_search() are true. 779 * @type string $s Search keyword(s). Prepending a term with a hyphen will 780 * exclude posts matching that term. Eg, 'pillow -sofa' will 781 * return posts containing 'pillow' but not 'sofa'. The 782 * character used for exclusion can be modified using the 783 * the 'wp_query_search_exclusion_prefix' filter. 784 * @type string[] $search_columns Array of column names to be searched. Accepts 'post_title', 785 * 'post_excerpt' and 'post_content'. Default empty array. 786 * @type int $second Second of the minute. Default empty. Accepts numbers 0-59. 787 * @type bool $sentence Whether to search by phrase. Default false. 788 * @type bool $suppress_filters Whether to suppress filters. Default false. 789 * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all). 790 * @type int[] $tag__and An array of tag IDs (AND in). 791 * @type int[] $tag__in An array of tag IDs (OR in). 792 * @type int[] $tag__not_in An array of tag IDs (NOT in). 793 * @type int $tag_id Tag id or comma-separated list of IDs. 794 * @type string[] $tag_slug__and An array of tag slugs (AND in). 795 * @type string[] $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is 796 * true. Note: a string of comma-separated IDs will NOT work. 797 * @type array $tax_query An associative array of WP_Tax_Query arguments. 798 * See WP_Tax_Query::__construct(). 799 * @type string $title Post title. 800 * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. 801 * @type bool $update_post_term_cache Whether to update the post term cache. Default true. 802 * @type bool $update_menu_item_cache Whether to update the menu item cache. Default false. 803 * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will 804 * disable cache priming for term meta, so that each 805 * get_term_meta() call will hit the database. 806 * Defaults to the value of `$update_post_term_cache`. 807 * @type int $w The week number of the year. Default empty. Accepts numbers 1-53. 808 * @type int $year The four-digit year. Default empty. Accepts any four-digit year. 809 * } 810 */ 811 public function parse_query( $query = '' ) { 812 if ( ! empty( $query ) ) { 813 $this->init(); 814 $this->query = wp_parse_args( $query ); 815 $this->query_vars = $this->query; 816 } elseif ( ! isset( $this->query ) ) { 817 $this->query = $this->query_vars; 818 } 819 820 $this->query_vars = $this->fill_query_vars( $this->query_vars ); 821 $query_vars = &$this->query_vars; 822 $this->query_vars_changed = true; 823 824 if ( ! empty( $query_vars['robots'] ) ) { 825 $this->is_robots = true; 826 } elseif ( ! empty( $query_vars['favicon'] ) ) { 827 $this->is_favicon = true; 828 } elseif ( ! empty( $query_vars['sitemap'] ) ) { 829 $this->is_sitemap = true; 830 } 831 832 if ( ! is_scalar( $query_vars['p'] ) || (int) $query_vars['p'] < 0 ) { 833 $query_vars['p'] = 0; 834 $query_vars['error'] = '404'; 835 } else { 836 $query_vars['p'] = (int) $query_vars['p']; 837 } 838 839 $query_vars['page_id'] = is_scalar( $query_vars['page_id'] ) ? absint( $query_vars['page_id'] ) : 0; 840 $query_vars['year'] = is_scalar( $query_vars['year'] ) ? absint( $query_vars['year'] ) : 0; 841 $query_vars['monthnum'] = is_scalar( $query_vars['monthnum'] ) ? absint( $query_vars['monthnum'] ) : 0; 842 $query_vars['day'] = is_scalar( $query_vars['day'] ) ? absint( $query_vars['day'] ) : 0; 843 $query_vars['w'] = is_scalar( $query_vars['w'] ) ? absint( $query_vars['w'] ) : 0; 844 $query_vars['m'] = is_scalar( $query_vars['m'] ) ? preg_replace( '|[^0-9]|', '', $query_vars['m'] ) : ''; 845 $query_vars['paged'] = is_scalar( $query_vars['paged'] ) ? absint( $query_vars['paged'] ) : 0; 846 $query_vars['cat'] = preg_replace( '|[^0-9,-]|', '', $query_vars['cat'] ); // Array or comma-separated list of positive or negative integers. 847 $query_vars['author'] = is_scalar( $query_vars['author'] ) ? preg_replace( '|[^0-9,-]|', '', $query_vars['author'] ) : ''; // Comma-separated list of positive or negative integers. 848 $query_vars['pagename'] = is_scalar( $query_vars['pagename'] ) ? trim( $query_vars['pagename'] ) : ''; 849 $query_vars['name'] = is_scalar( $query_vars['name'] ) ? trim( $query_vars['name'] ) : ''; 850 $query_vars['title'] = is_scalar( $query_vars['title'] ) ? trim( $query_vars['title'] ) : ''; 851 852 if ( is_scalar( $query_vars['hour'] ) && '' !== $query_vars['hour'] ) { 853 $query_vars['hour'] = absint( $query_vars['hour'] ); 854 } else { 855 $query_vars['hour'] = ''; 856 } 857 858 if ( is_scalar( $query_vars['minute'] ) && '' !== $query_vars['minute'] ) { 859 $query_vars['minute'] = absint( $query_vars['minute'] ); 860 } else { 861 $query_vars['minute'] = ''; 862 } 863 864 if ( is_scalar( $query_vars['second'] ) && '' !== $query_vars['second'] ) { 865 $query_vars['second'] = absint( $query_vars['second'] ); 866 } else { 867 $query_vars['second'] = ''; 868 } 869 870 if ( is_scalar( $query_vars['menu_order'] ) && '' !== $query_vars['menu_order'] ) { 871 $query_vars['menu_order'] = absint( $query_vars['menu_order'] ); 872 } else { 873 $query_vars['menu_order'] = ''; 874 } 875 876 // Fairly large, potentially too large, upper bound for search string lengths. 877 if ( ! is_scalar( $query_vars['s'] ) || ( ! empty( $query_vars['s'] ) && strlen( $query_vars['s'] ) > 1600 ) ) { 878 $query_vars['s'] = ''; 879 } 880 881 // Compat. Map subpost to attachment. 882 if ( is_scalar( $query_vars['subpost'] ) && '' != $query_vars['subpost'] ) { 883 $query_vars['attachment'] = $query_vars['subpost']; 884 } 885 if ( is_scalar( $query_vars['subpost_id'] ) && '' != $query_vars['subpost_id'] ) { 886 $query_vars['attachment_id'] = $query_vars['subpost_id']; 887 } 888 889 $query_vars['attachment_id'] = is_scalar( $query_vars['attachment_id'] ) ? absint( $query_vars['attachment_id'] ) : 0; 890 891 if ( ( '' !== $query_vars['attachment'] ) || ! empty( $query_vars['attachment_id'] ) ) { 892 $this->is_single = true; 893 $this->is_attachment = true; 894 } elseif ( '' !== $query_vars['name'] ) { 895 $this->is_single = true; 896 } elseif ( $query_vars['p'] ) { 897 $this->is_single = true; 898 } elseif ( '' !== $query_vars['pagename'] || ! empty( $query_vars['page_id'] ) ) { 899 $this->is_page = true; 900 $this->is_single = false; 901 } else { 902 // Look for archive queries. Dates, categories, authors, search, post type archives. 903 904 if ( isset( $this->query['s'] ) ) { 905 $this->is_search = true; 906 } 907 908 if ( '' !== $query_vars['second'] ) { 909 $this->is_time = true; 910 $this->is_date = true; 911 } 912 913 if ( '' !== $query_vars['minute'] ) { 914 $this->is_time = true; 915 $this->is_date = true; 916 } 917 918 if ( '' !== $query_vars['hour'] ) { 919 $this->is_time = true; 920 $this->is_date = true; 921 } 922 923 if ( $query_vars['day'] ) { 924 if ( ! $this->is_date ) { 925 $date = sprintf( '%04d-%02d-%02d', $query_vars['year'], $query_vars['monthnum'], $query_vars['day'] ); 926 if ( $query_vars['monthnum'] && $query_vars['year'] && ! wp_checkdate( $query_vars['monthnum'], $query_vars['day'], $query_vars['year'], $date ) ) { 927 $query_vars['error'] = '404'; 928 } else { 929 $this->is_day = true; 930 $this->is_date = true; 931 } 932 } 933 } 934 935 if ( $query_vars['monthnum'] ) { 936 if ( ! $this->is_date ) { 937 if ( 12 < $query_vars['monthnum'] ) { 938 $query_vars['error'] = '404'; 939 } else { 940 $this->is_month = true; 941 $this->is_date = true; 942 } 943 } 944 } 945 946 if ( $query_vars['year'] ) { 947 if ( ! $this->is_date ) { 948 $this->is_year = true; 949 $this->is_date = true; 950 } 951 } 952 953 if ( $query_vars['m'] ) { 954 $this->is_date = true; 955 if ( strlen( $query_vars['m'] ) > 9 ) { 956 $this->is_time = true; 957 } elseif ( strlen( $query_vars['m'] ) > 7 ) { 958 $this->is_day = true; 959 } elseif ( strlen( $query_vars['m'] ) > 5 ) { 960 $this->is_month = true; 961 } else { 962 $this->is_year = true; 963 } 964 } 965 966 if ( $query_vars['w'] ) { 967 $this->is_date = true; 968 } 969 970 $this->query_vars_hash = false; 971 $this->parse_tax_query( $query_vars ); 972 973 foreach ( $this->tax_query->queries as $tax_query ) { 974 if ( ! is_array( $tax_query ) ) { 975 continue; 976 } 977 978 if ( isset( $tax_query['operator'] ) && 'NOT IN' !== $tax_query['operator'] ) { 979 switch ( $tax_query['taxonomy'] ) { 980 case 'category': 981 $this->is_category = true; 982 break; 983 case 'post_tag': 984 $this->is_tag = true; 985 break; 986 default: 987 $this->is_tax = true; 988 } 989 } 990 } 991 unset( $tax_query ); 992 993 if ( empty( $query_vars['author'] ) || ( '0' == $query_vars['author'] ) ) { 994 $this->is_author = false; 995 } else { 996 $this->is_author = true; 997 } 998 999 if ( '' !== $query_vars['author_name'] ) { 1000 $this->is_author = true; 1001 } 1002 1003 if ( ! empty( $query_vars['post_type'] ) && ! is_array( $query_vars['post_type'] ) ) { 1004 $post_type_obj = get_post_type_object( $query_vars['post_type'] ); 1005 if ( ! empty( $post_type_obj->has_archive ) ) { 1006 $this->is_post_type_archive = true; 1007 } 1008 } 1009 1010 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) { 1011 $this->is_archive = true; 1012 } 1013 } 1014 1015 if ( '' != $query_vars['feed'] ) { 1016 $this->is_feed = true; 1017 } 1018 1019 if ( '' != $query_vars['embed'] ) { 1020 $this->is_embed = true; 1021 } 1022 1023 if ( '' != $query_vars['tb'] ) { 1024 $this->is_trackback = true; 1025 } 1026 1027 if ( '' != $query_vars['paged'] && ( (int) $query_vars['paged'] > 1 ) ) { 1028 $this->is_paged = true; 1029 } 1030 1031 // If we're previewing inside the write screen. 1032 if ( '' != $query_vars['preview'] ) { 1033 $this->is_preview = true; 1034 } 1035 1036 if ( is_admin() ) { 1037 $this->is_admin = true; 1038 } 1039 1040 if ( str_contains( $query_vars['feed'], 'comments-' ) ) { 1041 $query_vars['feed'] = str_replace( 'comments-', '', $query_vars['feed'] ); 1042 $query_vars['withcomments'] = 1; 1043 } 1044 1045 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; 1046 1047 if ( $this->is_feed && ( ! empty( $query_vars['withcomments'] ) || ( empty( $query_vars['withoutcomments'] ) && $this->is_singular ) ) ) { 1048 $this->is_comment_feed = true; 1049 } 1050 1051 if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed 1052 || ( wp_is_serving_rest_request() && $this->is_main_query() ) 1053 || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon || $this->is_sitemap ) ) { 1054 $this->is_home = true; 1055 } 1056 1057 // Correct `is_*` for 'page_on_front' and 'page_for_posts'. 1058 if ( $this->is_home && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) { 1059 $_query = wp_parse_args( $this->query ); 1060 // 'pagename' can be set and empty depending on matched rewrite rules. Ignore an empty 'pagename'. 1061 if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) { 1062 unset( $_query['pagename'] ); 1063 } 1064 1065 unset( $_query['embed'] ); 1066 1067 if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage' ) ) ) { 1068 $this->is_page = true; 1069 $this->is_home = false; 1070 $query_vars['page_id'] = get_option( 'page_on_front' ); 1071 // Correct <!--nextpage--> for 'page_on_front'. 1072 if ( ! empty( $query_vars['paged'] ) ) { 1073 $query_vars['page'] = $query_vars['paged']; 1074 unset( $query_vars['paged'] ); 1075 } 1076 } 1077 } 1078 1079 if ( '' !== $query_vars['pagename'] ) { 1080 $this->queried_object = get_page_by_path( $query_vars['pagename'] ); 1081 1082 if ( $this->queried_object && 'attachment' === $this->queried_object->post_type ) { 1083 if ( preg_match( '/^[^%]*%(?:postname)%/', get_option( 'permalink_structure' ) ) ) { 1084 // See if we also have a post with the same slug. 1085 $post = get_page_by_path( $query_vars['pagename'], OBJECT, 'post' ); 1086 if ( $post ) { 1087 $this->queried_object = $post; 1088 $this->is_page = false; 1089 $this->is_single = true; 1090 } 1091 } 1092 } 1093 1094 if ( ! empty( $this->queried_object ) ) { 1095 $this->queried_object_id = (int) $this->queried_object->ID; 1096 } else { 1097 unset( $this->queried_object ); 1098 } 1099 1100 if ( 'page' === get_option( 'show_on_front' ) && isset( $this->queried_object_id ) && get_option( 'page_for_posts' ) == $this->queried_object_id ) { 1101 $this->is_page = false; 1102 $this->is_home = true; 1103 $this->is_posts_page = true; 1104 } 1105 1106 if ( isset( $this->queried_object_id ) && get_option( 'wp_page_for_privacy_policy' ) == $this->queried_object_id ) { 1107 $this->is_privacy_policy = true; 1108 } 1109 } 1110 1111 if ( $query_vars['page_id'] ) { 1112 if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == $query_vars['page_id'] ) { 1113 $this->is_page = false; 1114 $this->is_home = true; 1115 $this->is_posts_page = true; 1116 } 1117 1118 if ( get_option( 'wp_page_for_privacy_policy' ) == $query_vars['page_id'] ) { 1119 $this->is_privacy_policy = true; 1120 } 1121 } 1122 1123 if ( ! empty( $query_vars['post_type'] ) ) { 1124 if ( is_array( $query_vars['post_type'] ) ) { 1125 $query_vars['post_type'] = array_map( 'sanitize_key', array_unique( $query_vars['post_type'] ) ); 1126 sort( $query_vars['post_type'] ); 1127 } else { 1128 $query_vars['post_type'] = sanitize_key( $query_vars['post_type'] ); 1129 } 1130 } 1131 1132 if ( ! empty( $query_vars['post_status'] ) ) { 1133 if ( is_array( $query_vars['post_status'] ) ) { 1134 $query_vars['post_status'] = array_map( 'sanitize_key', array_unique( $query_vars['post_status'] ) ); 1135 sort( $query_vars['post_status'] ); 1136 } else { 1137 $query_vars['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $query_vars['post_status'] ); 1138 } 1139 } 1140 1141 if ( $this->is_posts_page && ( ! isset( $query_vars['withcomments'] ) || ! $query_vars['withcomments'] ) ) { 1142 $this->is_comment_feed = false; 1143 } 1144 1145 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; 1146 // Done correcting `is_*` for 'page_on_front' and 'page_for_posts'. 1147 1148 if ( '404' == $query_vars['error'] ) { 1149 $this->set_404(); 1150 } 1151 1152 $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); 1153 1154 $this->query_vars_hash = md5( serialize( $this->query_vars ) ); 1155 $this->query_vars_changed = false; 1156 1157 /** 1158 * Fires after the main query vars have been parsed. 1159 * 1160 * @since 1.5.0 1161 * 1162 * @param WP_Query $query The WP_Query instance (passed by reference). 1163 */ 1164 do_action_ref_array( 'parse_query', array( &$this ) ); 1165 } 1166 1167 /** 1168 * Parses various taxonomy related query vars. 1169 * 1170 * For BC, this method is not marked as protected. See [28987]. 1171 * 1172 * @since 3.1.0 1173 * 1174 * @param array $query_vars The query variables. Passed by reference. 1175 */ 1176 public function parse_tax_query( &$query_vars ) { 1177 if ( ! empty( $query_vars['tax_query'] ) && is_array( $query_vars['tax_query'] ) ) { 1178 $tax_query = $query_vars['tax_query']; 1179 } else { 1180 $tax_query = array(); 1181 } 1182 1183 if ( ! empty( $query_vars['taxonomy'] ) && ! empty( $query_vars['term'] ) ) { 1184 $tax_query[] = array( 1185 'taxonomy' => $query_vars['taxonomy'], 1186 'terms' => array( $query_vars['term'] ), 1187 'field' => 'slug', 1188 ); 1189 } 1190 1191 foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { 1192 if ( 'post_tag' === $taxonomy ) { 1193 continue; // Handled further down in the $query_vars['tag'] block. 1194 } 1195 1196 if ( $t->query_var && ! empty( $query_vars[ $t->query_var ] ) ) { 1197 $tax_query_defaults = array( 1198 'taxonomy' => $taxonomy, 1199 'field' => 'slug', 1200 ); 1201 1202 if ( is_string( $query_vars[ $t->query_var ] ) && ! empty( $t->rewrite['hierarchical'] ) ) { 1203 $query_vars[ $t->query_var ] = wp_basename( $query_vars[ $t->query_var ] ); 1204 } 1205 1206 $term = $query_vars[ $t->query_var ]; 1207 1208 if ( ! is_array( $term ) ) { 1209 $term = explode( ',', $term ); 1210 $term = array_map( 'trim', $term ); 1211 } 1212 sort( $term ); 1213 $term = implode( ',', $term ); 1214 1215 if ( str_contains( $term, '+' ) ) { 1216 $terms = preg_split( '/[+]+/', $term ); 1217 foreach ( $terms as $term ) { 1218 $tax_query[] = array_merge( 1219 $tax_query_defaults, 1220 array( 1221 'terms' => array( $term ), 1222 ) 1223 ); 1224 } 1225 } else { 1226 $tax_query[] = array_merge( 1227 $tax_query_defaults, 1228 array( 1229 'terms' => preg_split( '/[,]+/', $term ), 1230 ) 1231 ); 1232 } 1233 } 1234 } 1235 1236 // If query string 'cat' is an array, implode it. 1237 if ( is_array( $query_vars['cat'] ) ) { 1238 $query_vars['cat'] = implode( ',', $query_vars['cat'] ); 1239 } 1240 1241 // Category stuff. 1242 1243 if ( ! empty( $query_vars['cat'] ) && ! $this->is_singular ) { 1244 $cat_in = array(); 1245 $cat_not_in = array(); 1246 1247 $cat_array = preg_split( '/[,\s]+/', urldecode( $query_vars['cat'] ) ); 1248 $cat_array = array_map( 'intval', $cat_array ); 1249 sort( $cat_array ); 1250 $query_vars['cat'] = implode( ',', $cat_array ); 1251 1252 foreach ( $cat_array as $cat ) { 1253 if ( $cat > 0 ) { 1254 $cat_in[] = $cat; 1255 } elseif ( $cat < 0 ) { 1256 $cat_not_in[] = abs( $cat ); 1257 } 1258 } 1259 1260 if ( ! empty( $cat_in ) ) { 1261 $tax_query[] = array( 1262 'taxonomy' => 'category', 1263 'terms' => $cat_in, 1264 'field' => 'term_id', 1265 'include_children' => true, 1266 ); 1267 } 1268 1269 if ( ! empty( $cat_not_in ) ) { 1270 $tax_query[] = array( 1271 'taxonomy' => 'category', 1272 'terms' => $cat_not_in, 1273 'field' => 'term_id', 1274 'operator' => 'NOT IN', 1275 'include_children' => true, 1276 ); 1277 } 1278 unset( $cat_array, $cat_in, $cat_not_in ); 1279 } 1280 1281 if ( ! empty( $query_vars['category__and'] ) && 1 === count( (array) $query_vars['category__and'] ) ) { 1282 $query_vars['category__and'] = (array) $query_vars['category__and']; 1283 if ( ! isset( $query_vars['category__in'] ) ) { 1284 $query_vars['category__in'] = array(); 1285 } 1286 $query_vars['category__in'][] = absint( reset( $query_vars['category__and'] ) ); 1287 unset( $query_vars['category__and'] ); 1288 } 1289 1290 if ( ! empty( $query_vars['category__in'] ) ) { 1291 $query_vars['category__in'] = array_map( 'absint', array_unique( (array) $query_vars['category__in'] ) ); 1292 sort( $query_vars['category__in'] ); 1293 $tax_query[] = array( 1294 'taxonomy' => 'category', 1295 'terms' => $query_vars['category__in'], 1296 'field' => 'term_id', 1297 'include_children' => false, 1298 ); 1299 } 1300 1301 if ( ! empty( $query_vars['category__not_in'] ) ) { 1302 $query_vars['category__not_in'] = array_map( 'absint', array_unique( (array) $query_vars['category__not_in'] ) ); 1303 sort( $query_vars['category__not_in'] ); 1304 $tax_query[] = array( 1305 'taxonomy' => 'category', 1306 'terms' => $query_vars['category__not_in'], 1307 'operator' => 'NOT IN', 1308 'include_children' => false, 1309 ); 1310 } 1311 1312 if ( ! empty( $query_vars['category__and'] ) ) { 1313 $query_vars['category__and'] = array_map( 'absint', array_unique( (array) $query_vars['category__and'] ) ); 1314 sort( $query_vars['category__and'] ); 1315 $tax_query[] = array( 1316 'taxonomy' => 'category', 1317 'terms' => $query_vars['category__and'], 1318 'field' => 'term_id', 1319 'operator' => 'AND', 1320 'include_children' => false, 1321 ); 1322 } 1323 1324 // If query string 'tag' is array, implode it. 1325 if ( is_array( $query_vars['tag'] ) ) { 1326 $query_vars['tag'] = implode( ',', $query_vars['tag'] ); 1327 } 1328 1329 // Tag stuff. 1330 1331 if ( '' !== $query_vars['tag'] && ! $this->is_singular && $this->query_vars_changed ) { 1332 if ( str_contains( $query_vars['tag'], ',' ) ) { 1333 // @todo Handle normalizing `tag` query string. 1334 $tags = preg_split( '/[,\r\n\t ]+/', $query_vars['tag'] ); 1335 foreach ( (array) $tags as $tag ) { 1336 $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); 1337 $query_vars['tag_slug__in'][] = $tag; 1338 sort( $query_vars['tag_slug__in'] ); 1339 } 1340 } elseif ( preg_match( '/[+\r\n\t ]+/', $query_vars['tag'] ) || ! empty( $query_vars['cat'] ) ) { 1341 $tags = preg_split( '/[+\r\n\t ]+/', $query_vars['tag'] ); 1342 foreach ( (array) $tags as $tag ) { 1343 $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); 1344 $query_vars['tag_slug__and'][] = $tag; 1345 } 1346 } else { 1347 $query_vars['tag'] = sanitize_term_field( 'slug', $query_vars['tag'], 0, 'post_tag', 'db' ); 1348 $query_vars['tag_slug__in'][] = $query_vars['tag']; 1349 sort( $query_vars['tag_slug__in'] ); 1350 } 1351 } 1352 1353 if ( ! empty( $query_vars['tag_id'] ) ) { 1354 $query_vars['tag_id'] = absint( $query_vars['tag_id'] ); 1355 $tax_query[] = array( 1356 'taxonomy' => 'post_tag', 1357 'terms' => $query_vars['tag_id'], 1358 ); 1359 } 1360 1361 if ( ! empty( $query_vars['tag__in'] ) ) { 1362 $query_vars['tag__in'] = array_map( 'absint', array_unique( (array) $query_vars['tag__in'] ) ); 1363 sort( $query_vars['tag__in'] ); 1364 $tax_query[] = array( 1365 'taxonomy' => 'post_tag', 1366 'terms' => $query_vars['tag__in'], 1367 ); 1368 } 1369 1370 if ( ! empty( $query_vars['tag__not_in'] ) ) { 1371 $query_vars['tag__not_in'] = array_map( 'absint', array_unique( (array) $query_vars['tag__not_in'] ) ); 1372 sort( $query_vars['tag__not_in'] ); 1373 $tax_query[] = array( 1374 'taxonomy' => 'post_tag', 1375 'terms' => $query_vars['tag__not_in'], 1376 'operator' => 'NOT IN', 1377 ); 1378 } 1379 1380 if ( ! empty( $query_vars['tag__and'] ) ) { 1381 $query_vars['tag__and'] = array_map( 'absint', array_unique( (array) $query_vars['tag__and'] ) ); 1382 sort( $query_vars['tag__and'] ); 1383 $tax_query[] = array( 1384 'taxonomy' => 'post_tag', 1385 'terms' => $query_vars['tag__and'], 1386 'operator' => 'AND', 1387 ); 1388 } 1389 1390 if ( ! empty( $query_vars['tag_slug__in'] ) ) { 1391 $query_vars['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $query_vars['tag_slug__in'] ) ); 1392 sort( $query_vars['tag_slug__in'] ); 1393 $tax_query[] = array( 1394 'taxonomy' => 'post_tag', 1395 'terms' => $query_vars['tag_slug__in'], 1396 'field' => 'slug', 1397 ); 1398 } 1399 1400 if ( ! empty( $query_vars['tag_slug__and'] ) ) { 1401 $query_vars['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $query_vars['tag_slug__and'] ) ); 1402 sort( $query_vars['tag_slug__and'] ); 1403 $tax_query[] = array( 1404 'taxonomy' => 'post_tag', 1405 'terms' => $query_vars['tag_slug__and'], 1406 'field' => 'slug', 1407 'operator' => 'AND', 1408 ); 1409 } 1410 1411 $this->tax_query = new WP_Tax_Query( $tax_query ); 1412 1413 /** 1414 * Fires after taxonomy-related query vars have been parsed. 1415 * 1416 * @since 3.7.0 1417 * 1418 * @param WP_Query $query The WP_Query instance. 1419 */ 1420 do_action( 'parse_tax_query', $this ); 1421 } 1422 1423 /** 1424 * Generates SQL for the WHERE clause based on passed search terms. 1425 * 1426 * @since 3.7.0 1427 * 1428 * @global wpdb $wpdb WordPress database abstraction object. 1429 * 1430 * @param array $query_vars Query variables. 1431 * @return string WHERE clause. 1432 */ 1433 protected function parse_search( &$query_vars ) { 1434 global $wpdb; 1435 1436 $search = ''; 1437 1438 // Added slashes screw with quote grouping when done early, so done later. 1439 $query_vars['s'] = stripslashes( $query_vars['s'] ); 1440 if ( empty( $_GET['s'] ) && $this->is_main_query() ) { 1441 $query_vars['s'] = urldecode( $query_vars['s'] ); 1442 } 1443 // There are no line breaks in <input /> fields. 1444 $query_vars['s'] = str_replace( array( "\r", "\n" ), '', $query_vars['s'] ); 1445 $query_vars['search_terms_count'] = 1; 1446 if ( ! empty( $query_vars['sentence'] ) ) { 1447 $query_vars['search_terms'] = array( $query_vars['s'] ); 1448 } else { 1449 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $query_vars['s'], $matches ) ) { 1450 $query_vars['search_terms_count'] = count( $matches[0] ); 1451 $query_vars['search_terms'] = $this->parse_search_terms( $matches[0] ); 1452 // If the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence. 1453 if ( empty( $query_vars['search_terms'] ) || count( $query_vars['search_terms'] ) > 9 ) { 1454 $query_vars['search_terms'] = array( $query_vars['s'] ); 1455 } 1456 } else { 1457 $query_vars['search_terms'] = array( $query_vars['s'] ); 1458 } 1459 } 1460 1461 $n = ! empty( $query_vars['exact'] ) ? '' : '%'; 1462 $searchand = ''; 1463 $query_vars['search_orderby_title'] = array(); 1464 1465 $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' ); 1466 $search_columns = ! empty( $query_vars['search_columns'] ) ? $query_vars['search_columns'] : $default_search_columns; 1467 if ( ! is_array( $search_columns ) ) { 1468 $search_columns = array( $search_columns ); 1469 } 1470 1471 /** 1472 * Filters the columns to search in a WP_Query search. 1473 * 1474 * The supported columns are `post_title`, `post_excerpt` and `post_content`. 1475 * They are all included by default. 1476 * 1477 * @since 6.2.0 1478 * 1479 * @param string[] $search_columns Array of column names to be searched. 1480 * @param string $search Text being searched. 1481 * @param WP_Query $query The current WP_Query instance. 1482 */ 1483 $search_columns = (array) apply_filters( 'post_search_columns', $search_columns, $query_vars['s'], $this ); 1484 1485 // Use only supported search columns. 1486 $search_columns = array_intersect( $search_columns, $default_search_columns ); 1487 if ( empty( $search_columns ) ) { 1488 $search_columns = $default_search_columns; 1489 } 1490 1491 /** 1492 * Filters the prefix that indicates that a search term should be excluded from results. 1493 * 1494 * @since 4.7.0 1495 * 1496 * @param string $exclusion_prefix The prefix. Default '-'. Returning 1497 * an empty value disables exclusions. 1498 */ 1499 $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); 1500 1501 foreach ( $query_vars['search_terms'] as $term ) { 1502 // If there is an $exclusion_prefix, terms prefixed with it should be excluded. 1503 $exclude = $exclusion_prefix && str_starts_with( $term, $exclusion_prefix ); 1504 if ( $exclude ) { 1505 $like_op = 'NOT LIKE'; 1506 $andor_op = 'AND'; 1507 $term = substr( $term, 1 ); 1508 } else { 1509 $like_op = 'LIKE'; 1510 $andor_op = 'OR'; 1511 } 1512 1513 if ( $n && ! $exclude ) { 1514 $like = '%' . $wpdb->esc_like( $term ) . '%'; 1515 $query_vars['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); 1516 } 1517 1518 $like = $n . $wpdb->esc_like( $term ) . $n; 1519 1520 $search_columns_parts = array(); 1521 foreach ( $search_columns as $search_column ) { 1522 $search_columns_parts[ $search_column ] = $wpdb->prepare( "({$wpdb->posts}.$search_column $like_op %s)", $like ); 1523 } 1524 1525 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 1526 $search_columns_parts['attachment'] = $wpdb->prepare( "(sq1.meta_value $like_op %s)", $like ); 1527 } 1528 1529 $search .= "$searchand(" . implode( " $andor_op ", $search_columns_parts ) . ')'; 1530 1531 $searchand = ' AND '; 1532 } 1533 1534 if ( ! empty( $search ) ) { 1535 $search = " AND ({$search}) "; 1536 if ( ! is_user_logged_in() ) { 1537 $search .= " AND ({$wpdb->posts}.post_password = '') "; 1538 } 1539 } 1540 1541 return $search; 1542 } 1543 1544 /** 1545 * Checks if the terms are suitable for searching. 1546 * 1547 * Uses an array of stopwords (terms) that are excluded from the separate 1548 * term matching when searching for posts. The list of English stopwords is 1549 * the approximate search engines list, and is translatable. 1550 * 1551 * @since 3.7.0 1552 * 1553 * @param string[] $terms Array of terms to check. 1554 * @return string[] Terms that are not stopwords. 1555 */ 1556 protected function parse_search_terms( $terms ) { 1557 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; 1558 $checked = array(); 1559 1560 $stopwords = $this->get_search_stopwords(); 1561 1562 foreach ( $terms as $term ) { 1563 // Keep before/after spaces when term is for exact match. 1564 if ( preg_match( '/^".+"$/', $term ) ) { 1565 $term = trim( $term, "\"'" ); 1566 } else { 1567 $term = trim( $term, "\"' " ); 1568 } 1569 1570 // Avoid single A-Z and single dashes. 1571 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) { 1572 continue; 1573 } 1574 1575 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) { 1576 continue; 1577 } 1578 1579 $checked[] = $term; 1580 } 1581 1582 return $checked; 1583 } 1584 1585 /** 1586 * Retrieves stopwords used when parsing search terms. 1587 * 1588 * @since 3.7.0 1589 * 1590 * @return string[] Stopwords. 1591 */ 1592 protected function get_search_stopwords() { 1593 if ( isset( $this->stopwords ) ) { 1594 return $this->stopwords; 1595 } 1596 1597 /* 1598 * translators: This is a comma-separated list of very common words that should be excluded from a search, 1599 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual 1600 * words into your language. Instead, look for and provide commonly accepted stopwords in your language. 1601 */ 1602 $words = explode( 1603 ',', 1604 _x( 1605 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www', 1606 'Comma-separated list of search stopwords in your language' 1607 ) 1608 ); 1609 1610 $stopwords = array(); 1611 foreach ( $words as $word ) { 1612 $word = trim( $word, "\r\n\t " ); 1613 if ( $word ) { 1614 $stopwords[] = $word; 1615 } 1616 } 1617 1618 /** 1619 * Filters stopwords used when parsing search terms. 1620 * 1621 * @since 3.7.0 1622 * 1623 * @param string[] $stopwords Array of stopwords. 1624 */ 1625 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); 1626 return $this->stopwords; 1627 } 1628 1629 /** 1630 * Generates SQL for the ORDER BY condition based on passed search terms. 1631 * 1632 * @since 3.7.0 1633 * 1634 * @global wpdb $wpdb WordPress database abstraction object. 1635 * 1636 * @param array $query_vars Query variables. 1637 * @return string ORDER BY clause. 1638 */ 1639 protected function parse_search_order( &$query_vars ) { 1640 global $wpdb; 1641 1642 if ( $query_vars['search_terms_count'] > 1 ) { 1643 $num_terms = count( $query_vars['search_orderby_title'] ); 1644 1645 // If the search terms contain negative queries, don't bother ordering by sentence matches. 1646 $like = ''; 1647 if ( ! preg_match( '/(?:\s|^)\-/', $query_vars['s'] ) ) { 1648 $like = '%' . $wpdb->esc_like( $query_vars['s'] ) . '%'; 1649 } 1650 1651 $search_orderby = ''; 1652 1653 // Sentence match in 'post_title'. 1654 if ( $like ) { 1655 $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like ); 1656 } 1657 1658 /* 1659 * Sanity limit, sort as sentence when more than 6 terms 1660 * (few searches are longer than 6 terms and most titles are not). 1661 */ 1662 if ( $num_terms < 7 ) { 1663 // All words in title. 1664 $search_orderby .= 'WHEN ' . implode( ' AND ', $query_vars['search_orderby_title'] ) . ' THEN 2 '; 1665 // Any word in title, not needed when $num_terms == 1. 1666 if ( $num_terms > 1 ) { 1667 $search_orderby .= 'WHEN ' . implode( ' OR ', $query_vars['search_orderby_title'] ) . ' THEN 3 '; 1668 } 1669 } 1670 1671 // Sentence match in 'post_content' and 'post_excerpt'. 1672 if ( $like ) { 1673 $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); 1674 $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); 1675 } 1676 1677 if ( $search_orderby ) { 1678 $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; 1679 } 1680 } else { 1681 // Single word or sentence search. 1682 $search_orderby = reset( $query_vars['search_orderby_title'] ) . ' DESC'; 1683 } 1684 1685 return $search_orderby; 1686 } 1687 1688 /** 1689 * Converts the given orderby alias (if allowed) to a properly-prefixed value. 1690 * 1691 * @since 4.0.0 1692 * 1693 * @global wpdb $wpdb WordPress database abstraction object. 1694 * 1695 * @param string $orderby Alias for the field to order by. 1696 * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise. 1697 */ 1698 protected function parse_orderby( $orderby ) { 1699 global $wpdb; 1700 1701 // Used to filter values. 1702 $allowed_keys = array( 1703 'post_name', 1704 'post_author', 1705 'post_date', 1706 'post_title', 1707 'post_modified', 1708 'post_parent', 1709 'post_type', 1710 'name', 1711 'author', 1712 'date', 1713 'title', 1714 'modified', 1715 'parent', 1716 'type', 1717 'ID', 1718 'menu_order', 1719 'comment_count', 1720 'rand', 1721 'post__in', 1722 'post_parent__in', 1723 'post_name__in', 1724 ); 1725 1726 $primary_meta_key = ''; 1727 $primary_meta_query = false; 1728 $meta_clauses = $this->meta_query->get_clauses(); 1729 if ( ! empty( $meta_clauses ) ) { 1730 $primary_meta_query = reset( $meta_clauses ); 1731 1732 if ( ! empty( $primary_meta_query['key'] ) ) { 1733 $primary_meta_key = $primary_meta_query['key']; 1734 $allowed_keys[] = $primary_meta_key; 1735 } 1736 1737 $allowed_keys[] = 'meta_value'; 1738 $allowed_keys[] = 'meta_value_num'; 1739 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); 1740 } 1741 1742 // If RAND() contains a seed value, sanitize and add to allowed keys. 1743 $rand_with_seed = false; 1744 if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { 1745 $orderby = sprintf( 'RAND(%s)', (int) $matches[1] ); 1746 $allowed_keys[] = $orderby; 1747 $rand_with_seed = true; 1748 } 1749 1750 if ( ! in_array( $orderby, $allowed_keys, true ) ) { 1751 return false; 1752 } 1753 1754 $orderby_clause = ''; 1755 1756 switch ( $orderby ) { 1757 case 'post_name': 1758 case 'post_author': 1759 case 'post_date': 1760 case 'post_title': 1761 case 'post_modified': 1762 case 'post_parent': 1763 case 'post_type': 1764 case 'ID': 1765 case 'menu_order': 1766 case 'comment_count': 1767 $orderby_clause = "{$wpdb->posts}.{$orderby}"; 1768 break; 1769 case 'rand': 1770 $orderby_clause = 'RAND()'; 1771 break; 1772 case $primary_meta_key: 1773 case 'meta_value': 1774 if ( ! empty( $primary_meta_query['type'] ) ) { 1775 $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; 1776 } else { 1777 $orderby_clause = "{$primary_meta_query['alias']}.meta_value"; 1778 } 1779 break; 1780 case 'meta_value_num': 1781 $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; 1782 break; 1783 case 'post__in': 1784 if ( ! empty( $this->query_vars['post__in'] ) ) { 1785 $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; 1786 } 1787 break; 1788 case 'post_parent__in': 1789 if ( ! empty( $this->query_vars['post_parent__in'] ) ) { 1790 $orderby_clause = "FIELD( {$wpdb->posts}.post_parent," . implode( ', ', array_map( 'absint', $this->query_vars['post_parent__in'] ) ) . ' )'; 1791 } 1792 break; 1793 case 'post_name__in': 1794 if ( ! empty( $this->query_vars['post_name__in'] ) ) { 1795 $post_name__in = array_map( 'sanitize_title_for_query', $this->query_vars['post_name__in'] ); 1796 $post_name__in_string = "'" . implode( "','", $post_name__in ) . "'"; 1797 $orderby_clause = "FIELD( {$wpdb->posts}.post_name," . $post_name__in_string . ' )'; 1798 } 1799 break; 1800 default: 1801 if ( array_key_exists( $orderby, $meta_clauses ) ) { 1802 // $orderby corresponds to a meta_query clause. 1803 $meta_clause = $meta_clauses[ $orderby ]; 1804 $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; 1805 } elseif ( $rand_with_seed ) { 1806 $orderby_clause = $orderby; 1807 } else { 1808 // Default: order by post field. 1809 $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby ); 1810 } 1811 1812 break; 1813 } 1814 1815 return $orderby_clause; 1816 } 1817 1818 /** 1819 * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 1820 * 1821 * @since 4.0.0 1822 * 1823 * @param string $order The 'order' query variable. 1824 * @return string The sanitized 'order' query variable. 1825 */ 1826 protected function parse_order( $order ) { 1827 if ( ! is_string( $order ) || empty( $order ) ) { 1828 return 'DESC'; 1829 } 1830 1831 if ( 'ASC' === strtoupper( $order ) ) { 1832 return 'ASC'; 1833 } else { 1834 return 'DESC'; 1835 } 1836 } 1837 1838 /** 1839 * Sets the 404 property and saves whether query is feed. 1840 * 1841 * @since 2.0.0 1842 */ 1843 public function set_404() { 1844 $is_feed = $this->is_feed; 1845 1846 $this->init_query_flags(); 1847 $this->is_404 = true; 1848 1849 $this->is_feed = $is_feed; 1850 1851 /** 1852 * Fires after a 404 is triggered. 1853 * 1854 * @since 5.5.0 1855 * 1856 * @param WP_Query $query The WP_Query instance (passed by reference). 1857 */ 1858 do_action_ref_array( 'set_404', array( $this ) ); 1859 } 1860 1861 /** 1862 * Retrieves the value of a query variable. 1863 * 1864 * @since 1.5.0 1865 * @since 3.9.0 The `$default_value` argument was introduced. 1866 * 1867 * @param string $query_var Query variable key. 1868 * @param mixed $default_value Optional. Value to return if the query variable is not set. 1869 * Default empty string. 1870 * @return mixed Contents of the query variable. 1871 */ 1872 public function get( $query_var, $default_value = '' ) { 1873 return $this->query_vars[ $query_var ] ?? $default_value; 1874 } 1875 1876 /** 1877 * Sets the value of a query variable. 1878 * 1879 * @since 1.5.0 1880 * 1881 * @param string $query_var Query variable key. 1882 * @param mixed $value Query variable value. 1883 */ 1884 public function set( $query_var, $value ) { 1885 $this->query_vars[ $query_var ] = $value; 1886 } 1887 1888 /** 1889 * Retrieves an array of posts based on query variables. 1890 * 1891 * There are a few filters and actions that can be used to modify the post 1892 * database query. 1893 * 1894 * @since 1.5.0 1895 * 1896 * @global wpdb $wpdb WordPress database abstraction object. 1897 * 1898 * @return WP_Post[]|int[] Array of post objects or post IDs. 1899 */ 1900 public function get_posts() { 1901 global $wpdb; 1902 1903 $this->parse_query(); 1904 1905 /** 1906 * Fires after the query variable object is created, but before the actual query is run. 1907 * 1908 * Note: If using conditional tags, use the method versions within the passed instance 1909 * (e.g. `$query->is_main_query()` instead of `is_main_query()`). This is because the functions 1910 * like `is_main_query()` test against the global `$wp_query` instance, not the passed one. 1911 * 1912 * @since 2.0.0 1913 * 1914 * @param WP_Query $query The WP_Query instance (passed by reference). 1915 */ 1916 do_action_ref_array( 'pre_get_posts', array( &$this ) ); 1917 1918 // Locally scoped reference for easy of use. 1919 $query_vars = &$this->query_vars; 1920 1921 // Fill again in case 'pre_get_posts' unset some vars. 1922 $query_vars = $this->fill_query_vars( $query_vars ); 1923 1924 /** 1925 * Filters whether an attachment query should include filenames or not. 1926 * 1927 * @since 6.0.3 1928 * 1929 * @param bool $allow_query_attachment_by_filename Whether or not to include filenames. 1930 */ 1931 $this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false ); 1932 remove_all_filters( 'wp_allow_query_attachment_by_filename' ); 1933 1934 // Parse meta query. 1935 $this->meta_query = new WP_Meta_Query(); 1936 $this->meta_query->parse_query_vars( $query_vars ); 1937 1938 // Set a flag if a 'pre_get_posts' hook changed the query vars. 1939 $hash = md5( serialize( $this->query_vars ) ); 1940 if ( $hash !== $this->query_vars_hash ) { 1941 $this->query_vars_changed = true; 1942 $this->query_vars_hash = $hash; 1943 } 1944 unset( $hash ); 1945 1946 // First let's clear some variables. 1947 $distinct = ''; 1948 $whichauthor = ''; 1949 $whichmimetype = ''; 1950 $where = ''; 1951 $limits = ''; 1952 $join = ''; 1953 $search = ''; 1954 $groupby = ''; 1955 $post_status_join = false; 1956 $page = 1; 1957 1958 if ( isset( $query_vars['caller_get_posts'] ) ) { 1959 _deprecated_argument( 1960 'WP_Query', 1961 '3.1.0', 1962 sprintf( 1963 /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */ 1964 __( '%1$s is deprecated. Use %2$s instead.' ), 1965 '<code>caller_get_posts</code>', 1966 '<code>ignore_sticky_posts</code>' 1967 ) 1968 ); 1969 1970 if ( ! isset( $query_vars['ignore_sticky_posts'] ) ) { 1971 $query_vars['ignore_sticky_posts'] = $query_vars['caller_get_posts']; 1972 } 1973 } 1974 1975 if ( ! isset( $query_vars['ignore_sticky_posts'] ) ) { 1976 $query_vars['ignore_sticky_posts'] = false; 1977 } 1978 1979 if ( ! isset( $query_vars['suppress_filters'] ) ) { 1980 $query_vars['suppress_filters'] = false; 1981 } 1982 1983 if ( ! isset( $query_vars['cache_results'] ) ) { 1984 $query_vars['cache_results'] = true; 1985 } 1986 1987 if ( ! isset( $query_vars['update_post_term_cache'] ) ) { 1988 $query_vars['update_post_term_cache'] = true; 1989 } 1990 1991 if ( ! isset( $query_vars['update_menu_item_cache'] ) ) { 1992 $query_vars['update_menu_item_cache'] = false; 1993 } 1994 1995 if ( ! isset( $query_vars['lazy_load_term_meta'] ) ) { 1996 $query_vars['lazy_load_term_meta'] = $query_vars['update_post_term_cache']; 1997 } elseif ( $query_vars['lazy_load_term_meta'] ) { // Lazy loading term meta only works if term caches are primed. 1998 $query_vars['update_post_term_cache'] = true; 1999 } 2000 2001 if ( ! isset( $query_vars['update_post_meta_cache'] ) ) { 2002 $query_vars['update_post_meta_cache'] = true; 2003 } 2004 2005 if ( ! isset( $query_vars['post_type'] ) ) { 2006 if ( $this->is_search ) { 2007 $query_vars['post_type'] = 'any'; 2008 } else { 2009 $query_vars['post_type'] = ''; 2010 } 2011 } 2012 $post_type = $query_vars['post_type']; 2013 if ( empty( $query_vars['posts_per_page'] ) ) { 2014 $query_vars['posts_per_page'] = get_option( 'posts_per_page' ); 2015 } 2016 if ( isset( $query_vars['showposts'] ) && $query_vars['showposts'] ) { 2017 $query_vars['showposts'] = (int) $query_vars['showposts']; 2018 $query_vars['posts_per_page'] = $query_vars['showposts']; 2019 } 2020 if ( ( isset( $query_vars['posts_per_archive_page'] ) && 0 != $query_vars['posts_per_archive_page'] ) && ( $this->is_archive || $this->is_search ) ) { 2021 $query_vars['posts_per_page'] = $query_vars['posts_per_archive_page']; 2022 } 2023 if ( ! isset( $query_vars['nopaging'] ) ) { 2024 if ( -1 == $query_vars['posts_per_page'] ) { 2025 $query_vars['nopaging'] = true; 2026 } else { 2027 $query_vars['nopaging'] = false; 2028 } 2029 } 2030 2031 if ( $this->is_feed ) { 2032 // This overrides 'posts_per_page'. 2033 if ( ! empty( $query_vars['posts_per_rss'] ) ) { 2034 $query_vars['posts_per_page'] = $query_vars['posts_per_rss']; 2035 } else { 2036 $query_vars['posts_per_page'] = get_option( 'posts_per_rss' ); 2037 } 2038 $query_vars['nopaging'] = false; 2039 } 2040 2041 $query_vars['posts_per_page'] = (int) $query_vars['posts_per_page']; 2042 if ( $query_vars['posts_per_page'] < -1 ) { 2043 $query_vars['posts_per_page'] = abs( $query_vars['posts_per_page'] ); 2044 } elseif ( 0 === $query_vars['posts_per_page'] ) { 2045 $query_vars['posts_per_page'] = 1; 2046 } 2047 2048 if ( ! isset( $query_vars['comments_per_page'] ) || 0 == $query_vars['comments_per_page'] ) { 2049 $query_vars['comments_per_page'] = get_option( 'comments_per_page' ); 2050 } 2051 2052 if ( $this->is_home && ( empty( $this->query ) || 'true' === $query_vars['preview'] ) && ( 'page' === get_option( 'show_on_front' ) ) && get_option( 'page_on_front' ) ) { 2053 $this->is_page = true; 2054 $this->is_home = false; 2055 $query_vars['page_id'] = get_option( 'page_on_front' ); 2056 } 2057 2058 if ( isset( $query_vars['page'] ) ) { 2059 $query_vars['page'] = is_scalar( $query_vars['page'] ) ? absint( trim( $query_vars['page'], '/' ) ) : 0; 2060 } 2061 2062 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. 2063 if ( isset( $query_vars['no_found_rows'] ) ) { 2064 $query_vars['no_found_rows'] = (bool) $query_vars['no_found_rows']; 2065 } else { 2066 $query_vars['no_found_rows'] = false; 2067 } 2068 2069 switch ( $query_vars['fields'] ) { 2070 case 'ids': 2071 $fields = "{$wpdb->posts}.ID"; 2072 break; 2073 case 'id=>parent': 2074 $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; 2075 break; 2076 case '': 2077 /* 2078 * Set the default to 'all'. 2079 * 2080 * This is used in `WP_Query::the_post` to determine if the 2081 * entire post object has been queried. 2082 */ 2083 $query_vars['fields'] = 'all'; 2084 // Falls through. 2085 default: 2086 $fields = "{$wpdb->posts}.*"; 2087 } 2088 2089 if ( '' !== $query_vars['menu_order'] ) { 2090 $where .= " AND {$wpdb->posts}.menu_order = " . $query_vars['menu_order']; 2091 } 2092 // The "m" parameter is meant for months but accepts datetimes of varying specificity. 2093 if ( $query_vars['m'] ) { 2094 $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 0, 4 ); 2095 if ( strlen( $query_vars['m'] ) > 5 ) { 2096 $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 4, 2 ); 2097 } 2098 if ( strlen( $query_vars['m'] ) > 7 ) { 2099 $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 6, 2 ); 2100 } 2101 if ( strlen( $query_vars['m'] ) > 9 ) { 2102 $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 8, 2 ); 2103 } 2104 if ( strlen( $query_vars['m'] ) > 11 ) { 2105 $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 10, 2 ); 2106 } 2107 if ( strlen( $query_vars['m'] ) > 13 ) { 2108 $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr( $query_vars['m'], 12, 2 ); 2109 } 2110 } 2111 2112 // Handle the other individual date parameters. 2113 $date_parameters = array(); 2114 2115 if ( '' !== $query_vars['hour'] ) { 2116 $date_parameters['hour'] = $query_vars['hour']; 2117 } 2118 2119 if ( '' !== $query_vars['minute'] ) { 2120 $date_parameters['minute'] = $query_vars['minute']; 2121 } 2122 2123 if ( '' !== $query_vars['second'] ) { 2124 $date_parameters['second'] = $query_vars['second']; 2125 } 2126 2127 if ( $query_vars['year'] ) { 2128 $date_parameters['year'] = $query_vars['year']; 2129 } 2130 2131 if ( $query_vars['monthnum'] ) { 2132 $date_parameters['monthnum'] = $query_vars['monthnum']; 2133 } 2134 2135 if ( $query_vars['w'] ) { 2136 $date_parameters['week'] = $query_vars['w']; 2137 } 2138 2139 if ( $query_vars['day'] ) { 2140 $date_parameters['day'] = $query_vars['day']; 2141 } 2142 2143 if ( $date_parameters ) { 2144 $date_query = new WP_Date_Query( array( $date_parameters ) ); 2145 $where .= $date_query->get_sql(); 2146 } 2147 unset( $date_parameters, $date_query ); 2148 2149 // Handle complex date queries. 2150 if ( ! empty( $query_vars['date_query'] ) ) { 2151 $this->date_query = new WP_Date_Query( $query_vars['date_query'] ); 2152 $where .= $this->date_query->get_sql(); 2153 } 2154 2155 // If we've got a post_type AND it's not "any" post_type. 2156 if ( ! empty( $query_vars['post_type'] ) && 'any' !== $query_vars['post_type'] ) { 2157 foreach ( (array) $query_vars['post_type'] as $_post_type ) { 2158 $ptype_obj = get_post_type_object( $_post_type ); 2159 if ( ! $ptype_obj || ! $ptype_obj->query_var || empty( $query_vars[ $ptype_obj->query_var ] ) ) { 2160 continue; 2161 } 2162 2163 if ( ! $ptype_obj->hierarchical ) { 2164 // Non-hierarchical post types can directly use 'name'. 2165 $query_vars['name'] = $query_vars[ $ptype_obj->query_var ]; 2166 } else { 2167 // Hierarchical post types will operate through 'pagename'. 2168 $query_vars['pagename'] = $query_vars[ $ptype_obj->query_var ]; 2169 $query_vars['name'] = ''; 2170 } 2171 2172 // Only one request for a slug is possible, this is why name & pagename are overwritten above. 2173 break; 2174 } // End foreach. 2175 unset( $ptype_obj ); 2176 } 2177 2178 if ( '' !== $query_vars['title'] ) { 2179 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $query_vars['title'] ) ); 2180 } 2181 2182 // Parameters related to 'post_name'. 2183 if ( '' !== $query_vars['name'] ) { 2184 $query_vars['name'] = sanitize_title_for_query( $query_vars['name'] ); 2185 $where .= " AND {$wpdb->posts}.post_name = '" . $query_vars['name'] . "'"; 2186 } elseif ( '' !== $query_vars['pagename'] ) { 2187 if ( isset( $this->queried_object_id ) ) { 2188 $reqpage = $this->queried_object_id; 2189 } else { 2190 if ( 'page' !== $query_vars['post_type'] ) { 2191 foreach ( (array) $query_vars['post_type'] as $_post_type ) { 2192 $ptype_obj = get_post_type_object( $_post_type ); 2193 if ( ! $ptype_obj || ! $ptype_obj->hierarchical ) { 2194 continue; 2195 } 2196 2197 $reqpage = get_page_by_path( $query_vars['pagename'], OBJECT, $_post_type ); 2198 if ( $reqpage ) { 2199 break; 2200 } 2201 } 2202 unset( $ptype_obj ); 2203 } else { 2204 $reqpage = get_page_by_path( $query_vars['pagename'] ); 2205 } 2206 if ( ! empty( $reqpage ) ) { 2207 $reqpage = $reqpage->ID; 2208 } else { 2209 $reqpage = 0; 2210 } 2211 } 2212 2213 $page_for_posts = get_option( 'page_for_posts' ); 2214 if ( ( 'page' !== get_option( 'show_on_front' ) ) || empty( $page_for_posts ) || ( $reqpage != $page_for_posts ) ) { 2215 $query_vars['pagename'] = sanitize_title_for_query( wp_basename( $query_vars['pagename'] ) ); 2216 $query_vars['name'] = $query_vars['pagename']; 2217 $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; 2218 $reqpage_obj = get_post( $reqpage ); 2219 if ( is_object( $reqpage_obj ) && 'attachment' === $reqpage_obj->post_type ) { 2220 $this->is_attachment = true; 2221 $post_type = 'attachment'; 2222 $query_vars['post_type'] = 'attachment'; 2223 $this->is_page = true; 2224 $query_vars['attachment_id'] = $reqpage; 2225 } 2226 } 2227 } elseif ( '' !== $query_vars['attachment'] ) { 2228 $query_vars['attachment'] = sanitize_title_for_query( wp_basename( $query_vars['attachment'] ) ); 2229 $query_vars['name'] = $query_vars['attachment']; 2230 $where .= " AND {$wpdb->posts}.post_name = '" . $query_vars['attachment'] . "'"; 2231 } elseif ( is_array( $query_vars['post_name__in'] ) && ! empty( $query_vars['post_name__in'] ) ) { 2232 $query_vars['post_name__in'] = array_map( 'sanitize_title_for_query', $query_vars['post_name__in'] ); 2233 // Duplicate array before sorting to allow for the orderby clause. 2234 $post_name__in_for_where = array_unique( $query_vars['post_name__in'] ); 2235 sort( $post_name__in_for_where ); 2236 $post_name__in = "'" . implode( "','", $post_name__in_for_where ) . "'"; 2237 $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; 2238 } 2239 2240 // If an attachment is requested by number, let it supersede any post number. 2241 if ( $query_vars['attachment_id'] ) { 2242 $query_vars['p'] = absint( $query_vars['attachment_id'] ); 2243 } 2244 2245 // If a post number is specified, load that post. 2246 if ( $query_vars['p'] ) { 2247 $where .= " AND {$wpdb->posts}.ID = " . $query_vars['p']; 2248 } elseif ( $query_vars['post__in'] ) { 2249 // Duplicate array before sorting to allow for the orderby clause. 2250 $post__in_for_where = $query_vars['post__in']; 2251 $post__in_for_where = array_unique( array_map( 'absint', $post__in_for_where ) ); 2252 sort( $post__in_for_where ); 2253 $post__in = implode( ',', array_map( 'absint', $post__in_for_where ) ); 2254 $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; 2255 } elseif ( $query_vars['post__not_in'] ) { 2256 sort( $query_vars['post__not_in'] ); 2257 $post__not_in = implode( ',', array_map( 'absint', $query_vars['post__not_in'] ) ); 2258 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; 2259 } 2260 2261 if ( is_numeric( $query_vars['post_parent'] ) ) { 2262 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $query_vars['post_parent'] ); 2263 } elseif ( $query_vars['post_parent__in'] ) { 2264 // Duplicate array before sorting to allow for the orderby clause. 2265 $post_parent__in_for_where = $query_vars['post_parent__in']; 2266 $post_parent__in_for_where = array_unique( array_map( 'absint', $post_parent__in_for_where ) ); 2267 sort( $post_parent__in_for_where ); 2268 $post_parent__in = implode( ',', array_map( 'absint', $post_parent__in_for_where ) ); 2269 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; 2270 } elseif ( $query_vars['post_parent__not_in'] ) { 2271 sort( $query_vars['post_parent__not_in'] ); 2272 $post_parent__not_in = implode( ',', array_map( 'absint', $query_vars['post_parent__not_in'] ) ); 2273 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; 2274 } 2275 2276 if ( $query_vars['page_id'] ) { 2277 if ( ( 'page' !== get_option( 'show_on_front' ) ) || ( get_option( 'page_for_posts' ) != $query_vars['page_id'] ) ) { 2278 $query_vars['p'] = $query_vars['page_id']; 2279 $where = " AND {$wpdb->posts}.ID = " . $query_vars['page_id']; 2280 } 2281 } 2282 2283 // If a search pattern is specified, load the posts that match. 2284 if ( strlen( $query_vars['s'] ) ) { 2285 $search = $this->parse_search( $query_vars ); 2286 } 2287 2288 if ( ! $query_vars['suppress_filters'] ) { 2289 /** 2290 * Filters the search SQL that is used in the WHERE clause of WP_Query. 2291 * 2292 * @since 3.0.0 2293 * 2294 * @param string $search Search SQL for WHERE clause. 2295 * @param WP_Query $query The current WP_Query object. 2296 */ 2297 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); 2298 } 2299 2300 // Taxonomies. 2301 if ( ! $this->is_singular ) { 2302 $this->parse_tax_query( $query_vars ); 2303 2304 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); 2305 2306 $join .= $clauses['join']; 2307 $where .= $clauses['where']; 2308 } 2309 2310 if ( $this->is_tax ) { 2311 if ( empty( $post_type ) ) { 2312 // Do a fully inclusive search for currently registered post types of queried taxonomies. 2313 $post_type = array(); 2314 $taxonomies = array_keys( $this->tax_query->queried_terms ); 2315 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { 2316 $object_taxonomies = 'attachment' === $pt ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); 2317 if ( array_intersect( $taxonomies, $object_taxonomies ) ) { 2318 $post_type[] = $pt; 2319 } 2320 } 2321 if ( ! $post_type ) { 2322 $post_type = 'any'; 2323 } elseif ( count( $post_type ) === 1 ) { 2324 $post_type = $post_type[0]; 2325 } else { 2326 // Sort post types to ensure same cache key generation. 2327 sort( $post_type ); 2328 } 2329 2330 $post_status_join = true; 2331 } elseif ( in_array( 'attachment', (array) $post_type, true ) ) { 2332 $post_status_join = true; 2333 } 2334 } 2335 2336 /* 2337 * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and 2338 * 'category_name' vars are set for backward compatibility. 2339 */ 2340 if ( ! empty( $this->tax_query->queried_terms ) ) { 2341 2342 /* 2343 * Set 'taxonomy', 'term', and 'term_id' to the 2344 * first taxonomy other than 'post_tag' or 'category'. 2345 */ 2346 if ( ! isset( $query_vars['taxonomy'] ) ) { 2347 foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { 2348 if ( empty( $queried_items['terms'][0] ) ) { 2349 continue; 2350 } 2351 2352 if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ), true ) ) { 2353 $query_vars['taxonomy'] = $queried_taxonomy; 2354 2355 if ( 'slug' === $queried_items['field'] ) { 2356 $query_vars['term'] = $queried_items['terms'][0]; 2357 } else { 2358 $query_vars['term_id'] = $queried_items['terms'][0]; 2359 } 2360 2361 // Take the first one we find. 2362 break; 2363 } 2364 } 2365 } 2366 2367 // 'cat', 'category_name', 'tag_id'. 2368 foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { 2369 if ( empty( $queried_items['terms'][0] ) ) { 2370 continue; 2371 } 2372 2373 if ( 'category' === $queried_taxonomy ) { 2374 $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); 2375 if ( $the_cat ) { 2376 $this->set( 'cat', $the_cat->term_id ); 2377 $this->set( 'category_name', $the_cat->slug ); 2378 } 2379 unset( $the_cat ); 2380 } 2381 2382 if ( 'post_tag' === $queried_taxonomy ) { 2383 $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); 2384 if ( $the_tag ) { 2385 $this->set( 'tag_id', $the_tag->term_id ); 2386 } 2387 unset( $the_tag ); 2388 } 2389 } 2390 } 2391 2392 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) || ! empty( $this->allow_query_attachment_by_filename ) ) { 2393 $groupby = "{$wpdb->posts}.ID"; 2394 } 2395 2396 // Author/user stuff. 2397 2398 if ( ! empty( $query_vars['author'] ) && '0' != $query_vars['author'] ) { 2399 $query_vars['author'] = wp_slash( '' . urldecode( $query_vars['author'] ) ); 2400 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $query_vars['author'] ) ) ); 2401 sort( $authors ); 2402 foreach ( $authors as $author ) { 2403 $key = $author > 0 ? 'author__in' : 'author__not_in'; 2404 $query_vars[ $key ][] = abs( $author ); 2405 } 2406 $query_vars['author'] = implode( ',', $authors ); 2407 } 2408 2409 if ( ! empty( $query_vars['author__not_in'] ) ) { 2410 $author__not_in_id_list = wp_parse_id_list( $query_vars['author__not_in'] ); 2411 if ( count( $author__not_in_id_list ) > 0 ) { 2412 sort( $author__not_in_id_list ); 2413 $where .= sprintf( 2414 " AND {$wpdb->posts}.post_author NOT IN (%s) ", 2415 implode( ',', $author__not_in_id_list ) 2416 ); 2417 /** Update the query var for stable cache key generation in {@see self::generate_cache_key()}. */ 2418 $query_vars['author__not_in'] = $author__not_in_id_list; 2419 } 2420 } elseif ( ! empty( $query_vars['author__in'] ) ) { 2421 if ( is_array( $query_vars['author__in'] ) ) { 2422 $query_vars['author__in'] = array_unique( array_map( 'absint', $query_vars['author__in'] ) ); 2423 sort( $query_vars['author__in'] ); 2424 } 2425 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $query_vars['author__in'] ) ) ); 2426 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; 2427 } 2428 2429 // Author stuff for nice URLs. 2430 2431 if ( '' !== $query_vars['author_name'] ) { 2432 if ( str_contains( $query_vars['author_name'], '/' ) ) { 2433 $author_name_parts = explode( '/', $query_vars['author_name'] ); 2434 $last_part = array_last( $author_name_parts ); 2435 2436 if ( $last_part ) { 2437 $query_vars['author_name'] = $last_part; // No trailing slash. 2438 } else { 2439 $query_vars['author_name'] = $author_name_parts[ count( $author_name_parts ) - 2 ]; // There was a trailing slash. 2440 } 2441 } 2442 $query_vars['author_name'] = sanitize_title_for_query( $query_vars['author_name'] ); 2443 $query_vars['author'] = get_user_by( 'slug', $query_vars['author_name'] ); 2444 if ( $query_vars['author'] ) { 2445 $query_vars['author'] = $query_vars['author']->ID; 2446 } 2447 $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint( $query_vars['author'] ) . ')'; 2448 } 2449 2450 // Matching by comment count. 2451 if ( isset( $query_vars['comment_count'] ) ) { 2452 // Numeric comment count is converted to array format. 2453 if ( is_numeric( $query_vars['comment_count'] ) ) { 2454 $query_vars['comment_count'] = array( 2455 'value' => (int) $query_vars['comment_count'], 2456 ); 2457 } 2458 2459 if ( isset( $query_vars['comment_count']['value'] ) ) { 2460 $query_vars['comment_count'] = array_merge( 2461 array( 2462 'compare' => '=', 2463 ), 2464 $query_vars['comment_count'] 2465 ); 2466 2467 // Fallback for invalid compare operators is '='. 2468 $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' ); 2469 if ( ! in_array( $query_vars['comment_count']['compare'], $compare_operators, true ) ) { 2470 $query_vars['comment_count']['compare'] = '='; 2471 } 2472 2473 $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$query_vars['comment_count']['compare']} %d", $query_vars['comment_count']['value'] ); 2474 } 2475 } 2476 2477 // MIME-Type stuff for attachment browsing. 2478 2479 if ( isset( $query_vars['post_mime_type'] ) && '' !== $query_vars['post_mime_type'] ) { 2480 $whichmimetype = wp_post_mime_type_where( $query_vars['post_mime_type'], $wpdb->posts ); 2481 } 2482 $where .= $search . $whichauthor . $whichmimetype; 2483 2484 if ( ! empty( $this->allow_query_attachment_by_filename ) ) { 2485 $join .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; 2486 } 2487 2488 if ( ! empty( $this->meta_query->queries ) ) { 2489 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 2490 $join .= $clauses['join']; 2491 $where .= $clauses['where']; 2492 } 2493 2494 $rand = ( isset( $query_vars['orderby'] ) && 'rand' === $query_vars['orderby'] ); 2495 if ( ! isset( $query_vars['order'] ) ) { 2496 $query_vars['order'] = $rand ? '' : 'DESC'; 2497 } else { 2498 $query_vars['order'] = $rand ? '' : $this->parse_order( $query_vars['order'] ); 2499 } 2500 2501 // These values of orderby should ignore the 'order' parameter. 2502 $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in' ); 2503 if ( isset( $query_vars['orderby'] ) && in_array( $query_vars['orderby'], $force_asc, true ) ) { 2504 $query_vars['order'] = ''; 2505 } 2506 2507 // Order by. 2508 if ( empty( $query_vars['orderby'] ) ) { 2509 /* 2510 * Boolean false or empty array blanks out ORDER BY, 2511 * while leaving the value unset or otherwise empty sets the default. 2512 */ 2513 if ( isset( $query_vars['orderby'] ) && ( is_array( $query_vars['orderby'] ) || false === $query_vars['orderby'] ) ) { 2514 $orderby = ''; 2515 } else { 2516 $orderby = "{$wpdb->posts}.post_date " . $query_vars['order']; 2517 } 2518 } elseif ( 'none' === $query_vars['orderby'] ) { 2519 $orderby = ''; 2520 } else { 2521 $orderby_array = array(); 2522 if ( is_array( $query_vars['orderby'] ) ) { 2523 foreach ( $query_vars['orderby'] as $_orderby => $order ) { 2524 $orderby = wp_slash( urldecode( $_orderby ) ); 2525 $parsed = $this->parse_orderby( $orderby ); 2526 2527 if ( ! $parsed ) { 2528 continue; 2529 } 2530 2531 $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); 2532 } 2533 $orderby = implode( ', ', $orderby_array ); 2534 2535 } else { 2536 $query_vars['orderby'] = urldecode( $query_vars['orderby'] ); 2537 $query_vars['orderby'] = wp_slash( $query_vars['orderby'] ); 2538 2539 foreach ( explode( ' ', $query_vars['orderby'] ) as $i => $orderby ) { 2540 $parsed = $this->parse_orderby( $orderby ); 2541 // Only allow certain values for safety. 2542 if ( ! $parsed ) { 2543 continue; 2544 } 2545 2546 $orderby_array[] = $parsed; 2547 } 2548 $orderby = implode( ' ' . $query_vars['order'] . ', ', $orderby_array ); 2549 2550 if ( empty( $orderby ) ) { 2551 $orderby = "{$wpdb->posts}.post_date " . $query_vars['order']; 2552 } elseif ( ! empty( $query_vars['order'] ) ) { 2553 $orderby .= " {$query_vars['order']}"; 2554 } 2555 } 2556 } 2557 2558 // Order search results by relevance only when another "orderby" is not specified in the query. 2559 if ( ! empty( $query_vars['s'] ) ) { 2560 $search_orderby = ''; 2561 if ( ! empty( $query_vars['search_orderby_title'] ) && ( empty( $query_vars['orderby'] ) && ! $this->is_feed ) || ( isset( $query_vars['orderby'] ) && 'relevance' === $query_vars['orderby'] ) ) { 2562 $search_orderby = $this->parse_search_order( $query_vars ); 2563 } 2564 2565 if ( ! $query_vars['suppress_filters'] ) { 2566 /** 2567 * Filters the ORDER BY used when ordering search results. 2568 * 2569 * @since 3.7.0 2570 * 2571 * @param string $search_orderby The ORDER BY clause. 2572 * @param WP_Query $query The current WP_Query instance. 2573 */ 2574 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); 2575 } 2576 2577 if ( $search_orderby ) { 2578 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; 2579 } 2580 } 2581 2582 if ( is_array( $post_type ) && count( $post_type ) > 1 ) { 2583 $post_type_cap = 'multiple_post_type'; 2584 } else { 2585 if ( is_array( $post_type ) ) { 2586 $post_type = reset( $post_type ); 2587 } 2588 $post_type_object = get_post_type_object( $post_type ); 2589 if ( empty( $post_type_object ) ) { 2590 $post_type_cap = $post_type; 2591 } 2592 } 2593 2594 if ( isset( $query_vars['post_password'] ) ) { 2595 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $query_vars['post_password'] ); 2596 if ( empty( $query_vars['perm'] ) ) { 2597 $query_vars['perm'] = 'readable'; 2598 } 2599 } elseif ( isset( $query_vars['has_password'] ) ) { 2600 $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $query_vars['has_password'] ? '!=' : '=' ); 2601 } 2602 2603 if ( ! empty( $query_vars['comment_status'] ) ) { 2604 $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $query_vars['comment_status'] ); 2605 } 2606 2607 if ( ! empty( $query_vars['ping_status'] ) ) { 2608 $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $query_vars['ping_status'] ); 2609 } 2610 2611 $skip_post_status = false; 2612 if ( 'any' === $post_type ) { 2613 $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) ); 2614 if ( empty( $in_search_post_types ) ) { 2615 $post_type_where = ' AND 1=0 '; 2616 $skip_post_status = true; 2617 } else { 2618 $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; 2619 } 2620 } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) { 2621 // Sort post types to ensure same cache key generation. 2622 sort( $post_type ); 2623 $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')"; 2624 } elseif ( ! empty( $post_type ) ) { 2625 $post_type_where = $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); 2626 $post_type_object = get_post_type_object( $post_type ); 2627 } elseif ( $this->is_attachment ) { 2628 $post_type_where = " AND {$wpdb->posts}.post_type = 'attachment'"; 2629 $post_type_object = get_post_type_object( 'attachment' ); 2630 } elseif ( $this->is_page ) { 2631 $post_type_where = " AND {$wpdb->posts}.post_type = 'page'"; 2632 $post_type_object = get_post_type_object( 'page' ); 2633 } else { 2634 $post_type_where = " AND {$wpdb->posts}.post_type = 'post'"; 2635 $post_type_object = get_post_type_object( 'post' ); 2636 } 2637 2638 $edit_cap = 'edit_post'; 2639 $read_cap = 'read_post'; 2640 2641 if ( ! empty( $post_type_object ) ) { 2642 $edit_others_cap = $post_type_object->cap->edit_others_posts; 2643 $read_private_cap = $post_type_object->cap->read_private_posts; 2644 } else { 2645 $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; 2646 $read_private_cap = 'read_private_' . $post_type_cap . 's'; 2647 } 2648 2649 $user_id = get_current_user_id(); 2650 2651 $q_status = array(); 2652 if ( $skip_post_status ) { 2653 $where .= $post_type_where; 2654 } elseif ( ! empty( $query_vars['post_status'] ) ) { 2655 2656 $where .= $post_type_where; 2657 2658 $statuswheres = array(); 2659 $q_status = $query_vars['post_status']; 2660 if ( ! is_array( $q_status ) ) { 2661 $q_status = explode( ',', $q_status ); 2662 } 2663 sort( $q_status ); 2664 $r_status = array(); 2665 $p_status = array(); 2666 $e_status = array(); 2667 if ( in_array( 'any', $q_status, true ) ) { 2668 foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { 2669 if ( ! in_array( $status, $q_status, true ) ) { 2670 $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; 2671 } 2672 } 2673 } else { 2674 foreach ( get_post_stati() as $status ) { 2675 if ( in_array( $status, $q_status, true ) ) { 2676 if ( 'private' === $status ) { 2677 $p_status[] = "{$wpdb->posts}.post_status = '$status'"; 2678 } else { 2679 $r_status[] = "{$wpdb->posts}.post_status = '$status'"; 2680 } 2681 } 2682 } 2683 } 2684 2685 if ( empty( $query_vars['perm'] ) || 'readable' !== $query_vars['perm'] ) { 2686 $r_status = array_merge( $r_status, $p_status ); 2687 unset( $p_status ); 2688 } 2689 2690 if ( ! empty( $e_status ) ) { 2691 $statuswheres[] = '(' . implode( ' AND ', $e_status ) . ')'; 2692 } 2693 if ( ! empty( $r_status ) ) { 2694 if ( ! empty( $query_vars['perm'] ) && 'editable' === $query_vars['perm'] && ! current_user_can( $edit_others_cap ) ) { 2695 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $r_status ) . '))'; 2696 } else { 2697 $statuswheres[] = '(' . implode( ' OR ', $r_status ) . ')'; 2698 } 2699 } 2700 if ( ! empty( $p_status ) ) { 2701 if ( ! empty( $query_vars['perm'] ) && 'readable' === $query_vars['perm'] && ! current_user_can( $read_private_cap ) ) { 2702 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . implode( ' OR ', $p_status ) . '))'; 2703 } else { 2704 $statuswheres[] = '(' . implode( ' OR ', $p_status ) . ')'; 2705 } 2706 } 2707 if ( $post_status_join ) { 2708 $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; 2709 foreach ( $statuswheres as $index => $statuswhere ) { 2710 $statuswheres[ $index ] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . '))'; 2711 } 2712 } 2713 $where_status = implode( ' OR ', $statuswheres ); 2714 if ( ! empty( $where_status ) ) { 2715 $where .= " AND ($where_status)"; 2716 } 2717 } elseif ( ! $this->is_singular ) { 2718 if ( 'any' === $post_type ) { 2719 $queried_post_types = get_post_types( array( 'exclude_from_search' => false ) ); 2720 } elseif ( is_array( $post_type ) ) { 2721 $queried_post_types = $post_type; 2722 } elseif ( ! empty( $post_type ) ) { 2723 $queried_post_types = array( $post_type ); 2724 } else { 2725 $queried_post_types = array( 'post' ); 2726 } 2727 2728 if ( ! empty( $queried_post_types ) ) { 2729 sort( $queried_post_types ); 2730 $status_type_clauses = array(); 2731 2732 foreach ( $queried_post_types as $queried_post_type ) { 2733 2734 $queried_post_type_object = get_post_type_object( $queried_post_type ); 2735 2736 $type_where = '(' . $wpdb->prepare( "{$wpdb->posts}.post_type = %s AND (", $queried_post_type ); 2737 2738 // Public statuses. 2739 $public_statuses = get_post_stati( array( 'public' => true ) ); 2740 $status_clauses = array(); 2741 foreach ( $public_statuses as $public_status ) { 2742 $status_clauses[] = "{$wpdb->posts}.post_status = '$public_status'"; 2743 } 2744 $type_where .= implode( ' OR ', $status_clauses ); 2745 2746 // Add protected states that should show in the admin all list. 2747 if ( $this->is_admin ) { 2748 $admin_all_statuses = get_post_stati( 2749 array( 2750 'protected' => true, 2751 'show_in_admin_all_list' => true, 2752 ) 2753 ); 2754 foreach ( $admin_all_statuses as $admin_all_status ) { 2755 $type_where .= " OR {$wpdb->posts}.post_status = '$admin_all_status'"; 2756 } 2757 } 2758 2759 // Add private states that are visible to current user. 2760 if ( is_user_logged_in() && $queried_post_type_object instanceof WP_Post_Type ) { 2761 $read_private_cap = $queried_post_type_object->cap->read_private_posts; 2762 $private_statuses = get_post_stati( array( 'private' => true ) ); 2763 foreach ( $private_statuses as $private_status ) { 2764 $type_where .= current_user_can( $read_private_cap ) ? " \nOR {$wpdb->posts}.post_status = '$private_status'" : " \nOR ({$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$private_status')"; 2765 } 2766 } 2767 2768 $type_where .= '))'; 2769 2770 $status_type_clauses[] = $type_where; 2771 } 2772 2773 if ( ! empty( $status_type_clauses ) ) { 2774 $where .= ' AND (' . implode( ' OR ', $status_type_clauses ) . ')'; 2775 } 2776 } else { 2777 $where .= ' AND 1=0 '; 2778 } 2779 } else { 2780 $where .= $post_type_where; 2781 } 2782 2783 /* 2784 * Apply filters on where and join prior to paging so that any 2785 * manipulations to them are reflected in the paging by day queries. 2786 */ 2787 if ( ! $query_vars['suppress_filters'] ) { 2788 /** 2789 * Filters the WHERE clause of the query. 2790 * 2791 * @since 1.5.0 2792 * 2793 * @param string $where The WHERE clause of the query. 2794 * @param WP_Query $query The WP_Query instance (passed by reference). 2795 */ 2796 $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); 2797 2798 /** 2799 * Filters the JOIN clause of the query. 2800 * 2801 * @since 1.5.0 2802 * 2803 * @param string $join The JOIN clause of the query. 2804 * @param WP_Query $query The WP_Query instance (passed by reference). 2805 */ 2806 $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); 2807 } 2808 2809 // Paging. 2810 if ( empty( $query_vars['nopaging'] ) && ! $this->is_singular ) { 2811 $page = absint( $query_vars['paged'] ); 2812 if ( ! $page ) { 2813 $page = 1; 2814 } 2815 2816 // If 'offset' is provided, it takes precedence over 'paged'. 2817 if ( isset( $query_vars['offset'] ) && is_numeric( $query_vars['offset'] ) ) { 2818 $query_vars['offset'] = absint( $query_vars['offset'] ); 2819 $pgstrt = $query_vars['offset'] . ', '; 2820 } else { 2821 $pgstrt = absint( ( $page - 1 ) * $query_vars['posts_per_page'] ) . ', '; 2822 } 2823 $limits = 'LIMIT ' . $pgstrt . $query_vars['posts_per_page']; 2824 } 2825 2826 // Comments feeds. 2827 if ( $this->is_comment_feed && ! $this->is_singular ) { 2828 if ( $this->is_archive || $this->is_search ) { 2829 $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID ) $join "; 2830 $cwhere = "WHERE comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note' $where"; 2831 $cgroupby = "{$wpdb->comments}.comment_id"; 2832 } else { // Other non-singular, e.g. front. 2833 $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; 2834 $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note'"; 2835 $cgroupby = ''; 2836 } 2837 2838 if ( ! $query_vars['suppress_filters'] ) { 2839 /** 2840 * Filters the JOIN clause of the comments feed query before sending. 2841 * 2842 * @since 2.2.0 2843 * 2844 * @param string $cjoin The JOIN clause of the query. 2845 * @param WP_Query $query The WP_Query instance (passed by reference). 2846 */ 2847 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); 2848 2849 /** 2850 * Filters the WHERE clause of the comments feed query before sending. 2851 * 2852 * @since 2.2.0 2853 * 2854 * @param string $cwhere The WHERE clause of the query. 2855 * @param WP_Query $query The WP_Query instance (passed by reference). 2856 */ 2857 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); 2858 2859 /** 2860 * Filters the GROUP BY clause of the comments feed query before sending. 2861 * 2862 * @since 2.2.0 2863 * 2864 * @param string $cgroupby The GROUP BY clause of the query. 2865 * @param WP_Query $query The WP_Query instance (passed by reference). 2866 */ 2867 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); 2868 2869 /** 2870 * Filters the ORDER BY clause of the comments feed query before sending. 2871 * 2872 * @since 2.8.0 2873 * 2874 * @param string $corderby The ORDER BY clause of the query. 2875 * @param WP_Query $query The WP_Query instance (passed by reference). 2876 */ 2877 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 2878 2879 /** 2880 * Filters the LIMIT clause of the comments feed query before sending. 2881 * 2882 * @since 2.8.0 2883 * 2884 * @param string $climits The JOIN clause of the query. 2885 * @param WP_Query $query The WP_Query instance (passed by reference). 2886 */ 2887 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); 2888 } 2889 2890 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 2891 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 2892 $climits = ( ! empty( $climits ) ) ? $climits : ''; 2893 2894 $comments_request = "SELECT $distinct {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; 2895 2896 $key = md5( $comments_request ); 2897 $last_changed = array( 2898 wp_cache_get_last_changed( 'comment' ), 2899 wp_cache_get_last_changed( 'posts' ), 2900 ); 2901 2902 $cache_key = "comment_feed:$key"; 2903 $comment_ids = wp_cache_get_salted( $cache_key, 'comment-queries', $last_changed ); 2904 if ( false === $comment_ids ) { 2905 $comment_ids = $wpdb->get_col( $comments_request ); 2906 wp_cache_set_salted( $cache_key, $comment_ids, 'comment-queries', $last_changed ); 2907 } 2908 _prime_comment_caches( $comment_ids ); 2909 2910 // Convert to WP_Comment. 2911 /** @var WP_Comment[] */ 2912 $this->comments = array_map( 'get_comment', $comment_ids ); 2913 $this->comment_count = count( $this->comments ); 2914 2915 $post_ids = array(); 2916 2917 foreach ( $this->comments as $comment ) { 2918 $post_ids[] = (int) $comment->comment_post_ID; 2919 } 2920 2921 $post_ids = implode( ',', $post_ids ); 2922 $join = ''; 2923 if ( $post_ids ) { 2924 $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; 2925 } else { 2926 $where = 'AND 0'; 2927 } 2928 } 2929 2930 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2931 2932 /* 2933 * Apply post-paging filters on where and join. Only plugins that 2934 * manipulate paging queries should use these hooks. 2935 */ 2936 if ( ! $query_vars['suppress_filters'] ) { 2937 /** 2938 * Filters the WHERE clause of the query. 2939 * 2940 * Specifically for manipulating paging queries. 2941 * 2942 * @since 1.5.0 2943 * 2944 * @param string $where The WHERE clause of the query. 2945 * @param WP_Query $query The WP_Query instance (passed by reference). 2946 */ 2947 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); 2948 2949 /** 2950 * Filters the GROUP BY clause of the query. 2951 * 2952 * @since 2.0.0 2953 * 2954 * @param string $groupby The GROUP BY clause of the query. 2955 * @param WP_Query $query The WP_Query instance (passed by reference). 2956 */ 2957 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); 2958 2959 /** 2960 * Filters the JOIN clause of the query. 2961 * 2962 * Specifically for manipulating paging queries. 2963 * 2964 * @since 1.5.0 2965 * 2966 * @param string $join The JOIN clause of the query. 2967 * @param WP_Query $query The WP_Query instance (passed by reference). 2968 */ 2969 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); 2970 2971 /** 2972 * Filters the ORDER BY clause of the query. 2973 * 2974 * @since 1.5.1 2975 * 2976 * @param string $orderby The ORDER BY clause of the query. 2977 * @param WP_Query $query The WP_Query instance (passed by reference). 2978 */ 2979 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); 2980 2981 /** 2982 * Filters the DISTINCT clause of the query. 2983 * 2984 * @since 2.1.0 2985 * 2986 * @param string $distinct The DISTINCT clause of the query. 2987 * @param WP_Query $query The WP_Query instance (passed by reference). 2988 */ 2989 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); 2990 2991 /** 2992 * Filters the LIMIT clause of the query. 2993 * 2994 * @since 2.1.0 2995 * 2996 * @param string $limits The LIMIT clause of the query. 2997 * @param WP_Query $query The WP_Query instance (passed by reference). 2998 */ 2999 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); 3000 3001 /** 3002 * Filters the SELECT clause of the query. 3003 * 3004 * @since 2.1.0 3005 * 3006 * @param string $fields The SELECT clause of the query. 3007 * @param WP_Query $query The WP_Query instance (passed by reference). 3008 */ 3009 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); 3010 3011 /** 3012 * Filters all query clauses at once, for convenience. 3013 * 3014 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, 3015 * fields (SELECT), and LIMIT clauses. 3016 * 3017 * @since 3.1.0 3018 * 3019 * @param string[] $clauses { 3020 * Associative array of the clauses for the query. 3021 * 3022 * @type string $where The WHERE clause of the query. 3023 * @type string $groupby The GROUP BY clause of the query. 3024 * @type string $join The JOIN clause of the query. 3025 * @type string $orderby The ORDER BY clause of the query. 3026 * @type string $distinct The DISTINCT clause of the query. 3027 * @type string $fields The SELECT clause of the query. 3028 * @type string $limits The LIMIT clause of the query. 3029 * } 3030 * @param WP_Query $query The WP_Query instance (passed by reference). 3031 */ 3032 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 3033 3034 $where = $clauses['where'] ?? ''; 3035 $groupby = $clauses['groupby'] ?? ''; 3036 $join = $clauses['join'] ?? ''; 3037 $orderby = $clauses['orderby'] ?? ''; 3038 $distinct = $clauses['distinct'] ?? ''; 3039 $fields = $clauses['fields'] ?? ''; 3040 $limits = $clauses['limits'] ?? ''; 3041 } 3042 3043 /** 3044 * Fires to announce the query's current selection parameters. 3045 * 3046 * For use by caching plugins. 3047 * 3048 * @since 2.3.0 3049 * 3050 * @param string $selection The assembled selection query. 3051 */ 3052 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); 3053 3054 /* 3055 * Filters again for the benefit of caching plugins. 3056 * Regular plugins should use the hooks above. 3057 */ 3058 if ( ! $query_vars['suppress_filters'] ) { 3059 /** 3060 * Filters the WHERE clause of the query. 3061 * 3062 * For use by caching plugins. 3063 * 3064 * @since 2.5.0 3065 * 3066 * @param string $where The WHERE clause of the query. 3067 * @param WP_Query $query The WP_Query instance (passed by reference). 3068 */ 3069 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); 3070 3071 /** 3072 * Filters the GROUP BY clause of the query. 3073 * 3074 * For use by caching plugins. 3075 * 3076 * @since 2.5.0 3077 * 3078 * @param string $groupby The GROUP BY clause of the query. 3079 * @param WP_Query $query The WP_Query instance (passed by reference). 3080 */ 3081 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); 3082 3083 /** 3084 * Filters the JOIN clause of the query. 3085 * 3086 * For use by caching plugins. 3087 * 3088 * @since 2.5.0 3089 * 3090 * @param string $join The JOIN clause of the query. 3091 * @param WP_Query $query The WP_Query instance (passed by reference). 3092 */ 3093 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); 3094 3095 /** 3096 * Filters the ORDER BY clause of the query. 3097 * 3098 * For use by caching plugins. 3099 * 3100 * @since 2.5.0 3101 * 3102 * @param string $orderby The ORDER BY clause of the query. 3103 * @param WP_Query $query The WP_Query instance (passed by reference). 3104 */ 3105 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); 3106 3107 /** 3108 * Filters the DISTINCT clause of the query. 3109 * 3110 * For use by caching plugins. 3111 * 3112 * @since 2.5.0 3113 * 3114 * @param string $distinct The DISTINCT clause of the query. 3115 * @param WP_Query $query The WP_Query instance (passed by reference). 3116 */ 3117 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); 3118 3119 /** 3120 * Filters the SELECT clause of the query. 3121 * 3122 * For use by caching plugins. 3123 * 3124 * @since 2.5.0 3125 * 3126 * @param string $fields The SELECT clause of the query. 3127 * @param WP_Query $query The WP_Query instance (passed by reference). 3128 */ 3129 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); 3130 3131 /** 3132 * Filters the LIMIT clause of the query. 3133 * 3134 * For use by caching plugins. 3135 * 3136 * @since 2.5.0 3137 * 3138 * @param string $limits The LIMIT clause of the query. 3139 * @param WP_Query $query The WP_Query instance (passed by reference). 3140 */ 3141 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); 3142 3143 /** 3144 * Filters all query clauses at once, for convenience. 3145 * 3146 * For use by caching plugins. 3147 * 3148 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, 3149 * fields (SELECT), and LIMIT clauses. 3150 * 3151 * @since 3.1.0 3152 * 3153 * @param string[] $clauses { 3154 * Associative array of the clauses for the query. 3155 * 3156 * @type string $where The WHERE clause of the query. 3157 * @type string $groupby The GROUP BY clause of the query. 3158 * @type string $join The JOIN clause of the query. 3159 * @type string $orderby The ORDER BY clause of the query. 3160 * @type string $distinct The DISTINCT clause of the query. 3161 * @type string $fields The SELECT clause of the query. 3162 * @type string $limits The LIMIT clause of the query. 3163 * } 3164 * @param WP_Query $query The WP_Query instance (passed by reference). 3165 */ 3166 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 3167 3168 $where = $clauses['where'] ?? ''; 3169 $groupby = $clauses['groupby'] ?? ''; 3170 $join = $clauses['join'] ?? ''; 3171 $orderby = $clauses['orderby'] ?? ''; 3172 $distinct = $clauses['distinct'] ?? ''; 3173 $fields = $clauses['fields'] ?? ''; 3174 $limits = $clauses['limits'] ?? ''; 3175 } 3176 3177 if ( ! empty( $groupby ) ) { 3178 $groupby = 'GROUP BY ' . $groupby; 3179 } 3180 if ( ! empty( $orderby ) ) { 3181 $orderby = 'ORDER BY ' . $orderby; 3182 } 3183 3184 $found_rows = ''; 3185 if ( ! $query_vars['no_found_rows'] && ! empty( $limits ) ) { 3186 $found_rows = 'SQL_CALC_FOUND_ROWS'; 3187 } 3188 3189 /* 3190 * Beginning of the string is on a new line to prevent leading whitespace. 3191 * 3192 * The additional indentation of subsequent lines is to ensure the SQL 3193 * queries are identical to those generated when splitting queries. This 3194 * improves caching of the query by ensuring the same cache key is 3195 * generated for the same database queries functionally. 3196 * 3197 * See https://core.trac.wordpress.org/ticket/56841. 3198 * See https://github.com/WordPress/wordpress-develop/pull/6393#issuecomment-2088217429 3199 */ 3200 $old_request = 3201 "SELECT $found_rows $distinct $fields 3202 FROM {$wpdb->posts} $join 3203 WHERE 1=1 $where 3204 $groupby 3205 $orderby 3206 $limits"; 3207 3208 $this->request = $old_request; 3209 3210 if ( ! $query_vars['suppress_filters'] ) { 3211 /** 3212 * Filters the completed SQL query before sending. 3213 * 3214 * @since 2.0.0 3215 * 3216 * @param string $request The complete SQL query. 3217 * @param WP_Query $query The WP_Query instance (passed by reference). 3218 */ 3219 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); 3220 } 3221 3222 /** 3223 * Filters the posts array before the query takes place. 3224 * 3225 * Return a non-null value to bypass WordPress' default post queries. 3226 * 3227 * Filtering functions that require pagination information are encouraged to set 3228 * the `found_posts` and `max_num_pages` properties of the WP_Query object, 3229 * passed to the filter by reference. If WP_Query does not perform a database 3230 * query, it will not have enough information to generate these values itself. 3231 * 3232 * @since 4.6.0 3233 * 3234 * @param WP_Post[]|int[]|null $posts Return an array of post data to short-circuit WP's query, 3235 * or null to allow WP to run its normal queries. 3236 * @param WP_Query $query The WP_Query instance (passed by reference). 3237 */ 3238 $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); 3239 3240 /* 3241 * Ensure the ID database query is able to be cached. 3242 * 3243 * Random queries are expected to have unpredictable results and 3244 * cannot be cached. Note the space before `RAND` in the string 3245 * search, that to ensure against a collision with another 3246 * function. 3247 * 3248 * If `$fields` has been modified by the `posts_fields`, 3249 * `posts_fields_request`, `post_clauses` or `posts_clauses_request` 3250 * filters, then caching is disabled to prevent caching collisions. 3251 */ 3252 $id_query_is_cacheable = ! str_contains( strtoupper( $orderby ), ' RAND(' ); 3253 3254 $cacheable_field_values = array( 3255 "{$wpdb->posts}.*", 3256 "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent", 3257 "{$wpdb->posts}.ID", 3258 ); 3259 3260 if ( ! in_array( $fields, $cacheable_field_values, true ) ) { 3261 $id_query_is_cacheable = false; 3262 } 3263 3264 $last_changed = (array) wp_cache_get_last_changed( 'posts' ); 3265 if ( ! empty( $this->tax_query->queries ) ) { 3266 $last_changed[] = wp_cache_get_last_changed( 'terms' ); 3267 } 3268 3269 if ( $query_vars['cache_results'] && $id_query_is_cacheable ) { 3270 $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request ); 3271 $cache_key = $this->generate_cache_key( $query_vars, $new_request ); 3272 3273 $cache_found = false; 3274 if ( null === $this->posts ) { 3275 $cached_results = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); 3276 3277 if ( $cached_results ) { 3278 $cache_found = true; 3279 /** @var int[] */ 3280 $post_ids = array_map( 'intval', $cached_results['posts'] ); 3281 3282 $this->post_count = count( $post_ids ); 3283 $this->found_posts = $cached_results['found_posts']; 3284 $this->max_num_pages = $cached_results['max_num_pages']; 3285 3286 if ( 'ids' === $query_vars['fields'] ) { 3287 $this->posts = $post_ids; 3288 3289 return $this->posts; 3290 } elseif ( 'id=>parent' === $query_vars['fields'] ) { 3291 _prime_post_parent_id_caches( $post_ids ); 3292 3293 $post_parent_cache_keys = array(); 3294 foreach ( $post_ids as $post_id ) { 3295 $post_parent_cache_keys[] = 'post_parent:' . (string) $post_id; 3296 } 3297 3298 /** @var int[] */ 3299 $post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' ); 3300 3301 foreach ( $post_parents as $cache_key => $post_parent ) { 3302 $obj = new stdClass(); 3303 $obj->ID = (int) str_replace( 'post_parent:', '', $cache_key ); 3304 $obj->post_parent = (int) $post_parent; 3305 3306 $this->posts[] = $obj; 3307 } 3308 3309 return $post_parents; 3310 } else { 3311 _prime_post_caches( $post_ids, $query_vars['update_post_term_cache'], $query_vars['update_post_meta_cache'] ); 3312 /** @var WP_Post[] */ 3313 $this->posts = array_map( 'get_post', $post_ids ); 3314 } 3315 } 3316 } 3317 } 3318 3319 if ( 'ids' === $query_vars['fields'] ) { 3320 if ( null === $this->posts ) { 3321 $this->posts = $wpdb->get_col( $this->request ); 3322 } 3323 3324 /** @var int[] */ 3325 $this->posts = array_map( 'intval', $this->posts ); 3326 $this->post_count = count( $this->posts ); 3327 $this->set_found_posts( $query_vars, $limits ); 3328 3329 if ( $query_vars['cache_results'] && $id_query_is_cacheable ) { 3330 $cache_value = array( 3331 'posts' => $this->posts, 3332 'found_posts' => $this->found_posts, 3333 'max_num_pages' => $this->max_num_pages, 3334 ); 3335 3336 wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed ); 3337 } 3338 3339 return $this->posts; 3340 } 3341 3342 if ( 'id=>parent' === $query_vars['fields'] ) { 3343 if ( null === $this->posts ) { 3344 $this->posts = $wpdb->get_results( $this->request ); 3345 } 3346 3347 $this->post_count = count( $this->posts ); 3348 $this->set_found_posts( $query_vars, $limits ); 3349 3350 /** @var int[] */ 3351 $post_parents = array(); 3352 $post_ids = array(); 3353 $post_parents_cache = array(); 3354 3355 foreach ( $this->posts as $key => $post ) { 3356 $this->posts[ $key ]->ID = (int) $post->ID; 3357 $this->posts[ $key ]->post_parent = (int) $post->post_parent; 3358 3359 $post_parents[ (int) $post->ID ] = (int) $post->post_parent; 3360 $post_ids[] = (int) $post->ID; 3361 3362 $post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent; 3363 } 3364 // Prime post parent caches, so that on second run, there is not another database query. 3365 wp_cache_add_multiple( $post_parents_cache, 'posts' ); 3366 3367 if ( $query_vars['cache_results'] && $id_query_is_cacheable ) { 3368 $cache_value = array( 3369 'posts' => $post_ids, 3370 'found_posts' => $this->found_posts, 3371 'max_num_pages' => $this->max_num_pages, 3372 ); 3373 3374 wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed ); 3375 } 3376 3377 return $post_parents; 3378 } 3379 3380 $is_unfiltered_query = $old_request === $this->request && "{$wpdb->posts}.*" === $fields; 3381 3382 if ( null === $this->posts ) { 3383 $split_the_query = ( 3384 $is_unfiltered_query 3385 && ( 3386 wp_using_ext_object_cache() 3387 || ( ! empty( $limits ) && $query_vars['posts_per_page'] < 500 ) 3388 ) 3389 ); 3390 3391 /** 3392 * Filters whether to split the query. 3393 * 3394 * Splitting the query will cause it to fetch just the IDs of the found posts 3395 * (and then individually fetch each post by ID), rather than fetching every 3396 * complete row at once. One massive result vs. many small results. 3397 * 3398 * @since 3.4.0 3399 * @since 6.6.0 Added the `$old_request` and `$clauses` parameters. 3400 * 3401 * @param bool $split_the_query Whether or not to split the query. 3402 * @param WP_Query $query The WP_Query instance. 3403 * @param string $old_request The complete SQL query before filtering. 3404 * @param string[] $clauses { 3405 * Associative array of the clauses for the query. 3406 * 3407 * @type string $where The WHERE clause of the query. 3408 * @type string $groupby The GROUP BY clause of the query. 3409 * @type string $join The JOIN clause of the query. 3410 * @type string $orderby The ORDER BY clause of the query. 3411 * @type string $distinct The DISTINCT clause of the query. 3412 * @type string $fields The SELECT clause of the query. 3413 * @type string $limits The LIMIT clause of the query. 3414 * } 3415 */ 3416 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this, $old_request, compact( $pieces ) ); 3417 3418 if ( $split_the_query ) { 3419 // First get the IDs and then fill in the objects. 3420 3421 // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841. 3422 $this->request = 3423 "SELECT $found_rows $distinct {$wpdb->posts}.ID 3424 FROM {$wpdb->posts} $join 3425 WHERE 1=1 $where 3426 $groupby 3427 $orderby 3428 $limits"; 3429 3430 /** 3431 * Filters the Post IDs SQL request before sending. 3432 * 3433 * @since 3.4.0 3434 * 3435 * @param string $request The post ID request. 3436 * @param WP_Query $query The WP_Query instance. 3437 */ 3438 $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); 3439 3440 $post_ids = $wpdb->get_col( $this->request ); 3441 3442 if ( $post_ids ) { 3443 $this->posts = $post_ids; 3444 $this->set_found_posts( $query_vars, $limits ); 3445 _prime_post_caches( $post_ids, $query_vars['update_post_term_cache'], $query_vars['update_post_meta_cache'] ); 3446 } else { 3447 $this->posts = array(); 3448 } 3449 } else { 3450 $this->posts = $wpdb->get_results( $this->request ); 3451 $this->set_found_posts( $query_vars, $limits ); 3452 } 3453 } 3454 3455 // Convert to WP_Post objects. 3456 if ( $this->posts ) { 3457 /** @var WP_Post[] */ 3458 $this->posts = array_map( 'get_post', $this->posts ); 3459 } 3460 3461 $unfiltered_posts = $this->posts; 3462 3463 if ( $query_vars['cache_results'] && $id_query_is_cacheable && ! $cache_found ) { 3464 $post_ids = wp_list_pluck( $this->posts, 'ID' ); 3465 3466 $cache_value = array( 3467 'posts' => $post_ids, 3468 'found_posts' => $this->found_posts, 3469 'max_num_pages' => $this->max_num_pages, 3470 ); 3471 3472 wp_cache_set_salted( $cache_key, $cache_value, 'post-queries', $last_changed ); 3473 } 3474 3475 if ( ! $query_vars['suppress_filters'] ) { 3476 /** 3477 * Filters the raw post results array, prior to status checks. 3478 * 3479 * @since 2.3.0 3480 * 3481 * @param WP_Post[] $posts Array of post objects. 3482 * @param WP_Query $query The WP_Query instance (passed by reference). 3483 */ 3484 $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); 3485 } 3486 3487 if ( ! empty( $this->posts ) && $this->is_comment_feed && $this->is_singular ) { 3488 /** This filter is documented in wp-includes/class-wp-query.php */ 3489 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); 3490 3491 /** This filter is documented in wp-includes/class-wp-query.php */ 3492 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note'", &$this ) ); 3493 3494 /** This filter is documented in wp-includes/class-wp-query.php */ 3495 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); 3496 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 3497 3498 /** This filter is documented in wp-includes/class-wp-query.php */ 3499 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 3500 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 3501 3502 /** This filter is documented in wp-includes/class-wp-query.php */ 3503 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); 3504 3505 $comments_request = "SELECT {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; 3506 3507 $comment_key = md5( $comments_request ); 3508 $comment_last_changed = wp_cache_get_last_changed( 'comment' ); 3509 3510 $comment_cache_key = "comment_feed:$comment_key"; 3511 $comment_ids = wp_cache_get_salted( $comment_cache_key, 'comment-queries', $comment_last_changed ); 3512 if ( false === $comment_ids ) { 3513 $comment_ids = $wpdb->get_col( $comments_request ); 3514 wp_cache_set_salted( $comment_cache_key, $comment_ids, 'comment-queries', $comment_last_changed ); 3515 } 3516 _prime_comment_caches( $comment_ids ); 3517 3518 // Convert to WP_Comment. 3519 /** @var WP_Comment[] */ 3520 $this->comments = array_map( 'get_comment', $comment_ids ); 3521 $this->comment_count = count( $this->comments ); 3522 } 3523 3524 // Check post status to determine if post should be displayed. 3525 if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) { 3526 $status = get_post_status( $this->posts[0] ); 3527 3528 if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { 3529 $this->is_page = false; 3530 $this->is_single = true; 3531 $this->is_attachment = true; 3532 } 3533 3534 // If the post_status was specifically requested, let it pass through. 3535 if ( ! in_array( $status, $q_status, true ) ) { 3536 $post_status_obj = get_post_status_object( $status ); 3537 3538 if ( $post_status_obj && ! $post_status_obj->public ) { 3539 if ( ! is_user_logged_in() ) { 3540 // User must be logged in to view unpublished posts. 3541 $this->posts = array(); 3542 } else { 3543 if ( $post_status_obj->protected ) { 3544 // User must have edit permissions on the draft to preview. 3545 if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { 3546 $this->posts = array(); 3547 } else { 3548 $this->is_preview = true; 3549 if ( 'future' !== $status ) { 3550 $this->posts[0]->post_date = current_time( 'mysql' ); 3551 } 3552 } 3553 } elseif ( $post_status_obj->private ) { 3554 if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) { 3555 $this->posts = array(); 3556 } 3557 } else { 3558 $this->posts = array(); 3559 } 3560 } 3561 } elseif ( ! $post_status_obj ) { 3562 // Post status is not registered, assume it's not public. 3563 if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { 3564 $this->posts = array(); 3565 } 3566 } 3567 } 3568 3569 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { 3570 /** 3571 * Filters the single post for preview mode. 3572 * 3573 * @since 2.7.0 3574 * 3575 * @param WP_Post $post_preview The Post object. 3576 * @param WP_Query $query The WP_Query instance (passed by reference). 3577 */ 3578 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); 3579 } 3580 } 3581 3582 // Put sticky posts at the top of the posts array. 3583 $sticky_posts = get_option( 'sticky_posts' ); 3584 if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $query_vars['ignore_sticky_posts'] ) { 3585 $num_posts = count( $this->posts ); 3586 $sticky_offset = 0; 3587 // Loop over posts and relocate stickies to the front. 3588 for ( $i = 0; $i < $num_posts; $i++ ) { 3589 if ( in_array( $this->posts[ $i ]->ID, $sticky_posts, true ) ) { 3590 $sticky_post = $this->posts[ $i ]; 3591 // Remove sticky from current position. 3592 array_splice( $this->posts, $i, 1 ); 3593 // Move to front, after other stickies. 3594 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); 3595 // Increment the sticky offset. The next sticky will be placed at this offset. 3596 ++$sticky_offset; 3597 // Remove post from sticky posts array. 3598 $offset = array_search( $sticky_post->ID, $sticky_posts, true ); 3599 unset( $sticky_posts[ $offset ] ); 3600 } 3601 } 3602 3603 // If any posts have been excluded specifically, Ignore those that are sticky. 3604 if ( ! empty( $sticky_posts ) && ! empty( $query_vars['post__not_in'] ) ) { 3605 $sticky_posts = array_diff( $sticky_posts, $query_vars['post__not_in'] ); 3606 } 3607 3608 // Fetch sticky posts that weren't in the query results. 3609 if ( ! empty( $sticky_posts ) ) { 3610 $stickies = get_posts( 3611 array( 3612 'post__in' => $sticky_posts, 3613 'post_type' => $post_type, 3614 'post_status' => 'publish', 3615 'posts_per_page' => count( $sticky_posts ), 3616 'suppress_filters' => $query_vars['suppress_filters'], 3617 'cache_results' => $query_vars['cache_results'], 3618 'update_post_meta_cache' => $query_vars['update_post_meta_cache'], 3619 'update_post_term_cache' => $query_vars['update_post_term_cache'], 3620 'lazy_load_term_meta' => $query_vars['lazy_load_term_meta'], 3621 ) 3622 ); 3623 3624 foreach ( $stickies as $sticky_post ) { 3625 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); 3626 ++$sticky_offset; 3627 } 3628 } 3629 } 3630 3631 if ( ! $query_vars['suppress_filters'] ) { 3632 /** 3633 * Filters the array of retrieved posts after they've been fetched and 3634 * internally processed. 3635 * 3636 * @since 1.5.0 3637 * 3638 * @param WP_Post[] $posts Array of post objects. 3639 * @param WP_Query $query The WP_Query instance (passed by reference). 3640 */ 3641 $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); 3642 } 3643 3644 /* 3645 * Ensure that any posts added/modified via one of the filters above are 3646 * of the type WP_Post and are filtered. 3647 */ 3648 if ( $this->posts ) { 3649 $this->post_count = count( $this->posts ); 3650 3651 /** @var WP_Post[] */ 3652 $this->posts = array_map( 'get_post', $this->posts ); 3653 3654 if ( $query_vars['cache_results'] ) { 3655 if ( $is_unfiltered_query && $unfiltered_posts === $this->posts ) { 3656 update_post_caches( $this->posts, $post_type, $query_vars['update_post_term_cache'], $query_vars['update_post_meta_cache'] ); 3657 } else { 3658 $post_ids = wp_list_pluck( $this->posts, 'ID' ); 3659 _prime_post_caches( $post_ids, $query_vars['update_post_term_cache'], $query_vars['update_post_meta_cache'] ); 3660 } 3661 } 3662 3663 /** @var WP_Post */ 3664 $this->post = reset( $this->posts ); 3665 } else { 3666 $this->post_count = 0; 3667 $this->posts = array(); 3668 } 3669 3670 if ( ! empty( $this->posts ) && $query_vars['update_menu_item_cache'] ) { 3671 update_menu_item_cache( $this->posts ); 3672 } 3673 3674 if ( $query_vars['lazy_load_term_meta'] ) { 3675 wp_queue_posts_for_term_meta_lazyload( $this->posts ); 3676 } 3677 3678 return $this->posts; 3679 } 3680 3681 /** 3682 * Sets up the amount of found posts and the number of pages (if limit clause was used) 3683 * for the current query. 3684 * 3685 * @since 3.5.0 3686 * 3687 * @global wpdb $wpdb WordPress database abstraction object. 3688 * 3689 * @param array $query_vars Query variables. 3690 * @param string $limits LIMIT clauses of the query. 3691 */ 3692 private function set_found_posts( $query_vars, $limits ) { 3693 global $wpdb; 3694 3695 /* 3696 * Bail if posts is an empty array. Continue if posts is an empty string, 3697 * null, or false to accommodate caching plugins that fill posts later. 3698 */ 3699 if ( $query_vars['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) { 3700 return; 3701 } 3702 3703 if ( ! empty( $limits ) ) { 3704 /** 3705 * Filters the query to run for retrieving the found posts. 3706 * 3707 * @since 2.1.0 3708 * 3709 * @param string $found_posts_query The query to run to find the found posts. 3710 * @param WP_Query $query The WP_Query instance (passed by reference). 3711 */ 3712 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 3713 3714 $this->found_posts = (int) $wpdb->get_var( $found_posts_query ); 3715 } else { 3716 if ( is_array( $this->posts ) ) { 3717 $this->found_posts = count( $this->posts ); 3718 } else { 3719 if ( null === $this->posts ) { 3720 $this->found_posts = 0; 3721 } else { 3722 $this->found_posts = 1; 3723 } 3724 } 3725 } 3726 3727 /** 3728 * Filters the number of found posts for the query. 3729 * 3730 * @since 2.1.0 3731 * 3732 * @param int $found_posts The number of posts found. 3733 * @param WP_Query $query The WP_Query instance (passed by reference). 3734 */ 3735 $this->found_posts = (int) apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 3736 3737 if ( ! empty( $limits ) ) { 3738 $this->max_num_pages = (int) ceil( $this->found_posts / $query_vars['posts_per_page'] ); 3739 } 3740 } 3741 3742 /** 3743 * Sets up the next post and iterate current post index. 3744 * 3745 * @since 1.5.0 3746 * 3747 * @return WP_Post Next post. 3748 */ 3749 public function next_post() { 3750 3751 ++$this->current_post; 3752 3753 /** @var WP_Post */ 3754 $this->post = $this->posts[ $this->current_post ]; 3755 return $this->post; 3756 } 3757 3758 /** 3759 * Sets up the current post. 3760 * 3761 * Retrieves the next post, sets up the post, sets the 'in the loop' 3762 * property to true. 3763 * 3764 * @since 1.5.0 3765 * 3766 * @global WP_Post $post Global post object. 3767 */ 3768 public function the_post() { 3769 global $post; 3770 3771 if ( ! $this->in_the_loop ) { 3772 if ( 'all' === $this->query_vars['fields'] ) { 3773 // Full post objects queried. 3774 $post_objects = $this->posts; 3775 } else { 3776 if ( 'ids' === $this->query_vars['fields'] ) { 3777 // Post IDs queried. 3778 $post_ids = $this->posts; 3779 } else { 3780 // Only partial objects queried, need to prime the cache for the loop. 3781 $post_ids = array_reduce( 3782 $this->posts, 3783 function ( $carry, $post ) { 3784 if ( isset( $post->ID ) ) { 3785 $carry[] = $post->ID; 3786 } 3787 3788 return $carry; 3789 }, 3790 array() 3791 ); 3792 } 3793 _prime_post_caches( $post_ids, $this->query_vars['update_post_term_cache'], $this->query_vars['update_post_meta_cache'] ); 3794 $post_objects = array_map( 'get_post', $post_ids ); 3795 } 3796 update_post_author_caches( $post_objects ); 3797 } 3798 3799 $this->in_the_loop = true; 3800 $this->before_loop = false; 3801 3802 if ( -1 === $this->current_post ) { // Loop has just started. 3803 /** 3804 * Fires once the loop is started. 3805 * 3806 * @since 2.0.0 3807 * 3808 * @param WP_Query $query The WP_Query instance (passed by reference). 3809 */ 3810 do_action_ref_array( 'loop_start', array( &$this ) ); 3811 } 3812 3813 $post = $this->next_post(); 3814 3815 // Ensure a full post object is available. 3816 if ( 'all' !== $this->query_vars['fields'] ) { 3817 if ( 'ids' === $this->query_vars['fields'] ) { 3818 // Post IDs queried. 3819 $post = get_post( $post ); 3820 } elseif ( isset( $post->ID ) ) { 3821 /* 3822 * Partial object queried. 3823 * 3824 * The post object was queried with a partial set of 3825 * fields, populate the entire object for the loop. 3826 */ 3827 $post = get_post( $post->ID ); 3828 } 3829 } 3830 3831 // Set up the global post object for the loop. 3832 $this->setup_postdata( $post ); 3833 } 3834 3835 /** 3836 * Determines whether there are more posts available in the loop. 3837 * 3838 * Calls the {@see 'loop_end'} action when the loop is complete. 3839 * 3840 * @since 1.5.0 3841 * 3842 * @return bool True if posts are available, false if end of the loop. 3843 */ 3844 public function have_posts() { 3845 if ( $this->current_post + 1 < $this->post_count ) { 3846 return true; 3847 } elseif ( $this->current_post + 1 === $this->post_count && $this->post_count > 0 ) { 3848 /** 3849 * Fires once the loop has ended. 3850 * 3851 * @since 2.0.0 3852 * 3853 * @param WP_Query $query The WP_Query instance (passed by reference). 3854 */ 3855 do_action_ref_array( 'loop_end', array( &$this ) ); 3856 3857 // Do some cleaning up after the loop. 3858 $this->rewind_posts(); 3859 } elseif ( 0 === $this->post_count ) { 3860 $this->before_loop = false; 3861 3862 /** 3863 * Fires if no results are found in a post query. 3864 * 3865 * @since 4.9.0 3866 * 3867 * @param WP_Query $query The WP_Query instance. 3868 */ 3869 do_action( 'loop_no_results', $this ); 3870 } 3871 3872 $this->in_the_loop = false; 3873 return false; 3874 } 3875 3876 /** 3877 * Rewinds the posts and resets post index. 3878 * 3879 * @since 1.5.0 3880 */ 3881 public function rewind_posts() { 3882 $this->current_post = -1; 3883 if ( $this->post_count > 0 ) { 3884 $this->post = $this->posts[0]; 3885 } 3886 } 3887 3888 /** 3889 * Iterates current comment index and returns WP_Comment object. 3890 * 3891 * @since 2.2.0 3892 * 3893 * @return WP_Comment Comment object. 3894 */ 3895 public function next_comment() { 3896 ++$this->current_comment; 3897 3898 /** @var WP_Comment */ 3899 $this->comment = $this->comments[ $this->current_comment ]; 3900 return $this->comment; 3901 } 3902 3903 /** 3904 * Sets up the current comment. 3905 * 3906 * @since 2.2.0 3907 * 3908 * @global WP_Comment $comment Global comment object. 3909 */ 3910 public function the_comment() { 3911 global $comment; 3912 3913 $comment = $this->next_comment(); 3914 3915 if ( 0 === $this->current_comment ) { 3916 /** 3917 * Fires once the comment loop is started. 3918 * 3919 * @since 2.2.0 3920 */ 3921 do_action( 'comment_loop_start' ); 3922 } 3923 } 3924 3925 /** 3926 * Determines whether there are more comments available. 3927 * 3928 * Automatically rewinds comments when finished. 3929 * 3930 * @since 2.2.0 3931 * 3932 * @return bool True if comments are available, false if no more comments. 3933 */ 3934 public function have_comments() { 3935 if ( $this->current_comment + 1 < $this->comment_count ) { 3936 return true; 3937 } elseif ( $this->current_comment + 1 === $this->comment_count ) { 3938 $this->rewind_comments(); 3939 } 3940 3941 return false; 3942 } 3943 3944 /** 3945 * Rewinds the comments, resets the comment index and comment to first. 3946 * 3947 * @since 2.2.0 3948 */ 3949 public function rewind_comments() { 3950 $this->current_comment = -1; 3951 if ( $this->comment_count > 0 ) { 3952 $this->comment = $this->comments[0]; 3953 } 3954 } 3955 3956 /** 3957 * Sets up the WordPress query by parsing query string. 3958 * 3959 * @since 1.5.0 3960 * 3961 * @see WP_Query::parse_query() for all available arguments. 3962 * 3963 * @param string|array $query URL query string or array of query arguments. 3964 * @return WP_Post[]|int[] Array of post objects or post IDs. 3965 * 3966 * @phpstan-return ( 3967 * $query is array{ fields: 'ids', ... } ? int[] : WP_Post[] 3968 * ) 3969 */ 3970 public function query( $query ) { 3971 $this->init(); 3972 $this->query = wp_parse_args( $query ); 3973 $this->query_vars = $this->query; 3974 return $this->get_posts(); 3975 } 3976 3977 /** 3978 * Retrieves the currently queried object. 3979 * 3980 * If queried object is not set, then the queried object will be set from 3981 * the category, tag, taxonomy, posts page, single post, page, or author 3982 * query variable. After it is set up, it will be returned. 3983 * 3984 * @since 1.5.0 3985 * 3986 * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object. 3987 */ 3988 public function get_queried_object() { 3989 if ( isset( $this->queried_object ) ) { 3990 return $this->queried_object; 3991 } 3992 3993 $this->queried_object = null; 3994 $this->queried_object_id = null; 3995 3996 if ( $this->is_category || $this->is_tag || $this->is_tax ) { 3997 if ( $this->is_category ) { 3998 $cat = $this->get( 'cat' ); 3999 $category_name = $this->get( 'category_name' ); 4000 4001 if ( $cat ) { 4002 $term = get_term( $cat, 'category' ); 4003 } elseif ( $category_name ) { 4004 $term = get_term_by( 'slug', $category_name, 'category' ); 4005 } 4006 } elseif ( $this->is_tag ) { 4007 $tag_id = $this->get( 'tag_id' ); 4008 $tag = $this->get( 'tag' ); 4009 4010 if ( $tag_id ) { 4011 $term = get_term( $tag_id, 'post_tag' ); 4012 } elseif ( $tag ) { 4013 $term = get_term_by( 'slug', $tag, 'post_tag' ); 4014 } 4015 } else { 4016 // For other tax queries, grab the first term from the first clause. 4017 if ( ! empty( $this->tax_query->queried_terms ) ) { 4018 $queried_taxonomies = array_keys( $this->tax_query->queried_terms ); 4019 $matched_taxonomy = reset( $queried_taxonomies ); 4020 $query = $this->tax_query->queried_terms[ $matched_taxonomy ]; 4021 4022 if ( ! empty( $query['terms'] ) ) { 4023 if ( 'term_id' === $query['field'] ) { 4024 $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); 4025 } else { 4026 $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); 4027 } 4028 } 4029 } 4030 } 4031 4032 if ( ! empty( $term ) && ! is_wp_error( $term ) ) { 4033 $this->queried_object = $term; 4034 $this->queried_object_id = (int) $term->term_id; 4035 4036 if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) { 4037 _make_cat_compat( $this->queried_object ); 4038 } 4039 } 4040 } elseif ( $this->is_post_type_archive ) { 4041 $post_type = $this->get( 'post_type' ); 4042 if ( is_array( $post_type ) ) { 4043 $post_type = reset( $post_type ); 4044 } 4045 4046 $this->queried_object = get_post_type_object( $post_type ); 4047 } elseif ( $this->is_posts_page ) { 4048 $posts_page = get_post( get_option( 'page_for_posts' ) ); 4049 if ( $posts_page ) { 4050 $this->queried_object = $posts_page; 4051 $this->queried_object_id = (int) $posts_page->ID; 4052 } 4053 } elseif ( $this->is_singular && ! empty( $this->post ) ) { 4054 $this->queried_object = $this->post; 4055 $this->queried_object_id = (int) $this->post->ID; 4056 } elseif ( $this->is_author ) { 4057 $author = (int) $this->get( 'author' ); 4058 $author_name = $this->get( 'author_name' ); 4059 4060 if ( $author ) { 4061 $this->queried_object_id = $author; 4062 } elseif ( $author_name ) { 4063 $user = get_user_by( 'slug', $author_name ); 4064 if ( $user ) { 4065 $this->queried_object_id = $user->ID; 4066 } 4067 } 4068 4069 if ( $this->queried_object_id ) { 4070 $user = get_userdata( $this->queried_object_id ); 4071 if ( $user ) { 4072 $this->queried_object = $user; 4073 } 4074 } 4075 } 4076 4077 return $this->queried_object; 4078 } 4079 4080 /** 4081 * Retrieves the ID of the currently queried object. 4082 * 4083 * @since 1.5.0 4084 * 4085 * @return int 4086 */ 4087 public function get_queried_object_id() { 4088 $this->get_queried_object(); 4089 return $this->queried_object_id ?? 0; 4090 } 4091 4092 /** 4093 * Constructor. 4094 * 4095 * Sets up the WordPress query, if parameter is not empty. 4096 * 4097 * @since 1.5.0 4098 * 4099 * @see WP_Query::parse_query() for all available arguments. 4100 * 4101 * @param string|array $query URL query string or array of vars. 4102 */ 4103 public function __construct( $query = '' ) { 4104 if ( ! empty( $query ) ) { 4105 $this->query( $query ); 4106 } 4107 } 4108 4109 /** 4110 * Makes private properties readable for backward compatibility. 4111 * 4112 * @since 4.0.0 4113 * 4114 * @param string $name Property to get. 4115 * @return mixed Property. 4116 */ 4117 public function __get( $name ) { 4118 if ( in_array( $name, $this->compat_fields, true ) ) { 4119 return $this->$name; 4120 } 4121 } 4122 4123 /** 4124 * Makes private properties checkable for backward compatibility. 4125 * 4126 * @since 4.0.0 4127 * 4128 * @param string $name Property to check if set. 4129 * @return bool Whether the property is set. 4130 */ 4131 public function __isset( $name ) { 4132 if ( in_array( $name, $this->compat_fields, true ) ) { 4133 return isset( $this->$name ); 4134 } 4135 4136 return false; 4137 } 4138 4139 /** 4140 * Makes private/protected methods readable for backward compatibility. 4141 * 4142 * @since 4.0.0 4143 * 4144 * @param string $name Method to call. 4145 * @param array $arguments Arguments to pass when calling. 4146 * @return mixed|false Return value of the callback, false otherwise. 4147 */ 4148 public function __call( $name, $arguments ) { 4149 if ( in_array( $name, $this->compat_methods, true ) ) { 4150 return $this->$name( ...$arguments ); 4151 } 4152 return false; 4153 } 4154 4155 /** 4156 * Determines whether the query is for an existing archive page. 4157 * 4158 * Archive pages include category, tag, author, date, custom post type, 4159 * and custom taxonomy based archives. 4160 * 4161 * @since 3.1.0 4162 * 4163 * @see WP_Query::is_category() 4164 * @see WP_Query::is_tag() 4165 * @see WP_Query::is_author() 4166 * @see WP_Query::is_date() 4167 * @see WP_Query::is_post_type_archive() 4168 * @see WP_Query::is_tax() 4169 * 4170 * @return bool Whether the query is for an existing archive page. 4171 */ 4172 public function is_archive() { 4173 return (bool) $this->is_archive; 4174 } 4175 4176 /** 4177 * Determines whether the query is for an existing post type archive page. 4178 * 4179 * @since 3.1.0 4180 * 4181 * @param string|string[] $post_types Optional. Post type or array of posts types 4182 * to check against. Default empty. 4183 * @return bool Whether the query is for an existing post type archive page. 4184 */ 4185 public function is_post_type_archive( $post_types = '' ) { 4186 if ( empty( $post_types ) || ! $this->is_post_type_archive ) { 4187 return (bool) $this->is_post_type_archive; 4188 } 4189 4190 $post_type = $this->get( 'post_type' ); 4191 if ( is_array( $post_type ) ) { 4192 $post_type = reset( $post_type ); 4193 } 4194 $post_type_object = get_post_type_object( $post_type ); 4195 4196 if ( ! $post_type_object ) { 4197 return false; 4198 } 4199 4200 return in_array( $post_type_object->name, (array) $post_types, true ); 4201 } 4202 4203 /** 4204 * Determines whether the query is for an existing attachment page. 4205 * 4206 * @since 3.1.0 4207 * 4208 * @param int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such 4209 * to check against. Default empty. 4210 * @return bool Whether the query is for an existing attachment page. 4211 */ 4212 public function is_attachment( $attachment = '' ) { 4213 if ( ! $this->is_attachment ) { 4214 return false; 4215 } 4216 4217 if ( empty( $attachment ) ) { 4218 return true; 4219 } 4220 4221 $attachment = array_map( 'strval', (array) $attachment ); 4222 4223 $post_obj = $this->get_queried_object(); 4224 if ( ! $post_obj ) { 4225 return false; 4226 } 4227 4228 if ( in_array( (string) $post_obj->ID, $attachment, true ) ) { 4229 return true; 4230 } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) { 4231 return true; 4232 } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) { 4233 return true; 4234 } 4235 return false; 4236 } 4237 4238 /** 4239 * Determines whether the query is for an existing author archive page. 4240 * 4241 * If the $author parameter is specified, this function will additionally 4242 * check if the query is for one of the authors specified. 4243 * 4244 * @since 3.1.0 4245 * 4246 * @param int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such 4247 * to check against. Default empty. 4248 * @return bool Whether the query is for an existing author archive page. 4249 */ 4250 public function is_author( $author = '' ) { 4251 if ( ! $this->is_author ) { 4252 return false; 4253 } 4254 4255 if ( empty( $author ) ) { 4256 return true; 4257 } 4258 4259 $author_obj = $this->get_queried_object(); 4260 if ( ! $author_obj ) { 4261 return false; 4262 } 4263 4264 $author = array_map( 'strval', (array) $author ); 4265 4266 if ( in_array( (string) $author_obj->ID, $author, true ) ) { 4267 return true; 4268 } elseif ( in_array( $author_obj->nickname, $author, true ) ) { 4269 return true; 4270 } elseif ( in_array( $author_obj->user_nicename, $author, true ) ) { 4271 return true; 4272 } 4273 4274 return false; 4275 } 4276 4277 /** 4278 * Determines whether the query is for an existing category archive page. 4279 * 4280 * If the $category parameter is specified, this function will additionally 4281 * check if the query is for one of the categories specified. 4282 * 4283 * @since 3.1.0 4284 * 4285 * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such 4286 * to check against. Default empty. 4287 * @return bool Whether the query is for an existing category archive page. 4288 */ 4289 public function is_category( $category = '' ) { 4290 if ( ! $this->is_category ) { 4291 return false; 4292 } 4293 4294 if ( empty( $category ) ) { 4295 return true; 4296 } 4297 4298 $cat_obj = $this->get_queried_object(); 4299 if ( ! $cat_obj ) { 4300 return false; 4301 } 4302 4303 $category = array_map( 'strval', (array) $category ); 4304 4305 if ( in_array( (string) $cat_obj->term_id, $category, true ) ) { 4306 return true; 4307 } elseif ( in_array( $cat_obj->name, $category, true ) ) { 4308 return true; 4309 } elseif ( in_array( $cat_obj->slug, $category, true ) ) { 4310 return true; 4311 } 4312 4313 return false; 4314 } 4315 4316 /** 4317 * Determines whether the query is for an existing tag archive page. 4318 * 4319 * If the $tag parameter is specified, this function will additionally 4320 * check if the query is for one of the tags specified. 4321 * 4322 * @since 3.1.0 4323 * 4324 * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such 4325 * to check against. Default empty. 4326 * @return bool Whether the query is for an existing tag archive page. 4327 */ 4328 public function is_tag( $tag = '' ) { 4329 if ( ! $this->is_tag ) { 4330 return false; 4331 } 4332 4333 if ( empty( $tag ) ) { 4334 return true; 4335 } 4336 4337 $tag_obj = $this->get_queried_object(); 4338 if ( ! $tag_obj ) { 4339 return false; 4340 } 4341 4342 $tag = array_map( 'strval', (array) $tag ); 4343 4344 if ( in_array( (string) $tag_obj->term_id, $tag, true ) ) { 4345 return true; 4346 } elseif ( in_array( $tag_obj->name, $tag, true ) ) { 4347 return true; 4348 } elseif ( in_array( $tag_obj->slug, $tag, true ) ) { 4349 return true; 4350 } 4351 4352 return false; 4353 } 4354 4355 /** 4356 * Determines whether the query is for an existing custom taxonomy archive page. 4357 * 4358 * If the $taxonomy parameter is specified, this function will additionally 4359 * check if the query is for that specific $taxonomy. 4360 * 4361 * If the $term parameter is specified in addition to the $taxonomy parameter, 4362 * this function will additionally check if the query is for one of the terms 4363 * specified. 4364 * 4365 * @since 3.1.0 4366 * 4367 * @global WP_Taxonomy[] $wp_taxonomies Registered taxonomies. 4368 * 4369 * @param string|string[] $taxonomy Optional. Taxonomy slug or slugs to check against. 4370 * Default empty. 4371 * @param int|string|int[]|string[] $term Optional. Term ID, name, slug, or array of such 4372 * to check against. Default empty. 4373 * @return bool Whether the query is for an existing custom taxonomy archive page. 4374 * True for custom taxonomy archive pages, false for built-in taxonomies 4375 * (category and tag archives). 4376 */ 4377 public function is_tax( $taxonomy = '', $term = '' ) { 4378 global $wp_taxonomies; 4379 4380 if ( ! $this->is_tax ) { 4381 return false; 4382 } 4383 4384 if ( empty( $taxonomy ) ) { 4385 return true; 4386 } 4387 4388 $queried_object = $this->get_queried_object(); 4389 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy ); 4390 $term_array = (array) $term; 4391 4392 // Check that the taxonomy matches. 4393 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array, true ) ) ) { 4394 return false; 4395 } 4396 4397 // Only a taxonomy provided. 4398 if ( empty( $term ) ) { 4399 return true; 4400 } 4401 4402 return isset( $queried_object->term_id ) && 4403 count( 4404 array_intersect( 4405 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ), 4406 $term_array 4407 ) 4408 ); 4409 } 4410 4411 /** 4412 * Determines whether the current URL is within the comments popup window. 4413 * 4414 * @since 3.1.0 4415 * @deprecated 4.5.0 4416 * 4417 * @return false Always returns false. 4418 */ 4419 public function is_comments_popup() { 4420 _deprecated_function( __FUNCTION__, '4.5.0' ); 4421 4422 return false; 4423 } 4424 4425 /** 4426 * Determines whether the query is for an existing date archive. 4427 * 4428 * @since 3.1.0 4429 * 4430 * @return bool Whether the query is for an existing date archive. 4431 */ 4432 public function is_date() { 4433 return (bool) $this->is_date; 4434 } 4435 4436 /** 4437 * Determines whether the query is for an existing day archive. 4438 * 4439 * @since 3.1.0 4440 * 4441 * @return bool Whether the query is for an existing day archive. 4442 */ 4443 public function is_day() { 4444 return (bool) $this->is_day; 4445 } 4446 4447 /** 4448 * Determines whether the query is for a feed. 4449 * 4450 * @since 3.1.0 4451 * 4452 * @param string|string[] $feeds Optional. Feed type or array of feed types 4453 * to check against. Default empty. 4454 * @return bool Whether the query is for a feed. 4455 */ 4456 public function is_feed( $feeds = '' ) { 4457 if ( empty( $feeds ) || ! $this->is_feed ) { 4458 return (bool) $this->is_feed; 4459 } 4460 4461 $query_var = $this->get( 'feed' ); 4462 if ( 'feed' === $query_var ) { 4463 $query_var = get_default_feed(); 4464 } 4465 4466 return in_array( $query_var, (array) $feeds, true ); 4467 } 4468 4469 /** 4470 * Determines whether the query is for a comments feed. 4471 * 4472 * @since 3.1.0 4473 * 4474 * @return bool Whether the query is for a comments feed. 4475 */ 4476 public function is_comment_feed() { 4477 return (bool) $this->is_comment_feed; 4478 } 4479 4480 /** 4481 * Determines whether the query is for the front page of the site. 4482 * 4483 * This is for what is displayed at your site's main URL. 4484 * 4485 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. 4486 * 4487 * If you set a static page for the front page of your site, this function will return 4488 * true when viewing that page. 4489 * 4490 * Otherwise the same as {@see WP_Query::is_home()}. 4491 * 4492 * @since 3.1.0 4493 * 4494 * @return bool Whether the query is for the front page of the site. 4495 */ 4496 public function is_front_page() { 4497 // Most likely case. 4498 if ( 'posts' === get_option( 'show_on_front' ) && $this->is_home() ) { 4499 return true; 4500 } elseif ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) 4501 && $this->is_page( get_option( 'page_on_front' ) ) 4502 ) { 4503 return true; 4504 } else { 4505 return false; 4506 } 4507 } 4508 4509 /** 4510 * Determines whether the query is for the blog homepage. 4511 * 4512 * This is the page which shows the time based blog content of your site. 4513 * 4514 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'. 4515 * 4516 * If you set a static page for the front page of your site, this function will return 4517 * true only on the page you set as the "Posts page". 4518 * 4519 * @since 3.1.0 4520 * 4521 * @see WP_Query::is_front_page() 4522 * 4523 * @return bool Whether the query is for the blog homepage. 4524 */ 4525 public function is_home() { 4526 return (bool) $this->is_home; 4527 } 4528 4529 /** 4530 * Determines whether the query is for the Privacy Policy page. 4531 * 4532 * This is the page which shows the Privacy Policy content of your site. 4533 * 4534 * Depends on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'. 4535 * 4536 * This function will return true only on the page you set as the "Privacy Policy page". 4537 * 4538 * @since 5.2.0 4539 * 4540 * @return bool Whether the query is for the Privacy Policy page. 4541 */ 4542 public function is_privacy_policy() { 4543 if ( get_option( 'wp_page_for_privacy_policy' ) 4544 && $this->is_page( get_option( 'wp_page_for_privacy_policy' ) ) 4545 ) { 4546 return true; 4547 } else { 4548 return false; 4549 } 4550 } 4551 4552 /** 4553 * Determines whether the query is for an existing month archive. 4554 * 4555 * @since 3.1.0 4556 * 4557 * @return bool Whether the query is for an existing month archive. 4558 */ 4559 public function is_month() { 4560 return (bool) $this->is_month; 4561 } 4562 4563 /** 4564 * Determines whether the query is for an existing single page. 4565 * 4566 * If the $page parameter is specified, this function will additionally 4567 * check if the query is for one of the pages specified. 4568 * 4569 * @since 3.1.0 4570 * 4571 * @see WP_Query::is_single() 4572 * @see WP_Query::is_singular() 4573 * 4574 * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, path, or array of such 4575 * to check against. Default empty. 4576 * @return bool Whether the query is for an existing single page. 4577 */ 4578 public function is_page( $page = '' ) { 4579 if ( ! $this->is_page ) { 4580 return false; 4581 } 4582 4583 if ( empty( $page ) ) { 4584 return true; 4585 } 4586 4587 $page_obj = $this->get_queried_object(); 4588 if ( ! $page_obj ) { 4589 return false; 4590 } 4591 4592 $page = array_map( 'strval', (array) $page ); 4593 4594 if ( in_array( (string) $page_obj->ID, $page, true ) ) { 4595 return true; 4596 } elseif ( in_array( $page_obj->post_title, $page, true ) ) { 4597 return true; 4598 } elseif ( in_array( $page_obj->post_name, $page, true ) ) { 4599 return true; 4600 } else { 4601 foreach ( $page as $pagepath ) { 4602 if ( ! strpos( $pagepath, '/' ) ) { 4603 continue; 4604 } 4605 4606 $pagepath_obj = get_page_by_path( $pagepath ); 4607 4608 if ( $pagepath_obj && ( $pagepath_obj->ID === $page_obj->ID ) ) { 4609 return true; 4610 } 4611 } 4612 } 4613 4614 return false; 4615 } 4616 4617 /** 4618 * Determines whether the query is for a paged result and not for the first page. 4619 * 4620 * @since 3.1.0 4621 * 4622 * @return bool Whether the query is for a paged result. 4623 */ 4624 public function is_paged() { 4625 return (bool) $this->is_paged; 4626 } 4627 4628 /** 4629 * Determines whether the query is for a post or page preview. 4630 * 4631 * @since 3.1.0 4632 * 4633 * @return bool Whether the query is for a post or page preview. 4634 */ 4635 public function is_preview() { 4636 return (bool) $this->is_preview; 4637 } 4638 4639 /** 4640 * Determines whether the query is for the robots.txt file. 4641 * 4642 * @since 3.1.0 4643 * 4644 * @return bool Whether the query is for the robots.txt file. 4645 */ 4646 public function is_robots() { 4647 return (bool) $this->is_robots; 4648 } 4649 4650 /** 4651 * Determines whether the query is for the favicon.ico file. 4652 * 4653 * @since 5.4.0 4654 * 4655 * @return bool Whether the query is for the favicon.ico file. 4656 */ 4657 public function is_favicon() { 4658 return (bool) $this->is_favicon; 4659 } 4660 4661 /** 4662 * Determines whether the query is for a sitemap. 4663 * 4664 * @since 7.1.0 4665 * 4666 * @return bool Whether the query is for a sitemap. 4667 */ 4668 public function is_sitemap(): bool { 4669 return $this->is_sitemap; 4670 } 4671 4672 /** 4673 * Determines whether the query is for a search. 4674 * 4675 * @since 3.1.0 4676 * 4677 * @return bool Whether the query is for a search. 4678 */ 4679 public function is_search() { 4680 return (bool) $this->is_search; 4681 } 4682 4683 /** 4684 * Determines whether the query is for an existing single post. 4685 * 4686 * Works for any post type excluding pages. 4687 * 4688 * If the $post parameter is specified, this function will additionally 4689 * check if the query is for one of the Posts specified. 4690 * 4691 * @since 3.1.0 4692 * 4693 * @see WP_Query::is_page() 4694 * @see WP_Query::is_singular() 4695 * 4696 * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, path, or array of such 4697 * to check against. Default empty. 4698 * @return bool Whether the query is for an existing single post. 4699 */ 4700 public function is_single( $post = '' ) { 4701 if ( ! $this->is_single ) { 4702 return false; 4703 } 4704 4705 if ( empty( $post ) ) { 4706 return true; 4707 } 4708 4709 $post_obj = $this->get_queried_object(); 4710 if ( ! $post_obj ) { 4711 return false; 4712 } 4713 4714 $post = array_map( 'strval', (array) $post ); 4715 4716 if ( in_array( (string) $post_obj->ID, $post, true ) ) { 4717 return true; 4718 } elseif ( in_array( $post_obj->post_title, $post, true ) ) { 4719 return true; 4720 } elseif ( in_array( $post_obj->post_name, $post, true ) ) { 4721 return true; 4722 } else { 4723 foreach ( $post as $postpath ) { 4724 if ( ! strpos( $postpath, '/' ) ) { 4725 continue; 4726 } 4727 4728 $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); 4729 4730 if ( $postpath_obj && ( $postpath_obj->ID === $post_obj->ID ) ) { 4731 return true; 4732 } 4733 } 4734 } 4735 return false; 4736 } 4737 4738 /** 4739 * Determines whether the query is for an existing single post of any post type 4740 * (post, attachment, page, custom post types). 4741 * 4742 * If the $post_types parameter is specified, this function will additionally 4743 * check if the query is for one of the Posts Types specified. 4744 * 4745 * @since 3.1.0 4746 * 4747 * @see WP_Query::is_page() 4748 * @see WP_Query::is_single() 4749 * 4750 * @param string|string[] $post_types Optional. Post type or array of post types 4751 * to check against. Default empty. 4752 * @return bool Whether the query is for an existing single post 4753 * or any of the given post types. 4754 */ 4755 public function is_singular( $post_types = '' ) { 4756 if ( empty( $post_types ) || ! $this->is_singular ) { 4757 return (bool) $this->is_singular; 4758 } 4759 4760 $post_obj = $this->get_queried_object(); 4761 if ( ! $post_obj ) { 4762 return false; 4763 } 4764 4765 return in_array( $post_obj->post_type, (array) $post_types, true ); 4766 } 4767 4768 /** 4769 * Determines whether the query is for a specific time. 4770 * 4771 * @since 3.1.0 4772 * 4773 * @return bool Whether the query is for a specific time. 4774 */ 4775 public function is_time() { 4776 return (bool) $this->is_time; 4777 } 4778 4779 /** 4780 * Determines whether the query is for a trackback endpoint call. 4781 * 4782 * @since 3.1.0 4783 * 4784 * @return bool Whether the query is for a trackback endpoint call. 4785 */ 4786 public function is_trackback() { 4787 return (bool) $this->is_trackback; 4788 } 4789 4790 /** 4791 * Determines whether the query is for an existing year archive. 4792 * 4793 * @since 3.1.0 4794 * 4795 * @return bool Whether the query is for an existing year archive. 4796 */ 4797 public function is_year() { 4798 return (bool) $this->is_year; 4799 } 4800 4801 /** 4802 * Determines whether the query is a 404 (returns no results). 4803 * 4804 * @since 3.1.0 4805 * 4806 * @return bool Whether the query is a 404 error. 4807 */ 4808 public function is_404() { 4809 return (bool) $this->is_404; 4810 } 4811 4812 /** 4813 * Determines whether the query is for an embedded post. 4814 * 4815 * @since 4.4.0 4816 * 4817 * @return bool Whether the query is for an embedded post. 4818 */ 4819 public function is_embed() { 4820 return (bool) $this->is_embed; 4821 } 4822 4823 /** 4824 * Determines whether the query is the main query. 4825 * 4826 * @since 3.3.0 4827 * 4828 * @global WP_Query $wp_the_query WordPress Query object. 4829 * 4830 * @return bool Whether the query is the main query. 4831 */ 4832 public function is_main_query() { 4833 global $wp_the_query; 4834 return $wp_the_query === $this; 4835 } 4836 4837 /** 4838 * Sets up global post data. 4839 * 4840 * @since 4.1.0 4841 * @since 4.4.0 Added the ability to pass a post ID to `$post`. 4842 * 4843 * @global int $id 4844 * @global WP_User $authordata 4845 * @global string $currentday 4846 * @global string $currentmonth 4847 * @global int $page 4848 * @global array $pages 4849 * @global int $multipage 4850 * @global int $more 4851 * @global int $numpages 4852 * 4853 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 4854 * @return bool True on success, false on failure. 4855 */ 4856 public function setup_postdata( $post ) { 4857 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages; 4858 4859 if ( ! ( $post instanceof WP_Post ) ) { 4860 $post = get_post( $post ); 4861 } 4862 4863 if ( ! $post ) { 4864 return false; 4865 } 4866 4867 $elements = $this->generate_postdata( $post ); 4868 if ( false === $elements ) { 4869 return false; 4870 } 4871 4872 $id = $elements['id']; 4873 $authordata = $elements['authordata']; 4874 $currentday = $elements['currentday']; 4875 $currentmonth = $elements['currentmonth']; 4876 $page = $elements['page']; 4877 $pages = $elements['pages']; 4878 $multipage = $elements['multipage']; 4879 $more = $elements['more']; 4880 $numpages = $elements['numpages']; 4881 4882 /** 4883 * Fires once the post data has been set up. 4884 * 4885 * @since 2.8.0 4886 * @since 4.1.0 Introduced `$query` parameter. 4887 * 4888 * @param WP_Post $post The Post object (passed by reference). 4889 * @param WP_Query $query The current Query object (passed by reference). 4890 */ 4891 do_action_ref_array( 'the_post', array( &$post, &$this ) ); 4892 4893 return true; 4894 } 4895 4896 /** 4897 * Generates post data. 4898 * 4899 * @since 5.2.0 4900 * 4901 * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 4902 * @return array|false Elements of post or false on failure. 4903 */ 4904 public function generate_postdata( $post ) { 4905 4906 if ( ! ( $post instanceof WP_Post ) ) { 4907 $post = get_post( $post ); 4908 } 4909 4910 if ( ! $post ) { 4911 return false; 4912 } 4913 4914 $id = (int) $post->ID; 4915 4916 $authordata = get_userdata( $post->post_author ); 4917 4918 $currentday = false; 4919 $currentmonth = false; 4920 4921 $post_date = $post->post_date; 4922 if ( ! empty( $post_date ) && '0000-00-00 00:00:00' !== $post_date ) { 4923 // Avoid using mysql2date for performance reasons. 4924 $currentmonth = substr( $post_date, 5, 2 ); 4925 $day = substr( $post_date, 8, 2 ); 4926 $year = substr( $post_date, 2, 2 ); 4927 4928 $currentday = sprintf( '%s.%s.%s', $day, $currentmonth, $year ); 4929 } 4930 4931 $numpages = 1; 4932 $multipage = 0; 4933 $page = $this->get( 'page' ); 4934 if ( ! $page ) { 4935 $page = 1; 4936 } 4937 4938 /* 4939 * Force full post content when viewing the permalink for the $post, 4940 * or when on an RSS feed. Otherwise respect the 'more' tag. 4941 */ 4942 if ( get_queried_object_id() === $post->ID && ( $this->is_page() || $this->is_single() ) ) { 4943 $more = 1; 4944 } elseif ( $this->is_feed() ) { 4945 $more = 1; 4946 } else { 4947 $more = 0; 4948 } 4949 4950 $content = $post->post_content; 4951 if ( str_contains( $content, '<!--nextpage-->' ) ) { 4952 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 4953 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 4954 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 4955 4956 // Remove the nextpage block delimiters, to avoid invalid block structures in the split content. 4957 $content = str_replace( '<!-- wp:nextpage -->', '', $content ); 4958 $content = str_replace( '<!-- /wp:nextpage -->', '', $content ); 4959 4960 // Ignore nextpage at the beginning of the content. 4961 if ( str_starts_with( $content, '<!--nextpage-->' ) ) { 4962 $content = substr( $content, 15 ); 4963 } 4964 4965 $pages = explode( '<!--nextpage-->', $content ); 4966 } else { 4967 $pages = array( $post->post_content ); 4968 } 4969 4970 /** 4971 * Filters the "pages" derived from splitting the post content. 4972 * 4973 * "Pages" are determined by splitting the post content based on the presence 4974 * of `<!-- nextpage -->` tags. 4975 * 4976 * @since 4.4.0 4977 * 4978 * @param string[] $pages Array of "pages" from the post content split by `<!-- nextpage -->` tags. 4979 * @param WP_Post $post Current post object. 4980 */ 4981 $pages = apply_filters( 'content_pagination', $pages, $post ); 4982 4983 $numpages = count( $pages ); 4984 4985 if ( $numpages > 1 ) { 4986 if ( $page > 1 ) { 4987 $more = 1; 4988 } 4989 $multipage = 1; 4990 } else { 4991 $multipage = 0; 4992 } 4993 4994 $elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); 4995 4996 return $elements; 4997 } 4998 4999 /** 5000 * Generates cache key. 5001 * 5002 * @since 6.1.0 5003 * 5004 * @global wpdb $wpdb WordPress database abstraction object. 5005 * 5006 * @param array $args Query arguments. 5007 * @param string $sql SQL statement. 5008 * @return string Cache key. 5009 */ 5010 protected function generate_cache_key( array $args, $sql ) { 5011 global $wpdb; 5012 5013 unset( 5014 $args['cache_results'], 5015 $args['fields'], 5016 $args['lazy_load_term_meta'], 5017 $args['update_post_meta_cache'], 5018 $args['update_post_term_cache'], 5019 $args['update_menu_item_cache'], 5020 $args['suppress_filters'] 5021 ); 5022 5023 if ( empty( $args['post_type'] ) ) { 5024 if ( $this->is_attachment ) { 5025 $args['post_type'] = 'attachment'; 5026 } elseif ( $this->is_page ) { 5027 $args['post_type'] = 'page'; 5028 } else { 5029 $args['post_type'] = 'post'; 5030 } 5031 } elseif ( 'any' === $args['post_type'] ) { 5032 $args['post_type'] = array_values( get_post_types( array( 'exclude_from_search' => false ) ) ); 5033 } 5034 $args['post_type'] = (array) $args['post_type']; 5035 // Sort post types to ensure same cache key generation. 5036 sort( $args['post_type'] ); 5037 5038 /* 5039 * Sort arrays that can be used for ordering prior to cache key generation. 5040 * 5041 * These arrays are sorted in the query generator for the purposes of the 5042 * WHERE clause but the arguments are not modified as they can be used for 5043 * the orderby clause. 5044 * 5045 * Their use in the orderby clause will generate a different SQL query so 5046 * they can be sorted for the cache key generation. 5047 */ 5048 $sortable_arrays_with_int_values = array( 5049 'post__in', 5050 'post_parent__in', 5051 ); 5052 foreach ( $sortable_arrays_with_int_values as $key ) { 5053 if ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) ) { 5054 $args[ $key ] = array_unique( array_map( 'absint', $args[ $key ] ) ); 5055 sort( $args[ $key ] ); 5056 } 5057 } 5058 5059 // Sort and unique the 'post_name__in' for cache key generation. 5060 if ( isset( $args['post_name__in'] ) && is_array( $args['post_name__in'] ) ) { 5061 $args['post_name__in'] = array_unique( $args['post_name__in'] ); 5062 sort( $args['post_name__in'] ); 5063 } 5064 5065 if ( isset( $args['post_status'] ) ) { 5066 $args['post_status'] = (array) $args['post_status']; 5067 // Sort post status to ensure same cache key generation. 5068 sort( $args['post_status'] ); 5069 } 5070 5071 // Add a default orderby value of date to ensure same cache key generation. 5072 if ( ! isset( $args['orderby'] ) ) { 5073 $args['orderby'] = 'date'; 5074 } 5075 5076 $placeholder = $wpdb->placeholder_escape(); 5077 array_walk_recursive( 5078 $args, 5079 /* 5080 * Replace wpdb placeholders with the string used in the database 5081 * query to avoid unreachable cache keys. This is necessary because 5082 * the placeholder is randomly generated in each request. 5083 * 5084 * $value is passed by reference to allow it to be modified. 5085 * array_walk_recursive() does not return an array. 5086 */ 5087 static function ( &$value ) use ( $wpdb, $placeholder ) { 5088 if ( is_string( $value ) && str_contains( $value, $placeholder ) ) { 5089 $value = $wpdb->remove_placeholder_escape( $value ); 5090 } 5091 } 5092 ); 5093 5094 ksort( $args ); 5095 5096 // Replace wpdb placeholder in the SQL statement used by the cache key. 5097 $sql = $wpdb->remove_placeholder_escape( $sql ); 5098 $key = md5( serialize( $args ) . $sql ); 5099 5100 $this->query_cache_key = "wp_query:$key"; 5101 return $this->query_cache_key; 5102 } 5103 5104 /** 5105 * After looping through a nested query, this function 5106 * restores the $post global to the current post in this query. 5107 * 5108 * @since 3.7.0 5109 * 5110 * @global WP_Post $post Global post object. 5111 */ 5112 public function reset_postdata() { 5113 if ( ! empty( $this->post ) ) { 5114 $GLOBALS['post'] = $this->post; 5115 $this->setup_postdata( $this->post ); 5116 } 5117 } 5118 5119 /** 5120 * Lazyloads term meta for posts in the loop. 5121 * 5122 * @since 4.4.0 5123 * @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload(). 5124 * 5125 * @param mixed $check 5126 * @param int $term_id 5127 * @return mixed 5128 */ 5129 public function lazyload_term_meta( $check, $term_id ) { 5130 _deprecated_function( __METHOD__, '4.5.0' ); 5131 return $check; 5132 } 5133 5134 /** 5135 * Lazyloads comment meta for comments in the loop. 5136 * 5137 * @since 4.4.0 5138 * @deprecated 4.5.0 See wp_lazyload_comment_meta(). 5139 * 5140 * @param mixed $check 5141 * @param int $comment_id 5142 * @return mixed 5143 */ 5144 public function lazyload_comment_meta( $check, $comment_id ) { 5145 _deprecated_function( __METHOD__, '4.5.0' ); 5146 return $check; 5147 } 5148 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Aug 13 08:20:23 2026 | Cross-referenced by PHPXref |