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