/wp-includes/class-wp-query.php

  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://codex.wordpress.org/Function_Reference/WP_Query Codex page. 
  14. * 
  15. * @since 1.5.0 
  16. * @since 4.5.0 Removed the `$comments_popup` property. 
  17. */ 
  18. class WP_Query { 
  19.  
  20. /** 
  21. * Query vars set by the user 
  22. * 
  23. * @since 1.5.0 
  24. * @access public 
  25. * @var array 
  26. */ 
  27. public $query; 
  28.  
  29. /** 
  30. * Query vars, after parsing 
  31. * 
  32. * @since 1.5.0 
  33. * @access public 
  34. * @var array 
  35. */ 
  36. public $query_vars = array(); 
  37.  
  38. /** 
  39. * Taxonomy query, as passed to get_tax_sql() 
  40. * 
  41. * @since 3.1.0 
  42. * @access public 
  43. * @var object WP_Tax_Query 
  44. */ 
  45. public $tax_query; 
  46.  
  47. /** 
  48. * Metadata query container 
  49. * 
  50. * @since 3.2.0 
  51. * @access public 
  52. * @var object WP_Meta_Query 
  53. */ 
  54. public $meta_query = false; 
  55.  
  56. /** 
  57. * Date query container 
  58. * 
  59. * @since 3.7.0 
  60. * @access public 
  61. * @var object WP_Date_Query 
  62. */ 
  63. public $date_query = false; 
  64.  
  65. /** 
  66. * Holds the data for a single object that is queried. 
  67. * 
  68. * Holds the contents of a post, page, category, attachment. 
  69. * 
  70. * @since 1.5.0 
  71. * @access public 
  72. * @var object|array 
  73. */ 
  74. public $queried_object; 
  75.  
  76. /** 
  77. * The ID of the queried object. 
  78. * 
  79. * @since 1.5.0 
  80. * @access public 
  81. * @var int 
  82. */ 
  83. public $queried_object_id; 
  84.  
  85. /** 
  86. * Get post database query. 
  87. * 
  88. * @since 2.0.1 
  89. * @access public 
  90. * @var string 
  91. */ 
  92. public $request; 
  93.  
  94. /** 
  95. * List of posts. 
  96. * 
  97. * @since 1.5.0 
  98. * @access public 
  99. * @var array 
  100. */ 
  101. public $posts; 
  102.  
  103. /** 
  104. * The amount of posts for the current query. 
  105. * 
  106. * @since 1.5.0 
  107. * @access public 
  108. * @var int 
  109. */ 
  110. public $post_count = 0; 
  111.  
  112. /** 
  113. * Index of the current item in the loop. 
  114. * 
  115. * @since 1.5.0 
  116. * @access public 
  117. * @var int 
  118. */ 
  119. public $current_post = -1; 
  120.  
  121. /** 
  122. * Whether the loop has started and the caller is in the loop. 
  123. * 
  124. * @since 2.0.0 
  125. * @access public 
  126. * @var bool 
  127. */ 
  128. public $in_the_loop = false; 
  129.  
  130. /** 
  131. * The current post. 
  132. * 
  133. * @since 1.5.0 
  134. * @access public 
  135. * @var WP_Post 
  136. */ 
  137. public $post; 
  138.  
  139. /** 
  140. * The list of comments for current post. 
  141. * 
  142. * @since 2.2.0 
  143. * @access public 
  144. * @var array 
  145. */ 
  146. public $comments; 
  147.  
  148. /** 
  149. * The amount of comments for the posts. 
  150. * 
  151. * @since 2.2.0 
  152. * @access public 
  153. * @var int 
  154. */ 
  155. public $comment_count = 0; 
  156.  
  157. /** 
  158. * The index of the comment in the comment loop. 
  159. * 
  160. * @since 2.2.0 
  161. * @access public 
  162. * @var int 
  163. */ 
  164. public $current_comment = -1; 
  165.  
  166. /** 
  167. * Current comment ID. 
  168. * 
  169. * @since 2.2.0 
  170. * @access public 
  171. * @var int 
  172. */ 
  173. public $comment; 
  174.  
  175. /** 
  176. * The amount of found posts for the current query. 
  177. * 
  178. * If limit clause was not used, equals $post_count. 
  179. * 
  180. * @since 2.1.0 
  181. * @access public 
  182. * @var int 
  183. */ 
  184. public $found_posts = 0; 
  185.  
  186. /** 
  187. * The amount of pages. 
  188. * 
  189. * @since 2.1.0 
  190. * @access public 
  191. * @var int 
  192. */ 
  193. public $max_num_pages = 0; 
  194.  
  195. /** 
  196. * The amount of comment pages. 
  197. * 
  198. * @since 2.7.0 
  199. * @access public 
  200. * @var int 
  201. */ 
  202. public $max_num_comment_pages = 0; 
  203.  
  204. /** 
  205. * Set if query is single post. 
  206. * 
  207. * @since 1.5.0 
  208. * @access public 
  209. * @var bool 
  210. */ 
  211. public $is_single = false; 
  212.  
  213. /** 
  214. * Set if query is preview of blog. 
  215. * 
  216. * @since 2.0.0 
  217. * @access public 
  218. * @var bool 
  219. */ 
  220. public $is_preview = false; 
  221.  
  222. /** 
  223. * Set if query returns a page. 
  224. * 
  225. * @since 1.5.0 
  226. * @access public 
  227. * @var bool 
  228. */ 
  229. public $is_page = false; 
  230.  
  231. /** 
  232. * Set if query is an archive list. 
  233. * 
  234. * @since 1.5.0 
  235. * @access public 
  236. * @var bool 
  237. */ 
  238. public $is_archive = false; 
  239.  
  240. /** 
  241. * Set if query is part of a date. 
  242. * 
  243. * @since 1.5.0 
  244. * @access public 
  245. * @var bool 
  246. */ 
  247. public $is_date = false; 
  248.  
  249. /** 
  250. * Set if query contains a year. 
  251. * 
  252. * @since 1.5.0 
  253. * @access public 
  254. * @var bool 
  255. */ 
  256. public $is_year = false; 
  257.  
  258. /** 
  259. * Set if query contains a month. 
  260. * 
  261. * @since 1.5.0 
  262. * @access public 
  263. * @var bool 
  264. */ 
  265. public $is_month = false; 
  266.  
  267. /** 
  268. * Set if query contains a day. 
  269. * 
  270. * @since 1.5.0 
  271. * @access public 
  272. * @var bool 
  273. */ 
  274. public $is_day = false; 
  275.  
  276. /** 
  277. * Set if query contains time. 
  278. * 
  279. * @since 1.5.0 
  280. * @access public 
  281. * @var bool 
  282. */ 
  283. public $is_time = false; 
  284.  
  285. /** 
  286. * Set if query contains an author. 
  287. * 
  288. * @since 1.5.0 
  289. * @access public 
  290. * @var bool 
  291. */ 
  292. public $is_author = false; 
  293.  
  294. /** 
  295. * Set if query contains category. 
  296. * 
  297. * @since 1.5.0 
  298. * @access public 
  299. * @var bool 
  300. */ 
  301. public $is_category = false; 
  302.  
  303. /** 
  304. * Set if query contains tag. 
  305. * 
  306. * @since 2.3.0 
  307. * @access public 
  308. * @var bool 
  309. */ 
  310. public $is_tag = false; 
  311.  
  312. /** 
  313. * Set if query contains taxonomy. 
  314. * 
  315. * @since 2.5.0 
  316. * @access public 
  317. * @var bool 
  318. */ 
  319. public $is_tax = false; 
  320.  
  321. /** 
  322. * Set if query was part of a search result. 
  323. * 
  324. * @since 1.5.0 
  325. * @access public 
  326. * @var bool 
  327. */ 
  328. public $is_search = false; 
  329.  
  330. /** 
  331. * Set if query is feed display. 
  332. * 
  333. * @since 1.5.0 
  334. * @access public 
  335. * @var bool 
  336. */ 
  337. public $is_feed = false; 
  338.  
  339. /** 
  340. * Set if query is comment feed display. 
  341. * 
  342. * @since 2.2.0 
  343. * @access public 
  344. * @var bool 
  345. */ 
  346. public $is_comment_feed = false; 
  347.  
  348. /** 
  349. * Set if query is trackback. 
  350. * 
  351. * @since 1.5.0 
  352. * @access public 
  353. * @var bool 
  354. */ 
  355. public $is_trackback = false; 
  356.  
  357. /** 
  358. * Set if query is blog homepage. 
  359. * 
  360. * @since 1.5.0 
  361. * @access public 
  362. * @var bool 
  363. */ 
  364. public $is_home = false; 
  365.  
  366. /** 
  367. * Set if query couldn't found anything. 
  368. * 
  369. * @since 1.5.0 
  370. * @access public 
  371. * @var bool 
  372. */ 
  373. public $is_404 = false; 
  374.  
  375. /** 
  376. * Set if query is embed. 
  377. * 
  378. * @since 4.4.0 
  379. * @access public 
  380. * @var bool 
  381. */ 
  382. public $is_embed = false; 
  383.  
  384. /** 
  385. * Set if query is paged 
  386. * 
  387. * @since 1.5.0 
  388. * @access public 
  389. * @var bool 
  390. */ 
  391. public $is_paged = false; 
  392.  
  393. /** 
  394. * Set if query is part of administration page. 
  395. * 
  396. * @since 1.5.0 
  397. * @access public 
  398. * @var bool 
  399. */ 
  400. public $is_admin = false; 
  401.  
  402. /** 
  403. * Set if query is an attachment. 
  404. * 
  405. * @since 2.0.0 
  406. * @access public 
  407. * @var bool 
  408. */ 
  409. public $is_attachment = false; 
  410.  
  411. /** 
  412. * Set if is single, is a page, or is an attachment. 
  413. * 
  414. * @since 2.1.0 
  415. * @access public 
  416. * @var bool 
  417. */ 
  418. public $is_singular = false; 
  419.  
  420. /** 
  421. * Set if query is for robots. 
  422. * 
  423. * @since 2.1.0 
  424. * @access public 
  425. * @var bool 
  426. */ 
  427. public $is_robots = false; 
  428.  
  429. /** 
  430. * Set if query contains posts. 
  431. * 
  432. * Basically, the homepage if the option isn't set for the static homepage. 
  433. * 
  434. * @since 2.1.0 
  435. * @access public 
  436. * @var bool 
  437. */ 
  438. public $is_posts_page = false; 
  439.  
  440. /** 
  441. * Set if query is for a post type archive. 
  442. * 
  443. * @since 3.1.0 
  444. * @access public 
  445. * @var bool 
  446. */ 
  447. public $is_post_type_archive = false; 
  448.  
  449. /** 
  450. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know 
  451. * whether we have to re-parse because something has changed 
  452. * 
  453. * @since 3.1.0 
  454. * @access private 
  455. * @var bool|string 
  456. */ 
  457. private $query_vars_hash = false; 
  458.  
  459. /** 
  460. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made 
  461. * via pre_get_posts hooks. 
  462. * 
  463. * @since 3.1.1 
  464. * @access private 
  465. */ 
  466. private $query_vars_changed = true; 
  467.  
  468. /** 
  469. * Set if post thumbnails are cached 
  470. * 
  471. * @since 3.2.0 
  472. * @access public 
  473. * @var bool 
  474. */ 
  475. public $thumbnails_cached = false; 
  476.  
  477. /** 
  478. * Cached list of search stopwords. 
  479. * 
  480. * @since 3.7.0 
  481. * @var array 
  482. */ 
  483. private $stopwords; 
  484.  
  485. private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' ); 
  486.  
  487. private $compat_methods = array( 'init_query_flags', 'parse_tax_query' ); 
  488.  
  489. /** 
  490. * Resets query flags to false. 
  491. * 
  492. * The query flags are what page info WordPress was able to figure out. 
  493. * 
  494. * @since 2.0.0 
  495. * @access private 
  496. */ 
  497. private function init_query_flags() { 
  498. $this->is_single = false; 
  499. $this->is_preview = false; 
  500. $this->is_page = false; 
  501. $this->is_archive = false; 
  502. $this->is_date = false; 
  503. $this->is_year = false; 
  504. $this->is_month = false; 
  505. $this->is_day = false; 
  506. $this->is_time = false; 
  507. $this->is_author = false; 
  508. $this->is_category = false; 
  509. $this->is_tag = false; 
  510. $this->is_tax = false; 
  511. $this->is_search = false; 
  512. $this->is_feed = false; 
  513. $this->is_comment_feed = false; 
  514. $this->is_trackback = false; 
  515. $this->is_home = false; 
  516. $this->is_404 = false; 
  517. $this->is_paged = false; 
  518. $this->is_admin = false; 
  519. $this->is_attachment = false; 
  520. $this->is_singular = false; 
  521. $this->is_robots = false; 
  522. $this->is_posts_page = false; 
  523. $this->is_post_type_archive = false; 
  524.  
  525. /** 
  526. * Initiates object properties and sets default values. 
  527. * 
  528. * @since 1.5.0 
  529. * @access public 
  530. */ 
  531. public function init() { 
  532. unset($this->posts); 
  533. unset($this->query); 
  534. $this->query_vars = array(); 
  535. unset($this->queried_object); 
  536. unset($this->queried_object_id); 
  537. $this->post_count = 0; 
  538. $this->current_post = -1; 
  539. $this->in_the_loop = false; 
  540. unset( $this->request ); 
  541. unset( $this->post ); 
  542. unset( $this->comments ); 
  543. unset( $this->comment ); 
  544. $this->comment_count = 0; 
  545. $this->current_comment = -1; 
  546. $this->found_posts = 0; 
  547. $this->max_num_pages = 0; 
  548. $this->max_num_comment_pages = 0; 
  549.  
  550. $this->init_query_flags(); 
  551.  
  552. /** 
  553. * Reparse the query vars. 
  554. * 
  555. * @since 1.5.0 
  556. * @access public 
  557. */ 
  558. public function parse_query_vars() { 
  559. $this->parse_query(); 
  560.  
  561. /** 
  562. * Fills in the query variables, which do not exist within the parameter. 
  563. * 
  564. * @since 2.1.0 
  565. * @since 4.4.0 Removed the `comments_popup` public query variable. 
  566. * @access public 
  567. * 
  568. * @param array $array Defined query variables. 
  569. * @return array Complete query variables with undefined ones filled in empty. 
  570. */ 
  571. public function fill_query_vars($array) { 
  572. $keys = array( 
  573. 'error' 
  574. , 'm' 
  575. , 'p' 
  576. , 'post_parent' 
  577. , 'subpost' 
  578. , 'subpost_id' 
  579. , 'attachment' 
  580. , 'attachment_id' 
  581. , 'name' 
  582. , 'static' 
  583. , 'pagename' 
  584. , 'page_id' 
  585. , 'second' 
  586. , 'minute' 
  587. , 'hour' 
  588. , 'day' 
  589. , 'monthnum' 
  590. , 'year' 
  591. , 'w' 
  592. , 'category_name' 
  593. , 'tag' 
  594. , 'cat' 
  595. , 'tag_id' 
  596. , 'author' 
  597. , 'author_name' 
  598. , 'feed' 
  599. , 'tb' 
  600. , 'paged' 
  601. , 'meta_key' 
  602. , 'meta_value' 
  603. , 'preview' 
  604. , 's' 
  605. , 'sentence' 
  606. , 'title' 
  607. , 'fields' 
  608. , 'menu_order' 
  609. , 'embed' 
  610. ); 
  611.  
  612. foreach ( $keys as $key ) { 
  613. if ( !isset($array[$key]) ) 
  614. $array[$key] = ''; 
  615.  
  616. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',  
  617. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',  
  618. 'author__in', 'author__not_in' ); 
  619.  
  620. foreach ( $array_keys as $key ) { 
  621. if ( !isset($array[$key]) ) 
  622. $array[$key] = array(); 
  623. return $array; 
  624.  
  625. /** 
  626. * Parse a query string and set query type booleans. 
  627. * 
  628. * @since 1.5.0 
  629. * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's 
  630. * array key to `$orderby`. 
  631. * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded 
  632. * search terms, by prepending a hyphen. 
  633. * @since 4.5.0 Removed the `$comments_popup` parameter. 
  634. * Introduced the `$comment_status` and `$ping_status` parameters. 
  635. * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts. 
  636. * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument. 
  637. * @access public 
  638. * 
  639. * @param string|array $query { 
  640. * Optional. Array or string of Query parameters. 
  641. * 
  642. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type. 
  643. * @type int|string $author Author ID, or comma-separated list of IDs. 
  644. * @type string $author_name User 'user_nicename'. 
  645. * @type array $author__in An array of author IDs to query from. 
  646. * @type array $author__not_in An array of author IDs not to query from. 
  647. * @type bool $cache_results Whether to cache post information. Default true. 
  648. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children). 
  649. * @type array $category__and An array of category IDs (AND in). 
  650. * @type array $category__in An array of category IDs (OR in, no children). 
  651. * @type array $category__not_in An array of category IDs (NOT in). 
  652. * @type string $category_name Use category slug (not name, this or any children). 
  653. * @type string $comment_status Comment status. 
  654. * @type int $comments_per_page The number of comments to return per page. 
  655. * Default 'comments_per_page' option. 
  656. * @type array $date_query An associative array of WP_Date_Query arguments. 
  657. * See WP_Date_Query::__construct(). 
  658. * @type int $day Day of the month. Default empty. Accepts numbers 1-31. 
  659. * @type bool $exact Whether to search by exact keyword. Default false. 
  660. * @type string|array $fields Which fields to return. Single field or all fields (string),  
  661. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'. 
  662. * Default all fields. Accepts 'ids', 'id=>parent'. 
  663. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23. 
  664. * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false 
  665. * excludes stickies from 'post__in'. Accepts 1|true, 0|false. 
  666. * Default 0|false. 
  667. * @type int $m Combination YearMonth. Accepts any four-digit year and month 
  668. * numbers 1-12. Default empty. 
  669. * @type string $meta_compare Comparison operator to test the 'meta_value'. 
  670. * @type string $meta_key Custom field key. 
  671. * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query. 
  672. * @type string $meta_value Custom field value. 
  673. * @type int $meta_value_num Custom field value number. 
  674. * @type int $menu_order The menu order of the posts. 
  675. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12. 
  676. * @type string $name Post slug. 
  677. * @type bool $nopaging Show all posts (true) or paginate (false). Default false. 
  678. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve 
  679. * performance. Default false. 
  680. * @type int $offset The number of posts to offset before retrieval. 
  681. * @type string $order Designates ascending or descending order of posts. Default 'DESC'. 
  682. * Accepts 'ASC', 'DESC'. 
  683. * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be 
  684. * passed. To use 'meta_value', or 'meta_value_num',  
  685. * 'meta_key=keyname' must be also be defined. To sort by a 
  686. * specific `$meta_query` clause, use that clause's array key. 
  687. * Default 'date'. Accepts 'none', 'name', 'author', 'date',  
  688. * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',  
  689. * 'RAND(x)' (where 'x' is an integer seed value),  
  690. * 'comment_count', 'meta_value', 'meta_value_num', 'post__in',  
  691. * 'post_name__in', 'post_parent__in', and the array keys 
  692. * of `$meta_query`. 
  693. * @type int $p Post ID. 
  694. * @type int $page Show the number of posts that would show up on page X of a 
  695. * static front page. 
  696. * @type int $paged The number of the current page. 
  697. * @type int $page_id Page ID. 
  698. * @type string $pagename Page slug. 
  699. * @type string $perm Show posts if user has the appropriate capability. 
  700. * @type string $ping_status Ping status. 
  701. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included 
  702. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type. 
  703. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma- 
  704. * separated IDs will NOT work. 
  705. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve 
  706. * top-level pages. 
  707. * @type array $post_parent__in An array containing parent page IDs to query child pages from. 
  708. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from. 
  709. * @type string|array $post_type A post type slug (string) or array of post type slugs. 
  710. * Default 'any' if using 'tax_query'. 
  711. * @type string|array $post_status A post status (string) or array of post statuses. 
  712. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts. 
  713. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides 
  714. * 'posts_per_page' when is_archive(), or is_search() are true. 
  715. * @type array $post_name__in An array of post slugs that results must match. 
  716. * @type string $s Search keyword(s). Prepending a term with a hyphen will 
  717. * exclude posts matching that term. Eg, 'pillow -sofa' will 
  718. * return posts containing 'pillow' but not 'sofa'. The 
  719. * character used for exclusion can be modified using the 
  720. * the 'wp_query_search_exclusion_prefix' filter. 
  721. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60. 
  722. * @type bool $sentence Whether to search by phrase. Default false. 
  723. * @type bool $suppress_filters Whether to suppress filters. Default false. 
  724. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all). 
  725. * @type array $tag__and An array of tag ids (AND in). 
  726. * @type array $tag__in An array of tag ids (OR in). 
  727. * @type array $tag__not_in An array of tag ids (NOT in). 
  728. * @type int $tag_id Tag id or comma-separated list of IDs. 
  729. * @type array $tag_slug__and An array of tag slugs (AND in). 
  730. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is 
  731. * true. Note: a string of comma-separated IDs will NOT work. 
  732. * @type array $tax_query An associative array of WP_Tax_Query arguments. 
  733. * See WP_Tax_Query->queries. 
  734. * @type string $title Post title. 
  735. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. 
  736. * @type bool $update_post_term_cache Whether to update the post term cache. Default true. 
  737. * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will 
  738. * disable cache priming for term meta, so that each 
  739. * get_term_meta() call will hit the database. 
  740. * Defaults to the value of `$update_post_term_cache`. 
  741. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. 
  742. * @type int $year The four-digit year. Default empty. Accepts any four-digit year. 
  743. * } 
  744. */ 
  745. public function parse_query( $query = '' ) { 
  746. if ( ! empty( $query ) ) { 
  747. $this->init(); 
  748. $this->query = $this->query_vars = wp_parse_args( $query ); 
  749. } elseif ( ! isset( $this->query ) ) { 
  750. $this->query = $this->query_vars; 
  751.  
  752. $this->query_vars = $this->fill_query_vars($this->query_vars); 
  753. $qv = &$this->query_vars; 
  754. $this->query_vars_changed = true; 
  755.  
  756. if ( ! empty($qv['robots']) ) 
  757. $this->is_robots = true; 
  758.  
  759. if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) { 
  760. $qv['p'] = 0; 
  761. $qv['error'] = '404'; 
  762. } else { 
  763. $qv['p'] = intval( $qv['p'] ); 
  764.  
  765. $qv['page_id'] = absint($qv['page_id']); 
  766. $qv['year'] = absint($qv['year']); 
  767. $qv['monthnum'] = absint($qv['monthnum']); 
  768. $qv['day'] = absint($qv['day']); 
  769. $qv['w'] = absint($qv['w']); 
  770. $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : ''; 
  771. $qv['paged'] = absint($qv['paged']); 
  772. $qv['cat'] = preg_replace( '|[^0-9, -]|', '', $qv['cat'] ); // comma separated list of positive or negative integers 
  773. $qv['author'] = preg_replace( '|[^0-9, -]|', '', $qv['author'] ); // comma separated list of positive or negative integers 
  774. $qv['pagename'] = trim( $qv['pagename'] ); 
  775. $qv['name'] = trim( $qv['name'] ); 
  776. $qv['title'] = trim( $qv['title'] ); 
  777. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']); 
  778. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']); 
  779. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']); 
  780. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']); 
  781.  
  782. // Fairly insane upper bound for search string lengths. 
  783. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) { 
  784. $qv['s'] = ''; 
  785.  
  786. // Compat. Map subpost to attachment. 
  787. if ( '' != $qv['subpost'] ) 
  788. $qv['attachment'] = $qv['subpost']; 
  789. if ( '' != $qv['subpost_id'] ) 
  790. $qv['attachment_id'] = $qv['subpost_id']; 
  791.  
  792. $qv['attachment_id'] = absint($qv['attachment_id']); 
  793.  
  794. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) { 
  795. $this->is_single = true; 
  796. $this->is_attachment = true; 
  797. } elseif ( '' != $qv['name'] ) { 
  798. $this->is_single = true; 
  799. } elseif ( $qv['p'] ) { 
  800. $this->is_single = true; 
  801. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) { 
  802. // If year, month, day, hour, minute, and second are set, a single 
  803. // post is being queried. 
  804. $this->is_single = true; 
  805. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) { 
  806. $this->is_page = true; 
  807. $this->is_single = false; 
  808. } else { 
  809. // Look for archive queries. Dates, categories, authors, search, post type archives. 
  810.  
  811. if ( isset( $this->query['s'] ) ) { 
  812. $this->is_search = true; 
  813.  
  814. if ( '' !== $qv['second'] ) { 
  815. $this->is_time = true; 
  816. $this->is_date = true; 
  817.  
  818. if ( '' !== $qv['minute'] ) { 
  819. $this->is_time = true; 
  820. $this->is_date = true; 
  821.  
  822. if ( '' !== $qv['hour'] ) { 
  823. $this->is_time = true; 
  824. $this->is_date = true; 
  825.  
  826. if ( $qv['day'] ) { 
  827. if ( ! $this->is_date ) { 
  828. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] ); 
  829. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) { 
  830. $qv['error'] = '404'; 
  831. } else { 
  832. $this->is_day = true; 
  833. $this->is_date = true; 
  834.  
  835. if ( $qv['monthnum'] ) { 
  836. if ( ! $this->is_date ) { 
  837. if ( 12 < $qv['monthnum'] ) { 
  838. $qv['error'] = '404'; 
  839. } else { 
  840. $this->is_month = true; 
  841. $this->is_date = true; 
  842.  
  843. if ( $qv['year'] ) { 
  844. if ( ! $this->is_date ) { 
  845. $this->is_year = true; 
  846. $this->is_date = true; 
  847.  
  848. if ( $qv['m'] ) { 
  849. $this->is_date = true; 
  850. if ( strlen($qv['m']) > 9 ) { 
  851. $this->is_time = true; 
  852. } elseif ( strlen( $qv['m'] ) > 7 ) { 
  853. $this->is_day = true; 
  854. } elseif ( strlen( $qv['m'] ) > 5 ) { 
  855. $this->is_month = true; 
  856. } else { 
  857. $this->is_year = true; 
  858.  
  859. if ( '' != $qv['w'] ) { 
  860. $this->is_date = true; 
  861.  
  862. $this->query_vars_hash = false; 
  863. $this->parse_tax_query( $qv ); 
  864.  
  865. foreach ( $this->tax_query->queries as $tax_query ) { 
  866. if ( ! is_array( $tax_query ) ) { 
  867. continue; 
  868.  
  869. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) { 
  870. switch ( $tax_query['taxonomy'] ) { 
  871. case 'category': 
  872. $this->is_category = true; 
  873. break; 
  874. case 'post_tag': 
  875. $this->is_tag = true; 
  876. break; 
  877. default: 
  878. $this->is_tax = true; 
  879. unset( $tax_query ); 
  880.  
  881. if ( empty($qv['author']) || ($qv['author'] == '0') ) { 
  882. $this->is_author = false; 
  883. } else { 
  884. $this->is_author = true; 
  885.  
  886. if ( '' != $qv['author_name'] ) 
  887. $this->is_author = true; 
  888.  
  889. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { 
  890. $post_type_obj = get_post_type_object( $qv['post_type'] ); 
  891. if ( ! empty( $post_type_obj->has_archive ) ) 
  892. $this->is_post_type_archive = true; 
  893.  
  894. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) 
  895. $this->is_archive = true; 
  896.  
  897. if ( '' != $qv['feed'] ) 
  898. $this->is_feed = true; 
  899.  
  900. if ( '' != $qv['embed'] ) { 
  901. $this->is_embed = true; 
  902.  
  903. if ( '' != $qv['tb'] ) 
  904. $this->is_trackback = true; 
  905.  
  906. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) ) 
  907. $this->is_paged = true; 
  908.  
  909. // if we're previewing inside the write screen 
  910. if ( '' != $qv['preview'] ) 
  911. $this->is_preview = true; 
  912.  
  913. if ( is_admin() ) 
  914. $this->is_admin = true; 
  915.  
  916. if ( false !== strpos($qv['feed'], 'comments-') ) { 
  917. $qv['feed'] = str_replace('comments-', '', $qv['feed']); 
  918. $qv['withcomments'] = 1; 
  919.  
  920. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; 
  921.  
  922. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) ) 
  923. $this->is_comment_feed = true; 
  924.  
  925. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots ) ) 
  926. $this->is_home = true; 
  927.  
  928. // Correct is_* for page_on_front and page_for_posts 
  929. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) { 
  930. $_query = wp_parse_args($this->query); 
  931. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename. 
  932. if ( isset($_query['pagename']) && '' == $_query['pagename'] ) 
  933. unset($_query['pagename']); 
  934.  
  935. unset( $_query['embed'] ); 
  936.  
  937. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) { 
  938. $this->is_page = true; 
  939. $this->is_home = false; 
  940. $qv['page_id'] = get_option('page_on_front'); 
  941. // Correct <!--nextpage--> for page_on_front 
  942. if ( !empty($qv['paged']) ) { 
  943. $qv['page'] = $qv['paged']; 
  944. unset($qv['paged']); 
  945.  
  946. if ( '' != $qv['pagename'] ) { 
  947. $this->queried_object = get_page_by_path( $qv['pagename'] ); 
  948.  
  949. if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) { 
  950. if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) { 
  951. // See if we also have a post with the same slug 
  952. $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' ); 
  953. if ( $post ) { 
  954. $this->queried_object = $post; 
  955. $this->is_page = false; 
  956. $this->is_single = true; 
  957.  
  958. if ( ! empty( $this->queried_object ) ) { 
  959. $this->queried_object_id = (int) $this->queried_object->ID; 
  960. } else { 
  961. unset( $this->queried_object ); 
  962.  
  963. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) { 
  964. $this->is_page = false; 
  965. $this->is_home = true; 
  966. $this->is_posts_page = true; 
  967.  
  968. if ( $qv['page_id'] ) { 
  969. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) { 
  970. $this->is_page = false; 
  971. $this->is_home = true; 
  972. $this->is_posts_page = true; 
  973.  
  974. if ( !empty($qv['post_type']) ) { 
  975. if ( is_array($qv['post_type']) ) 
  976. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']); 
  977. else 
  978. $qv['post_type'] = sanitize_key($qv['post_type']); 
  979.  
  980. if ( ! empty( $qv['post_status'] ) ) { 
  981. if ( is_array( $qv['post_status'] ) ) 
  982. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']); 
  983. else 
  984. $qv['post_status'] = preg_replace('|[^a-z0-9_, -]|', '', $qv['post_status']); 
  985.  
  986. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) ) 
  987. $this->is_comment_feed = false; 
  988.  
  989. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; 
  990. // Done correcting is_* for page_on_front and page_for_posts 
  991.  
  992. if ( '404' == $qv['error'] ) 
  993. $this->set_404(); 
  994.  
  995. $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); 
  996.  
  997. $this->query_vars_hash = md5( serialize( $this->query_vars ) ); 
  998. $this->query_vars_changed = false; 
  999.  
  1000. /** 
  1001. * Fires after the main query vars have been parsed. 
  1002. * 
  1003. * @since 1.5.0 
  1004. * 
  1005. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  1006. */ 
  1007. do_action_ref_array( 'parse_query', array( &$this ) ); 
  1008.  
  1009. /** 
  1010. * Parses various taxonomy related query vars. 
  1011. * 
  1012. * For BC, this method is not marked as protected. See [28987]. 
  1013. * 
  1014. * @access protected 
  1015. * @since 3.1.0 
  1016. * 
  1017. * @param array $q The query variables. Passed by reference. 
  1018. */ 
  1019. public function parse_tax_query( &$q ) { 
  1020. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) { 
  1021. $tax_query = $q['tax_query']; 
  1022. } else { 
  1023. $tax_query = array(); 
  1024.  
  1025. if ( !empty($q['taxonomy']) && !empty($q['term']) ) { 
  1026. $tax_query[] = array( 
  1027. 'taxonomy' => $q['taxonomy'],  
  1028. 'terms' => array( $q['term'] ),  
  1029. 'field' => 'slug',  
  1030. ); 
  1031.  
  1032. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) { 
  1033. if ( 'post_tag' == $taxonomy ) 
  1034. continue; // Handled further down in the $q['tag'] block 
  1035.  
  1036. if ( $t->query_var && !empty( $q[$t->query_var] ) ) { 
  1037. $tax_query_defaults = array( 
  1038. 'taxonomy' => $taxonomy,  
  1039. 'field' => 'slug',  
  1040. ); 
  1041.  
  1042. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { 
  1043. $q[$t->query_var] = wp_basename( $q[$t->query_var] ); 
  1044.  
  1045. $term = $q[$t->query_var]; 
  1046.  
  1047. if ( is_array( $term ) ) { 
  1048. $term = implode( ', ', $term ); 
  1049.  
  1050. if ( strpos($term, '+') !== false ) { 
  1051. $terms = preg_split( '/[+]+/', $term ); 
  1052. foreach ( $terms as $term ) { 
  1053. $tax_query[] = array_merge( $tax_query_defaults, array( 
  1054. 'terms' => array( $term ) 
  1055. ) ); 
  1056. } else { 
  1057. $tax_query[] = array_merge( $tax_query_defaults, array( 
  1058. 'terms' => preg_split( '/[, ]+/', $term ) 
  1059. ) ); 
  1060.  
  1061. // If querystring 'cat' is an array, implode it. 
  1062. if ( is_array( $q['cat'] ) ) { 
  1063. $q['cat'] = implode( ', ', $q['cat'] ); 
  1064.  
  1065. // Category stuff 
  1066. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) { 
  1067. $cat_in = $cat_not_in = array(); 
  1068.  
  1069. $cat_array = preg_split( '/[, \s]+/', urldecode( $q['cat'] ) ); 
  1070. $cat_array = array_map( 'intval', $cat_array ); 
  1071. $q['cat'] = implode( ', ', $cat_array ); 
  1072.  
  1073. foreach ( $cat_array as $cat ) { 
  1074. if ( $cat > 0 ) 
  1075. $cat_in[] = $cat; 
  1076. elseif ( $cat < 0 ) 
  1077. $cat_not_in[] = abs( $cat ); 
  1078.  
  1079. if ( ! empty( $cat_in ) ) { 
  1080. $tax_query[] = array( 
  1081. 'taxonomy' => 'category',  
  1082. 'terms' => $cat_in,  
  1083. 'field' => 'term_id',  
  1084. 'include_children' => true 
  1085. ); 
  1086.  
  1087. if ( ! empty( $cat_not_in ) ) { 
  1088. $tax_query[] = array( 
  1089. 'taxonomy' => 'category',  
  1090. 'terms' => $cat_not_in,  
  1091. 'field' => 'term_id',  
  1092. 'operator' => 'NOT IN',  
  1093. 'include_children' => true 
  1094. ); 
  1095. unset( $cat_array, $cat_in, $cat_not_in ); 
  1096.  
  1097. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { 
  1098. $q['category__and'] = (array) $q['category__and']; 
  1099. if ( ! isset( $q['category__in'] ) ) 
  1100. $q['category__in'] = array(); 
  1101. $q['category__in'][] = absint( reset( $q['category__and'] ) ); 
  1102. unset( $q['category__and'] ); 
  1103.  
  1104. if ( ! empty( $q['category__in'] ) ) { 
  1105. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); 
  1106. $tax_query[] = array( 
  1107. 'taxonomy' => 'category',  
  1108. 'terms' => $q['category__in'],  
  1109. 'field' => 'term_id',  
  1110. 'include_children' => false 
  1111. ); 
  1112.  
  1113. if ( ! empty($q['category__not_in']) ) { 
  1114. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); 
  1115. $tax_query[] = array( 
  1116. 'taxonomy' => 'category',  
  1117. 'terms' => $q['category__not_in'],  
  1118. 'operator' => 'NOT IN',  
  1119. 'include_children' => false 
  1120. ); 
  1121.  
  1122. if ( ! empty($q['category__and']) ) { 
  1123. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); 
  1124. $tax_query[] = array( 
  1125. 'taxonomy' => 'category',  
  1126. 'terms' => $q['category__and'],  
  1127. 'field' => 'term_id',  
  1128. 'operator' => 'AND',  
  1129. 'include_children' => false 
  1130. ); 
  1131.  
  1132. // If querystring 'tag' is array, implode it. 
  1133. if ( is_array( $q['tag'] ) ) { 
  1134. $q['tag'] = implode( ', ', $q['tag'] ); 
  1135.  
  1136. // Tag stuff 
  1137. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) { 
  1138. if ( strpos($q['tag'], ', ') !== false ) { 
  1139. $tags = preg_split('/[, \r\n\t ]+/', $q['tag']); 
  1140. foreach ( (array) $tags as $tag ) { 
  1141. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 
  1142. $q['tag_slug__in'][] = $tag; 
  1143. } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { 
  1144. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']); 
  1145. foreach ( (array) $tags as $tag ) { 
  1146. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 
  1147. $q['tag_slug__and'][] = $tag; 
  1148. } else { 
  1149. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); 
  1150. $q['tag_slug__in'][] = $q['tag']; 
  1151.  
  1152. if ( !empty($q['tag_id']) ) { 
  1153. $q['tag_id'] = absint( $q['tag_id'] ); 
  1154. $tax_query[] = array( 
  1155. 'taxonomy' => 'post_tag',  
  1156. 'terms' => $q['tag_id'] 
  1157. ); 
  1158.  
  1159. if ( !empty($q['tag__in']) ) { 
  1160. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) ); 
  1161. $tax_query[] = array( 
  1162. 'taxonomy' => 'post_tag',  
  1163. 'terms' => $q['tag__in'] 
  1164. ); 
  1165.  
  1166. if ( !empty($q['tag__not_in']) ) { 
  1167. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) ); 
  1168. $tax_query[] = array( 
  1169. 'taxonomy' => 'post_tag',  
  1170. 'terms' => $q['tag__not_in'],  
  1171. 'operator' => 'NOT IN' 
  1172. ); 
  1173.  
  1174. if ( !empty($q['tag__and']) ) { 
  1175. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) ); 
  1176. $tax_query[] = array( 
  1177. 'taxonomy' => 'post_tag',  
  1178. 'terms' => $q['tag__and'],  
  1179. 'operator' => 'AND' 
  1180. ); 
  1181.  
  1182. if ( !empty($q['tag_slug__in']) ) { 
  1183. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); 
  1184. $tax_query[] = array( 
  1185. 'taxonomy' => 'post_tag',  
  1186. 'terms' => $q['tag_slug__in'],  
  1187. 'field' => 'slug' 
  1188. ); 
  1189.  
  1190. if ( !empty($q['tag_slug__and']) ) { 
  1191. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); 
  1192. $tax_query[] = array( 
  1193. 'taxonomy' => 'post_tag',  
  1194. 'terms' => $q['tag_slug__and'],  
  1195. 'field' => 'slug',  
  1196. 'operator' => 'AND' 
  1197. ); 
  1198.  
  1199. $this->tax_query = new WP_Tax_Query( $tax_query ); 
  1200.  
  1201. /** 
  1202. * Fires after taxonomy-related query vars have been parsed. 
  1203. * 
  1204. * @since 3.7.0 
  1205. * 
  1206. * @param WP_Query $this The WP_Query instance. 
  1207. */ 
  1208. do_action( 'parse_tax_query', $this ); 
  1209.  
  1210. /** 
  1211. * Generate SQL for the WHERE clause based on passed search terms. 
  1212. * 
  1213. * @since 3.7.0 
  1214. * 
  1215. * @param array $q Query variables. 
  1216. * @return string WHERE clause. 
  1217. */ 
  1218. protected function parse_search( &$q ) { 
  1219. global $wpdb; 
  1220.  
  1221. $search = ''; 
  1222.  
  1223. // added slashes screw with quote grouping when done early, so done later 
  1224. $q['s'] = stripslashes( $q['s'] ); 
  1225. if ( empty( $_GET['s'] ) && $this->is_main_query() ) 
  1226. $q['s'] = urldecode( $q['s'] ); 
  1227. // there are no line breaks in <input /> fields 
  1228. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] ); 
  1229. $q['search_terms_count'] = 1; 
  1230. if ( ! empty( $q['sentence'] ) ) { 
  1231. $q['search_terms'] = array( $q['s'] ); 
  1232. } else { 
  1233. if ( preg_match_all( '/".*?("|$)|((?<=[\t ", +])|^)[^\t ", +]+/', $q['s'], $matches ) ) { 
  1234. $q['search_terms_count'] = count( $matches[0] ); 
  1235. $q['search_terms'] = $this->parse_search_terms( $matches[0] ); 
  1236. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence 
  1237. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) 
  1238. $q['search_terms'] = array( $q['s'] ); 
  1239. } else { 
  1240. $q['search_terms'] = array( $q['s'] ); 
  1241.  
  1242. $n = ! empty( $q['exact'] ) ? '' : '%'; 
  1243. $searchand = ''; 
  1244. $q['search_orderby_title'] = array(); 
  1245.  
  1246. /** 
  1247. * Filters the prefix that indicates that a search term should be excluded from results. 
  1248. * 
  1249. * @since 4.7.0 
  1250. * 
  1251. * @param string $exclusion_prefix The prefix. Default '-'. Returning 
  1252. * an empty value disables exclusions. 
  1253. */ 
  1254. $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); 
  1255.  
  1256. foreach ( $q['search_terms'] as $term ) { 
  1257. // If there is an $exclusion_prefix, terms prefixed with it should be excluded. 
  1258. $exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) ); 
  1259. if ( $exclude ) { 
  1260. $like_op = 'NOT LIKE'; 
  1261. $andor_op = 'AND'; 
  1262. $term = substr( $term, 1 ); 
  1263. } else { 
  1264. $like_op = 'LIKE'; 
  1265. $andor_op = 'OR'; 
  1266.  
  1267. if ( $n && ! $exclude ) { 
  1268. $like = '%' . $wpdb->esc_like( $term ) . '%'; 
  1269. $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like ); 
  1270.  
  1271. $like = $n . $wpdb->esc_like( $term ) . $n; 
  1272. $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like ); 
  1273. $searchand = ' AND '; 
  1274.  
  1275. if ( ! empty( $search ) ) { 
  1276. $search = " AND ({$search}) "; 
  1277. if ( ! is_user_logged_in() ) { 
  1278. $search .= " AND ({$wpdb->posts}.post_password = '') "; 
  1279.  
  1280. return $search; 
  1281.  
  1282. /** 
  1283. * Check if the terms are suitable for searching. 
  1284. * 
  1285. * Uses an array of stopwords (terms) that are excluded from the separate 
  1286. * term matching when searching for posts. The list of English stopwords is 
  1287. * the approximate search engines list, and is translatable. 
  1288. * 
  1289. * @since 3.7.0 
  1290. * 
  1291. * @param array $terms Terms to check. 
  1292. * @return array Terms that are not stopwords. 
  1293. */ 
  1294. protected function parse_search_terms( $terms ) { 
  1295. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; 
  1296. $checked = array(); 
  1297.  
  1298. $stopwords = $this->get_search_stopwords(); 
  1299.  
  1300. foreach ( $terms as $term ) { 
  1301. // keep before/after spaces when term is for exact match 
  1302. if ( preg_match( '/^".+"$/', $term ) ) 
  1303. $term = trim( $term, "\"'" ); 
  1304. else 
  1305. $term = trim( $term, "\"' " ); 
  1306.  
  1307. // Avoid single A-Z and single dashes. 
  1308. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) 
  1309. continue; 
  1310.  
  1311. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) 
  1312. continue; 
  1313.  
  1314. $checked[] = $term; 
  1315.  
  1316. return $checked; 
  1317.  
  1318. /** 
  1319. * Retrieve stopwords used when parsing search terms. 
  1320. * 
  1321. * @since 3.7.0 
  1322. * 
  1323. * @return array Stopwords. 
  1324. */ 
  1325. protected function get_search_stopwords() { 
  1326. if ( isset( $this->stopwords ) ) 
  1327. return $this->stopwords; 
  1328.  
  1329. /** translators: This is a comma-separated list of very common words that should be excluded from a search,  
  1330. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual 
  1331. * words into your language. Instead, look for and provide commonly accepted stopwords in your language. 
  1332. */ 
  1333. $words = explode( ', ', _x( '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',  
  1334. 'Comma-separated list of search stopwords in your language' ) ); 
  1335.  
  1336. $stopwords = array(); 
  1337. foreach ( $words as $word ) { 
  1338. $word = trim( $word, "\r\n\t " ); 
  1339. if ( $word ) 
  1340. $stopwords[] = $word; 
  1341.  
  1342. /** 
  1343. * Filters stopwords used when parsing search terms. 
  1344. * 
  1345. * @since 3.7.0 
  1346. * 
  1347. * @param array $stopwords Stopwords. 
  1348. */ 
  1349. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); 
  1350. return $this->stopwords; 
  1351.  
  1352. /** 
  1353. * Generate SQL for the ORDER BY condition based on passed search terms. 
  1354. * 
  1355. * @param array $q Query variables. 
  1356. * @return string ORDER BY clause. 
  1357. */ 
  1358. protected function parse_search_order( &$q ) { 
  1359. global $wpdb; 
  1360.  
  1361. if ( $q['search_terms_count'] > 1 ) { 
  1362. $num_terms = count( $q['search_orderby_title'] ); 
  1363.  
  1364. // If the search terms contain negative queries, don't bother ordering by sentence matches. 
  1365. $like = ''; 
  1366. if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) { 
  1367. $like = '%' . $wpdb->esc_like( $q['s'] ) . '%'; 
  1368.  
  1369. $search_orderby = ''; 
  1370.  
  1371. // sentence match in 'post_title' 
  1372. if ( $like ) { 
  1373. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like ); 
  1374.  
  1375. // sanity limit, sort as sentence when more than 6 terms 
  1376. // (few searches are longer than 6 terms and most titles are not) 
  1377. if ( $num_terms < 7 ) { 
  1378. // all words in title 
  1379. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 '; 
  1380. // any word in title, not needed when $num_terms == 1 
  1381. if ( $num_terms > 1 ) 
  1382. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 '; 
  1383.  
  1384. // Sentence match in 'post_content' and 'post_excerpt'. 
  1385. if ( $like ) { 
  1386. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); 
  1387. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); 
  1388.  
  1389. if ( $search_orderby ) { 
  1390. $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; 
  1391. } else { 
  1392. // single word or sentence search 
  1393. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC'; 
  1394.  
  1395. return $search_orderby; 
  1396.  
  1397. /** 
  1398. * If the passed orderby value is allowed, convert the alias to a 
  1399. * properly-prefixed orderby value. 
  1400. * 
  1401. * @since 4.0.0 
  1402. * @access protected 
  1403. * 
  1404. * @param string $orderby Alias for the field to order by. 
  1405. * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise. 
  1406. */ 
  1407. protected function parse_orderby( $orderby ) { 
  1408. global $wpdb; 
  1409.  
  1410. // Used to filter values. 
  1411. $allowed_keys = array( 
  1412. 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',  
  1413. 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',  
  1414. 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',  
  1415. ); 
  1416.  
  1417. $primary_meta_key = ''; 
  1418. $primary_meta_query = false; 
  1419. $meta_clauses = $this->meta_query->get_clauses(); 
  1420. if ( ! empty( $meta_clauses ) ) { 
  1421. $primary_meta_query = reset( $meta_clauses ); 
  1422.  
  1423. if ( ! empty( $primary_meta_query['key'] ) ) { 
  1424. $primary_meta_key = $primary_meta_query['key']; 
  1425. $allowed_keys[] = $primary_meta_key; 
  1426.  
  1427. $allowed_keys[] = 'meta_value'; 
  1428. $allowed_keys[] = 'meta_value_num'; 
  1429. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); 
  1430.  
  1431. // If RAND() contains a seed value, sanitize and add to allowed keys. 
  1432. $rand_with_seed = false; 
  1433. if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { 
  1434. $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) ); 
  1435. $allowed_keys[] = $orderby; 
  1436. $rand_with_seed = true; 
  1437.  
  1438. if ( ! in_array( $orderby, $allowed_keys, true ) ) { 
  1439. return false; 
  1440.  
  1441. switch ( $orderby ) { 
  1442. case 'post_name': 
  1443. case 'post_author': 
  1444. case 'post_date': 
  1445. case 'post_title': 
  1446. case 'post_modified': 
  1447. case 'post_parent': 
  1448. case 'post_type': 
  1449. case 'ID': 
  1450. case 'menu_order': 
  1451. case 'comment_count': 
  1452. $orderby_clause = "{$wpdb->posts}.{$orderby}"; 
  1453. break; 
  1454. case 'rand': 
  1455. $orderby_clause = 'RAND()'; 
  1456. break; 
  1457. case $primary_meta_key: 
  1458. case 'meta_value': 
  1459. if ( ! empty( $primary_meta_query['type'] ) ) { 
  1460. $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; 
  1461. } else { 
  1462. $orderby_clause = "{$primary_meta_query['alias']}.meta_value"; 
  1463. break; 
  1464. case 'meta_value_num': 
  1465. $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; 
  1466. break; 
  1467. default: 
  1468. if ( array_key_exists( $orderby, $meta_clauses ) ) { 
  1469. // $orderby corresponds to a meta_query clause. 
  1470. $meta_clause = $meta_clauses[ $orderby ]; 
  1471. $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; 
  1472. } elseif ( $rand_with_seed ) { 
  1473. $orderby_clause = $orderby; 
  1474. } else { 
  1475. // Default: order by post field. 
  1476. $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby ); 
  1477.  
  1478. break; 
  1479.  
  1480. return $orderby_clause; 
  1481.  
  1482. /** 
  1483. * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 
  1484. * 
  1485. * @since 4.0.0 
  1486. * @access protected 
  1487. * 
  1488. * @param string $order The 'order' query variable. 
  1489. * @return string The sanitized 'order' query variable. 
  1490. */ 
  1491. protected function parse_order( $order ) { 
  1492. if ( ! is_string( $order ) || empty( $order ) ) { 
  1493. return 'DESC'; 
  1494.  
  1495. if ( 'ASC' === strtoupper( $order ) ) { 
  1496. return 'ASC'; 
  1497. } else { 
  1498. return 'DESC'; 
  1499.  
  1500. /** 
  1501. * Sets the 404 property and saves whether query is feed. 
  1502. * 
  1503. * @since 2.0.0 
  1504. * @access public 
  1505. */ 
  1506. public function set_404() { 
  1507. $is_feed = $this->is_feed; 
  1508.  
  1509. $this->init_query_flags(); 
  1510. $this->is_404 = true; 
  1511.  
  1512. $this->is_feed = $is_feed; 
  1513.  
  1514. /** 
  1515. * Retrieve query variable. 
  1516. * 
  1517. * @since 1.5.0 
  1518. * @since 3.9.0 The `$default` argument was introduced. 
  1519. * 
  1520. * @access public 
  1521. * 
  1522. * @param string $query_var Query variable key. 
  1523. * @param mixed $default Optional. Value to return if the query variable is not set. Default empty. 
  1524. * @return mixed Contents of the query variable. 
  1525. */ 
  1526. public function get( $query_var, $default = '' ) { 
  1527. if ( isset( $this->query_vars[ $query_var ] ) ) { 
  1528. return $this->query_vars[ $query_var ]; 
  1529.  
  1530. return $default; 
  1531.  
  1532. /** 
  1533. * Set query variable. 
  1534. * 
  1535. * @since 1.5.0 
  1536. * @access public 
  1537. * 
  1538. * @param string $query_var Query variable key. 
  1539. * @param mixed $value Query variable value. 
  1540. */ 
  1541. public function set($query_var, $value) { 
  1542. $this->query_vars[$query_var] = $value; 
  1543.  
  1544. /** 
  1545. * Retrieve the posts based on query variables. 
  1546. * 
  1547. * There are a few filters and actions that can be used to modify the post 
  1548. * database query. 
  1549. * 
  1550. * @since 1.5.0 
  1551. * @access public 
  1552. * 
  1553. * @return array List of posts. 
  1554. */ 
  1555. public function get_posts() { 
  1556. global $wpdb; 
  1557.  
  1558. $this->parse_query(); 
  1559.  
  1560. /** 
  1561. * Fires after the query variable object is created, but before the actual query is run. 
  1562. * 
  1563. * Note: If using conditional tags, use the method versions within the passed instance 
  1564. * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions 
  1565. * like is_main_query() test against the global $wp_query instance, not the passed one. 
  1566. * 
  1567. * @since 2.0.0 
  1568. * 
  1569. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  1570. */ 
  1571. do_action_ref_array( 'pre_get_posts', array( &$this ) ); 
  1572.  
  1573. // Shorthand. 
  1574. $q = &$this->query_vars; 
  1575.  
  1576. // Fill again in case pre_get_posts unset some vars. 
  1577. $q = $this->fill_query_vars($q); 
  1578.  
  1579. // Parse meta query 
  1580. $this->meta_query = new WP_Meta_Query(); 
  1581. $this->meta_query->parse_query_vars( $q ); 
  1582.  
  1583. // Set a flag if a pre_get_posts hook changed the query vars. 
  1584. $hash = md5( serialize( $this->query_vars ) ); 
  1585. if ( $hash != $this->query_vars_hash ) { 
  1586. $this->query_vars_changed = true; 
  1587. $this->query_vars_hash = $hash; 
  1588. unset($hash); 
  1589.  
  1590. // First let's clear some variables 
  1591. $distinct = ''; 
  1592. $whichauthor = ''; 
  1593. $whichmimetype = ''; 
  1594. $where = ''; 
  1595. $limits = ''; 
  1596. $join = ''; 
  1597. $search = ''; 
  1598. $groupby = ''; 
  1599. $post_status_join = false; 
  1600. $page = 1; 
  1601.  
  1602. if ( isset( $q['caller_get_posts'] ) ) { 
  1603. _deprecated_argument( 'WP_Query', '3.1.0', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) ); 
  1604. if ( !isset( $q['ignore_sticky_posts'] ) ) 
  1605. $q['ignore_sticky_posts'] = $q['caller_get_posts']; 
  1606.  
  1607. if ( !isset( $q['ignore_sticky_posts'] ) ) 
  1608. $q['ignore_sticky_posts'] = false; 
  1609.  
  1610. if ( !isset($q['suppress_filters']) ) 
  1611. $q['suppress_filters'] = false; 
  1612.  
  1613. if ( !isset($q['cache_results']) ) { 
  1614. if ( wp_using_ext_object_cache() ) 
  1615. $q['cache_results'] = false; 
  1616. else 
  1617. $q['cache_results'] = true; 
  1618.  
  1619. if ( !isset($q['update_post_term_cache']) ) 
  1620. $q['update_post_term_cache'] = true; 
  1621.  
  1622. if ( ! isset( $q['lazy_load_term_meta'] ) ) { 
  1623. $q['lazy_load_term_meta'] = $q['update_post_term_cache']; 
  1624.  
  1625. if ( !isset($q['update_post_meta_cache']) ) 
  1626. $q['update_post_meta_cache'] = true; 
  1627.  
  1628. if ( !isset($q['post_type']) ) { 
  1629. if ( $this->is_search ) 
  1630. $q['post_type'] = 'any'; 
  1631. else 
  1632. $q['post_type'] = ''; 
  1633. $post_type = $q['post_type']; 
  1634. if ( empty( $q['posts_per_page'] ) ) { 
  1635. $q['posts_per_page'] = get_option( 'posts_per_page' ); 
  1636. if ( isset($q['showposts']) && $q['showposts'] ) { 
  1637. $q['showposts'] = (int) $q['showposts']; 
  1638. $q['posts_per_page'] = $q['showposts']; 
  1639. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) 
  1640. $q['posts_per_page'] = $q['posts_per_archive_page']; 
  1641. if ( !isset($q['nopaging']) ) { 
  1642. if ( $q['posts_per_page'] == -1 ) { 
  1643. $q['nopaging'] = true; 
  1644. } else { 
  1645. $q['nopaging'] = false; 
  1646.  
  1647. if ( $this->is_feed ) { 
  1648. // This overrides posts_per_page. 
  1649. if ( ! empty( $q['posts_per_rss'] ) ) { 
  1650. $q['posts_per_page'] = $q['posts_per_rss']; 
  1651. } else { 
  1652. $q['posts_per_page'] = get_option( 'posts_per_rss' ); 
  1653. $q['nopaging'] = false; 
  1654. $q['posts_per_page'] = (int) $q['posts_per_page']; 
  1655. if ( $q['posts_per_page'] < -1 ) 
  1656. $q['posts_per_page'] = abs($q['posts_per_page']); 
  1657. elseif ( $q['posts_per_page'] == 0 ) 
  1658. $q['posts_per_page'] = 1; 
  1659.  
  1660. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 ) 
  1661. $q['comments_per_page'] = get_option('comments_per_page'); 
  1662.  
  1663. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) { 
  1664. $this->is_page = true; 
  1665. $this->is_home = false; 
  1666. $q['page_id'] = get_option('page_on_front'); 
  1667.  
  1668. if ( isset($q['page']) ) { 
  1669. $q['page'] = trim($q['page'], '/'); 
  1670. $q['page'] = absint($q['page']); 
  1671.  
  1672. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. 
  1673. if ( isset($q['no_found_rows']) ) 
  1674. $q['no_found_rows'] = (bool) $q['no_found_rows']; 
  1675. else 
  1676. $q['no_found_rows'] = false; 
  1677.  
  1678. switch ( $q['fields'] ) { 
  1679. case 'ids': 
  1680. $fields = "{$wpdb->posts}.ID"; 
  1681. break; 
  1682. case 'id=>parent': 
  1683. $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; 
  1684. break; 
  1685. default: 
  1686. $fields = "{$wpdb->posts}.*"; 
  1687.  
  1688. if ( '' !== $q['menu_order'] ) { 
  1689. $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; 
  1690. // The "m" parameter is meant for months but accepts datetimes of varying specificity 
  1691. if ( $q['m'] ) { 
  1692. $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4); 
  1693. if ( strlen($q['m']) > 5 ) { 
  1694. $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2); 
  1695. if ( strlen($q['m']) > 7 ) { 
  1696. $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2); 
  1697. if ( strlen($q['m']) > 9 ) { 
  1698. $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2); 
  1699. if ( strlen($q['m']) > 11 ) { 
  1700. $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2); 
  1701. if ( strlen($q['m']) > 13 ) { 
  1702. $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2); 
  1703.  
  1704. // Handle the other individual date parameters 
  1705. $date_parameters = array(); 
  1706.  
  1707. if ( '' !== $q['hour'] ) 
  1708. $date_parameters['hour'] = $q['hour']; 
  1709.  
  1710. if ( '' !== $q['minute'] ) 
  1711. $date_parameters['minute'] = $q['minute']; 
  1712.  
  1713. if ( '' !== $q['second'] ) 
  1714. $date_parameters['second'] = $q['second']; 
  1715.  
  1716. if ( $q['year'] ) 
  1717. $date_parameters['year'] = $q['year']; 
  1718.  
  1719. if ( $q['monthnum'] ) 
  1720. $date_parameters['monthnum'] = $q['monthnum']; 
  1721.  
  1722. if ( $q['w'] ) 
  1723. $date_parameters['week'] = $q['w']; 
  1724.  
  1725. if ( $q['day'] ) 
  1726. $date_parameters['day'] = $q['day']; 
  1727.  
  1728. if ( $date_parameters ) { 
  1729. $date_query = new WP_Date_Query( array( $date_parameters ) ); 
  1730. $where .= $date_query->get_sql(); 
  1731. unset( $date_parameters, $date_query ); 
  1732.  
  1733. // Handle complex date queries 
  1734. if ( ! empty( $q['date_query'] ) ) { 
  1735. $this->date_query = new WP_Date_Query( $q['date_query'] ); 
  1736. $where .= $this->date_query->get_sql(); 
  1737.  
  1738.  
  1739. // If we've got a post_type AND it's not "any" post_type. 
  1740. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) { 
  1741. foreach ( (array)$q['post_type'] as $_post_type ) { 
  1742. $ptype_obj = get_post_type_object($_post_type); 
  1743. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) ) 
  1744. continue; 
  1745.  
  1746. if ( ! $ptype_obj->hierarchical ) { 
  1747. // Non-hierarchical post types can directly use 'name'. 
  1748. $q['name'] = $q[ $ptype_obj->query_var ]; 
  1749. } else { 
  1750. // Hierarchical post types will operate through 'pagename'. 
  1751. $q['pagename'] = $q[ $ptype_obj->query_var ]; 
  1752. $q['name'] = ''; 
  1753.  
  1754. // Only one request for a slug is possible, this is why name & pagename are overwritten above. 
  1755. break; 
  1756. } //end foreach 
  1757. unset($ptype_obj); 
  1758.  
  1759. if ( '' !== $q['title'] ) { 
  1760. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); 
  1761.  
  1762. // Parameters related to 'post_name'. 
  1763. if ( '' != $q['name'] ) { 
  1764. $q['name'] = sanitize_title_for_query( $q['name'] ); 
  1765. $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; 
  1766. } elseif ( '' != $q['pagename'] ) { 
  1767. if ( isset($this->queried_object_id) ) { 
  1768. $reqpage = $this->queried_object_id; 
  1769. } else { 
  1770. if ( 'page' != $q['post_type'] ) { 
  1771. foreach ( (array)$q['post_type'] as $_post_type ) { 
  1772. $ptype_obj = get_post_type_object($_post_type); 
  1773. if ( !$ptype_obj || !$ptype_obj->hierarchical ) 
  1774. continue; 
  1775.  
  1776. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type); 
  1777. if ( $reqpage ) 
  1778. break; 
  1779. unset($ptype_obj); 
  1780. } else { 
  1781. $reqpage = get_page_by_path($q['pagename']); 
  1782. if ( !empty($reqpage) ) 
  1783. $reqpage = $reqpage->ID; 
  1784. else 
  1785. $reqpage = 0; 
  1786.  
  1787. $page_for_posts = get_option('page_for_posts'); 
  1788. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) { 
  1789. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); 
  1790. $q['name'] = $q['pagename']; 
  1791. $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; 
  1792. $reqpage_obj = get_post( $reqpage ); 
  1793. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) { 
  1794. $this->is_attachment = true; 
  1795. $post_type = $q['post_type'] = 'attachment'; 
  1796. $this->is_page = true; 
  1797. $q['attachment_id'] = $reqpage; 
  1798. } elseif ( '' != $q['attachment'] ) { 
  1799. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); 
  1800. $q['name'] = $q['attachment']; 
  1801. $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; 
  1802. } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { 
  1803. $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); 
  1804. $post_name__in = "'" . implode( "', '", $q['post_name__in'] ) . "'"; 
  1805. $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; 
  1806.  
  1807. // If an attachment is requested by number, let it supersede any post number. 
  1808. if ( $q['attachment_id'] ) 
  1809. $q['p'] = absint($q['attachment_id']); 
  1810.  
  1811. // If a post number is specified, load that post 
  1812. if ( $q['p'] ) { 
  1813. $where .= " AND {$wpdb->posts}.ID = " . $q['p']; 
  1814. } elseif ( $q['post__in'] ) { 
  1815. $post__in = implode(', ', array_map( 'absint', $q['post__in'] )); 
  1816. $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; 
  1817. } elseif ( $q['post__not_in'] ) { 
  1818. $post__not_in = implode(', ', array_map( 'absint', $q['post__not_in'] )); 
  1819. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; 
  1820.  
  1821. if ( is_numeric( $q['post_parent'] ) ) { 
  1822. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); 
  1823. } elseif ( $q['post_parent__in'] ) { 
  1824. $post_parent__in = implode( ', ', array_map( 'absint', $q['post_parent__in'] ) ); 
  1825. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; 
  1826. } elseif ( $q['post_parent__not_in'] ) { 
  1827. $post_parent__not_in = implode( ', ', array_map( 'absint', $q['post_parent__not_in'] ) ); 
  1828. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; 
  1829.  
  1830. if ( $q['page_id'] ) { 
  1831. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) { 
  1832. $q['p'] = $q['page_id']; 
  1833. $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; 
  1834.  
  1835. // If a search pattern is specified, load the posts that match. 
  1836. if ( strlen( $q['s'] ) ) { 
  1837. $search = $this->parse_search( $q ); 
  1838.  
  1839. if ( ! $q['suppress_filters'] ) { 
  1840. /** 
  1841. * Filters the search SQL that is used in the WHERE clause of WP_Query. 
  1842. * 
  1843. * @since 3.0.0 
  1844. * 
  1845. * @param string $search Search SQL for WHERE clause. 
  1846. * @param WP_Query $this The current WP_Query object. 
  1847. */ 
  1848. $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); 
  1849.  
  1850. // Taxonomies 
  1851. if ( !$this->is_singular ) { 
  1852. $this->parse_tax_query( $q ); 
  1853.  
  1854. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); 
  1855.  
  1856. $join .= $clauses['join']; 
  1857. $where .= $clauses['where']; 
  1858.  
  1859. if ( $this->is_tax ) { 
  1860. if ( empty($post_type) ) { 
  1861. // Do a fully inclusive search for currently registered post types of queried taxonomies 
  1862. $post_type = array(); 
  1863. $taxonomies = array_keys( $this->tax_query->queried_terms ); 
  1864. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { 
  1865. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); 
  1866. if ( array_intersect( $taxonomies, $object_taxonomies ) ) 
  1867. $post_type[] = $pt; 
  1868. if ( ! $post_type ) 
  1869. $post_type = 'any'; 
  1870. elseif ( count( $post_type ) == 1 ) 
  1871. $post_type = $post_type[0]; 
  1872.  
  1873. $post_status_join = true; 
  1874. } elseif ( in_array('attachment', (array) $post_type) ) { 
  1875. $post_status_join = true; 
  1876.  
  1877. /** 
  1878. * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and 
  1879. * 'category_name' vars are set for backward compatibility. 
  1880. */ 
  1881. if ( ! empty( $this->tax_query->queried_terms ) ) { 
  1882.  
  1883. /** 
  1884. * Set 'taxonomy', 'term', and 'term_id' to the 
  1885. * first taxonomy other than 'post_tag' or 'category'. 
  1886. */ 
  1887. if ( ! isset( $q['taxonomy'] ) ) { 
  1888. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { 
  1889. if ( empty( $queried_items['terms'][0] ) ) { 
  1890. continue; 
  1891.  
  1892. if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) { 
  1893. $q['taxonomy'] = $queried_taxonomy; 
  1894.  
  1895. if ( 'slug' === $queried_items['field'] ) { 
  1896. $q['term'] = $queried_items['terms'][0]; 
  1897. } else { 
  1898. $q['term_id'] = $queried_items['terms'][0]; 
  1899.  
  1900. // Take the first one we find. 
  1901. break; 
  1902.  
  1903. // 'cat', 'category_name', 'tag_id' 
  1904. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { 
  1905. if ( empty( $queried_items['terms'][0] ) ) { 
  1906. continue; 
  1907.  
  1908. if ( 'category' === $queried_taxonomy ) { 
  1909. $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); 
  1910. if ( $the_cat ) { 
  1911. $this->set( 'cat', $the_cat->term_id ); 
  1912. $this->set( 'category_name', $the_cat->slug ); 
  1913. unset( $the_cat ); 
  1914.  
  1915. if ( 'post_tag' === $queried_taxonomy ) { 
  1916. $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); 
  1917. if ( $the_tag ) { 
  1918. $this->set( 'tag_id', $the_tag->term_id ); 
  1919. unset( $the_tag ); 
  1920.  
  1921. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { 
  1922. $groupby = "{$wpdb->posts}.ID"; 
  1923.  
  1924. // Author/user stuff 
  1925.  
  1926. if ( ! empty( $q['author'] ) && $q['author'] != '0' ) { 
  1927. $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); 
  1928. $authors = array_unique( array_map( 'intval', preg_split( '/[, \s]+/', $q['author'] ) ) ); 
  1929. foreach ( $authors as $author ) { 
  1930. $key = $author > 0 ? 'author__in' : 'author__not_in'; 
  1931. $q[$key][] = abs( $author ); 
  1932. $q['author'] = implode( ', ', $authors ); 
  1933.  
  1934. if ( ! empty( $q['author__not_in'] ) ) { 
  1935. $author__not_in = implode( ', ', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); 
  1936. $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; 
  1937. } elseif ( ! empty( $q['author__in'] ) ) { 
  1938. $author__in = implode( ', ', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); 
  1939. $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; 
  1940.  
  1941. // Author stuff for nice URLs 
  1942.  
  1943. if ( '' != $q['author_name'] ) { 
  1944. if ( strpos($q['author_name'], '/') !== false ) { 
  1945. $q['author_name'] = explode('/', $q['author_name']); 
  1946. if ( $q['author_name'][ count($q['author_name'])-1 ] ) { 
  1947. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash 
  1948. } else { 
  1949. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash 
  1950. $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); 
  1951. $q['author'] = get_user_by('slug', $q['author_name']); 
  1952. if ( $q['author'] ) 
  1953. $q['author'] = $q['author']->ID; 
  1954. $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')'; 
  1955.  
  1956. // MIME-Type stuff for attachment browsing 
  1957.  
  1958. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) { 
  1959. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts ); 
  1960. $where .= $search . $whichauthor . $whichmimetype; 
  1961.  
  1962. if ( ! empty( $this->meta_query->queries ) ) { 
  1963. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); 
  1964. $join .= $clauses['join']; 
  1965. $where .= $clauses['where']; 
  1966.  
  1967. $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); 
  1968. if ( ! isset( $q['order'] ) ) { 
  1969. $q['order'] = $rand ? '' : 'DESC'; 
  1970. } else { 
  1971. $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); 
  1972.  
  1973. // Order by. 
  1974. if ( empty( $q['orderby'] ) ) { 
  1975. /** 
  1976. * Boolean false or empty array blanks out ORDER BY,  
  1977. * while leaving the value unset or otherwise empty sets the default. 
  1978. */ 
  1979. if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { 
  1980. $orderby = ''; 
  1981. } else { 
  1982. $orderby = "{$wpdb->posts}.post_date " . $q['order']; 
  1983. } elseif ( 'none' == $q['orderby'] ) { 
  1984. $orderby = ''; 
  1985. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) { 
  1986. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )"; 
  1987. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) { 
  1988. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )"; 
  1989. } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) { 
  1990. $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )"; 
  1991. } else { 
  1992. $orderby_array = array(); 
  1993. if ( is_array( $q['orderby'] ) ) { 
  1994. foreach ( $q['orderby'] as $_orderby => $order ) { 
  1995. $orderby = addslashes_gpc( urldecode( $_orderby ) ); 
  1996. $parsed = $this->parse_orderby( $orderby ); 
  1997.  
  1998. if ( ! $parsed ) { 
  1999. continue; 
  2000.  
  2001. $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); 
  2002. $orderby = implode( ', ', $orderby_array ); 
  2003.  
  2004. } else { 
  2005. $q['orderby'] = urldecode( $q['orderby'] ); 
  2006. $q['orderby'] = addslashes_gpc( $q['orderby'] ); 
  2007.  
  2008. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) { 
  2009. $parsed = $this->parse_orderby( $orderby ); 
  2010. // Only allow certain values for safety. 
  2011. if ( ! $parsed ) { 
  2012. continue; 
  2013.  
  2014. $orderby_array[] = $parsed; 
  2015. $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); 
  2016.  
  2017. if ( empty( $orderby ) ) { 
  2018. $orderby = "{$wpdb->posts}.post_date " . $q['order']; 
  2019. } elseif ( ! empty( $q['order'] ) ) { 
  2020. $orderby .= " {$q['order']}"; 
  2021.  
  2022. // Order search results by relevance only when another "orderby" is not specified in the query. 
  2023. if ( ! empty( $q['s'] ) ) { 
  2024. $search_orderby = ''; 
  2025. if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) 
  2026. $search_orderby = $this->parse_search_order( $q ); 
  2027.  
  2028. if ( ! $q['suppress_filters'] ) { 
  2029. /** 
  2030. * Filters the ORDER BY used when ordering search results. 
  2031. * 
  2032. * @since 3.7.0 
  2033. * 
  2034. * @param string $search_orderby The ORDER BY clause. 
  2035. * @param WP_Query $this The current WP_Query instance. 
  2036. */ 
  2037. $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); 
  2038.  
  2039. if ( $search_orderby ) 
  2040. $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; 
  2041.  
  2042. if ( is_array( $post_type ) && count( $post_type ) > 1 ) { 
  2043. $post_type_cap = 'multiple_post_type'; 
  2044. } else { 
  2045. if ( is_array( $post_type ) ) 
  2046. $post_type = reset( $post_type ); 
  2047. $post_type_object = get_post_type_object( $post_type ); 
  2048. if ( empty( $post_type_object ) ) 
  2049. $post_type_cap = $post_type; 
  2050.  
  2051. if ( isset( $q['post_password'] ) ) { 
  2052. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] ); 
  2053. if ( empty( $q['perm'] ) ) { 
  2054. $q['perm'] = 'readable'; 
  2055. } elseif ( isset( $q['has_password'] ) ) { 
  2056. $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); 
  2057.  
  2058. if ( ! empty( $q['comment_status'] ) ) { 
  2059. $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); 
  2060.  
  2061. if ( ! empty( $q['ping_status'] ) ) { 
  2062. $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); 
  2063.  
  2064. if ( 'any' == $post_type ) { 
  2065. $in_search_post_types = get_post_types( array('exclude_from_search' => false) ); 
  2066. if ( empty( $in_search_post_types ) ) { 
  2067. $where .= ' AND 1=0 '; 
  2068. } else { 
  2069. $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; 
  2070. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) { 
  2071. $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", esc_sql( $post_type ) ) . "')"; 
  2072. } elseif ( ! empty( $post_type ) ) { 
  2073. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); 
  2074. $post_type_object = get_post_type_object ( $post_type ); 
  2075. } elseif ( $this->is_attachment ) { 
  2076. $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; 
  2077. $post_type_object = get_post_type_object ( 'attachment' ); 
  2078. } elseif ( $this->is_page ) { 
  2079. $where .= " AND {$wpdb->posts}.post_type = 'page'"; 
  2080. $post_type_object = get_post_type_object ( 'page' ); 
  2081. } else { 
  2082. $where .= " AND {$wpdb->posts}.post_type = 'post'"; 
  2083. $post_type_object = get_post_type_object ( 'post' ); 
  2084.  
  2085. $edit_cap = 'edit_post'; 
  2086. $read_cap = 'read_post'; 
  2087.  
  2088. if ( ! empty( $post_type_object ) ) { 
  2089. $edit_others_cap = $post_type_object->cap->edit_others_posts; 
  2090. $read_private_cap = $post_type_object->cap->read_private_posts; 
  2091. } else { 
  2092. $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; 
  2093. $read_private_cap = 'read_private_' . $post_type_cap . 's'; 
  2094.  
  2095. $user_id = get_current_user_id(); 
  2096.  
  2097. $q_status = array(); 
  2098. if ( ! empty( $q['post_status'] ) ) { 
  2099. $statuswheres = array(); 
  2100. $q_status = $q['post_status']; 
  2101. if ( ! is_array( $q_status ) ) 
  2102. $q_status = explode(', ', $q_status); 
  2103. $r_status = array(); 
  2104. $p_status = array(); 
  2105. $e_status = array(); 
  2106. if ( in_array( 'any', $q_status ) ) { 
  2107. foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { 
  2108. if ( ! in_array( $status, $q_status ) ) { 
  2109. $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; 
  2110. } else { 
  2111. foreach ( get_post_stati() as $status ) { 
  2112. if ( in_array( $status, $q_status ) ) { 
  2113. if ( 'private' == $status ) { 
  2114. $p_status[] = "{$wpdb->posts}.post_status = '$status'"; 
  2115. } else { 
  2116. $r_status[] = "{$wpdb->posts}.post_status = '$status'"; 
  2117.  
  2118. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) { 
  2119. $r_status = array_merge($r_status, $p_status); 
  2120. unset($p_status); 
  2121.  
  2122. if ( !empty($e_status) ) { 
  2123. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")"; 
  2124. if ( !empty($r_status) ) { 
  2125. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) { 
  2126. $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))"; 
  2127. } else { 
  2128. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; 
  2129. if ( !empty($p_status) ) { 
  2130. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) { 
  2131. $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))"; 
  2132. } else { 
  2133. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; 
  2134. if ( $post_status_join ) { 
  2135. $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; 
  2136. foreach ( $statuswheres as $index => $statuswhere ) { 
  2137. $statuswheres[$index] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . "))"; 
  2138. $where_status = implode( ' OR ', $statuswheres ); 
  2139. if ( ! empty( $where_status ) ) { 
  2140. $where .= " AND ($where_status)"; 
  2141. } elseif ( !$this->is_singular ) { 
  2142. $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; 
  2143.  
  2144. // Add public states. 
  2145. $public_states = get_post_stati( array('public' => true) ); 
  2146. foreach ( (array) $public_states as $state ) { 
  2147. if ( 'publish' == $state ) // Publish is hard-coded above. 
  2148. continue; 
  2149. $where .= " OR {$wpdb->posts}.post_status = '$state'"; 
  2150.  
  2151. if ( $this->is_admin ) { 
  2152. // Add protected states that should show in the admin all list. 
  2153. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) ); 
  2154. foreach ( (array) $admin_all_states as $state ) { 
  2155. $where .= " OR {$wpdb->posts}.post_status = '$state'"; 
  2156.  
  2157. if ( is_user_logged_in() ) { 
  2158. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. 
  2159. $private_states = get_post_stati( array('private' => true) ); 
  2160. foreach ( (array) $private_states as $state ) { 
  2161. $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'"; 
  2162.  
  2163. $where .= ')'; 
  2164.  
  2165. /** 
  2166. * Apply filters on where and join prior to paging so that any 
  2167. * manipulations to them are reflected in the paging by day queries. 
  2168. */ 
  2169. if ( !$q['suppress_filters'] ) { 
  2170. /** 
  2171. * Filters the WHERE clause of the query. 
  2172. * 
  2173. * @since 1.5.0 
  2174. * 
  2175. * @param string $where The WHERE clause of the query. 
  2176. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2177. */ 
  2178. $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); 
  2179.  
  2180. /** 
  2181. * Filters the JOIN clause of the query. 
  2182. * 
  2183. * @since 1.5.0 
  2184. * 
  2185. * @param string $where The JOIN clause of the query. 
  2186. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2187. */ 
  2188. $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); 
  2189.  
  2190. // Paging 
  2191. if ( empty($q['nopaging']) && !$this->is_singular ) { 
  2192. $page = absint($q['paged']); 
  2193. if ( !$page ) 
  2194. $page = 1; 
  2195.  
  2196. // If 'offset' is provided, it takes precedence over 'paged'. 
  2197. if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { 
  2198. $q['offset'] = absint( $q['offset'] ); 
  2199. $pgstrt = $q['offset'] . ', '; 
  2200. } else { 
  2201. $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; 
  2202. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; 
  2203.  
  2204. // Comments feeds 
  2205. if ( $this->is_comment_feed && ! $this->is_singular ) { 
  2206. if ( $this->is_archive || $this->is_search ) { 
  2207. $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join "; 
  2208. $cwhere = "WHERE comment_approved = '1' $where"; 
  2209. $cgroupby = "{$wpdb->comments}.comment_id"; 
  2210. } else { // Other non singular e.g. front 
  2211. $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; 
  2212. $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'"; 
  2213. $cgroupby = ''; 
  2214.  
  2215. if ( !$q['suppress_filters'] ) { 
  2216. /** 
  2217. * Filters the JOIN clause of the comments feed query before sending. 
  2218. * 
  2219. * @since 2.2.0 
  2220. * 
  2221. * @param string $cjoin The JOIN clause of the query. 
  2222. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2223. */ 
  2224. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); 
  2225.  
  2226. /** 
  2227. * Filters the WHERE clause of the comments feed query before sending. 
  2228. * 
  2229. * @since 2.2.0 
  2230. * 
  2231. * @param string $cwhere The WHERE clause of the query. 
  2232. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2233. */ 
  2234. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); 
  2235.  
  2236. /** 
  2237. * Filters the GROUP BY clause of the comments feed query before sending. 
  2238. * 
  2239. * @since 2.2.0 
  2240. * 
  2241. * @param string $cgroupby The GROUP BY clause of the query. 
  2242. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2243. */ 
  2244. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); 
  2245.  
  2246. /** 
  2247. * Filters the ORDER BY clause of the comments feed query before sending. 
  2248. * 
  2249. * @since 2.8.0 
  2250. * 
  2251. * @param string $corderby The ORDER BY clause of the query. 
  2252. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2253. */ 
  2254. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 
  2255.  
  2256. /** 
  2257. * Filters the LIMIT clause of the comments feed query before sending. 
  2258. * 
  2259. * @since 2.8.0 
  2260. * 
  2261. * @param string $climits The JOIN clause of the query. 
  2262. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2263. */ 
  2264. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 
  2265. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 
  2266. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 
  2267.  
  2268. $comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"); 
  2269. // Convert to WP_Comment 
  2270. $this->comments = array_map( 'get_comment', $comments ); 
  2271. $this->comment_count = count($this->comments); 
  2272.  
  2273. $post_ids = array(); 
  2274.  
  2275. foreach ( $this->comments as $comment ) 
  2276. $post_ids[] = (int) $comment->comment_post_ID; 
  2277.  
  2278. $post_ids = join(', ', $post_ids); 
  2279. $join = ''; 
  2280. if ( $post_ids ) { 
  2281. $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; 
  2282. } else { 
  2283. $where = "AND 0"; 
  2284.  
  2285. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 
  2286.  
  2287. /** 
  2288. * Apply post-paging filters on where and join. Only plugins that 
  2289. * manipulate paging queries should use these hooks. 
  2290. */ 
  2291. if ( !$q['suppress_filters'] ) { 
  2292. /** 
  2293. * Filters the WHERE clause of the query. 
  2294. * 
  2295. * Specifically for manipulating paging queries. 
  2296. * 
  2297. * @since 1.5.0 
  2298. * 
  2299. * @param string $where The WHERE clause of the query. 
  2300. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2301. */ 
  2302. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); 
  2303.  
  2304. /** 
  2305. * Filters the GROUP BY clause of the query. 
  2306. * 
  2307. * @since 2.0.0 
  2308. * 
  2309. * @param string $groupby The GROUP BY clause of the query. 
  2310. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2311. */ 
  2312. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); 
  2313.  
  2314. /** 
  2315. * Filters the JOIN clause of the query. 
  2316. * 
  2317. * Specifically for manipulating paging queries. 
  2318. * 
  2319. * @since 1.5.0 
  2320. * 
  2321. * @param string $join The JOIN clause of the query. 
  2322. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2323. */ 
  2324. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); 
  2325.  
  2326. /** 
  2327. * Filters the ORDER BY clause of the query. 
  2328. * 
  2329. * @since 1.5.1 
  2330. * 
  2331. * @param string $orderby The ORDER BY clause of the query. 
  2332. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2333. */ 
  2334. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); 
  2335.  
  2336. /** 
  2337. * Filters the DISTINCT clause of the query. 
  2338. * 
  2339. * @since 2.1.0 
  2340. * 
  2341. * @param string $distinct The DISTINCT clause of the query. 
  2342. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2343. */ 
  2344. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); 
  2345.  
  2346. /** 
  2347. * Filters the LIMIT clause of the query. 
  2348. * 
  2349. * @since 2.1.0 
  2350. * 
  2351. * @param string $limits The LIMIT clause of the query. 
  2352. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2353. */ 
  2354. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); 
  2355.  
  2356. /** 
  2357. * Filters the SELECT clause of the query. 
  2358. * 
  2359. * @since 2.1.0 
  2360. * 
  2361. * @param string $fields The SELECT clause of the query. 
  2362. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2363. */ 
  2364. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); 
  2365.  
  2366. /** 
  2367. * Filters all query clauses at once, for convenience. 
  2368. * 
  2369. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,  
  2370. * fields (SELECT), and LIMITS clauses. 
  2371. * 
  2372. * @since 3.1.0 
  2373. * 
  2374. * @param array $clauses The list of clauses for the query. 
  2375. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2376. */ 
  2377. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 
  2378.  
  2379. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 
  2380. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 
  2381. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 
  2382. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 
  2383. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; 
  2384. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 
  2385. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 
  2386.  
  2387. /** 
  2388. * Fires to announce the query's current selection parameters. 
  2389. * 
  2390. * For use by caching plugins. 
  2391. * 
  2392. * @since 2.3.0 
  2393. * 
  2394. * @param string $selection The assembled selection query. 
  2395. */ 
  2396. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); 
  2397.  
  2398. /** 
  2399. * Filters again for the benefit of caching plugins. 
  2400. * Regular plugins should use the hooks above. 
  2401. */ 
  2402. if ( !$q['suppress_filters'] ) { 
  2403. /** 
  2404. * Filters the WHERE clause of the query. 
  2405. * 
  2406. * For use by caching plugins. 
  2407. * 
  2408. * @since 2.5.0 
  2409. * 
  2410. * @param string $where The WHERE clause of the query. 
  2411. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2412. */ 
  2413. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); 
  2414.  
  2415. /** 
  2416. * Filters the GROUP BY clause of the query. 
  2417. * 
  2418. * For use by caching plugins. 
  2419. * 
  2420. * @since 2.5.0 
  2421. * 
  2422. * @param string $groupby The GROUP BY clause of the query. 
  2423. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2424. */ 
  2425. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); 
  2426.  
  2427. /** 
  2428. * Filters the JOIN clause of the query. 
  2429. * 
  2430. * For use by caching plugins. 
  2431. * 
  2432. * @since 2.5.0 
  2433. * 
  2434. * @param string $join The JOIN clause of the query. 
  2435. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2436. */ 
  2437. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); 
  2438.  
  2439. /** 
  2440. * Filters the ORDER BY clause of the query. 
  2441. * 
  2442. * For use by caching plugins. 
  2443. * 
  2444. * @since 2.5.0 
  2445. * 
  2446. * @param string $orderby The ORDER BY clause of the query. 
  2447. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2448. */ 
  2449. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); 
  2450.  
  2451. /** 
  2452. * Filters the DISTINCT clause of the query. 
  2453. * 
  2454. * For use by caching plugins. 
  2455. * 
  2456. * @since 2.5.0 
  2457. * 
  2458. * @param string $distinct The DISTINCT clause of the query. 
  2459. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2460. */ 
  2461. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); 
  2462.  
  2463. /** 
  2464. * Filters the SELECT clause of the query. 
  2465. * 
  2466. * For use by caching plugins. 
  2467. * 
  2468. * @since 2.5.0 
  2469. * 
  2470. * @param string $fields The SELECT clause of the query. 
  2471. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2472. */ 
  2473. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); 
  2474.  
  2475. /** 
  2476. * Filters the LIMIT clause of the query. 
  2477. * 
  2478. * For use by caching plugins. 
  2479. * 
  2480. * @since 2.5.0 
  2481. * 
  2482. * @param string $limits The LIMIT clause of the query. 
  2483. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2484. */ 
  2485. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); 
  2486.  
  2487. /** 
  2488. * Filters all query clauses at once, for convenience. 
  2489. * 
  2490. * For use by caching plugins. 
  2491. * 
  2492. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,  
  2493. * fields (SELECT), and LIMITS clauses. 
  2494. * 
  2495. * @since 3.1.0 
  2496. * 
  2497. * @param array $pieces The pieces of the query. 
  2498. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2499. */ 
  2500. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 
  2501.  
  2502. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 
  2503. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 
  2504. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 
  2505. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 
  2506. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; 
  2507. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 
  2508. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 
  2509.  
  2510. if ( ! empty($groupby) ) 
  2511. $groupby = 'GROUP BY ' . $groupby; 
  2512. if ( !empty( $orderby ) ) 
  2513. $orderby = 'ORDER BY ' . $orderby; 
  2514.  
  2515. $found_rows = ''; 
  2516. if ( !$q['no_found_rows'] && !empty($limits) ) 
  2517. $found_rows = 'SQL_CALC_FOUND_ROWS'; 
  2518.  
  2519. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; 
  2520.  
  2521. if ( !$q['suppress_filters'] ) { 
  2522. /** 
  2523. * Filters the completed SQL query before sending. 
  2524. * 
  2525. * @since 2.0.0 
  2526. * 
  2527. * @param string $request The complete SQL query. 
  2528. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2529. */ 
  2530. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); 
  2531.  
  2532. /** 
  2533. * Filters the posts array before the query takes place. 
  2534. * 
  2535. * Return a non-null value to bypass WordPress's default post queries. 
  2536. * 
  2537. * Filtering functions that require pagination information are encouraged to set 
  2538. * the `found_posts` and `max_num_pages` properties of the WP_Query object,  
  2539. * passed to the filter by reference. If WP_Query does not perform a database 
  2540. * query, it will not have enough information to generate these values itself. 
  2541. * 
  2542. * @since 4.6.0 
  2543. * 
  2544. * @param array|null $posts Return an array of post data to short-circuit WP's query,  
  2545. * or null to allow WP to run its normal queries. 
  2546. * @param WP_Query $this The WP_Query instance, passed by reference. 
  2547. */ 
  2548. $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); 
  2549.  
  2550. if ( 'ids' == $q['fields'] ) { 
  2551. if ( null === $this->posts ) { 
  2552. $this->posts = $wpdb->get_col( $this->request ); 
  2553.  
  2554. $this->posts = array_map( 'intval', $this->posts ); 
  2555. $this->post_count = count( $this->posts ); 
  2556. $this->set_found_posts( $q, $limits ); 
  2557.  
  2558. return $this->posts; 
  2559.  
  2560. if ( 'id=>parent' == $q['fields'] ) { 
  2561. if ( null === $this->posts ) { 
  2562. $this->posts = $wpdb->get_results( $this->request ); 
  2563.  
  2564. $this->post_count = count( $this->posts ); 
  2565. $this->set_found_posts( $q, $limits ); 
  2566.  
  2567. $r = array(); 
  2568. foreach ( $this->posts as $key => $post ) { 
  2569. $this->posts[ $key ]->ID = (int) $post->ID; 
  2570. $this->posts[ $key ]->post_parent = (int) $post->post_parent; 
  2571.  
  2572. $r[ (int) $post->ID ] = (int) $post->post_parent; 
  2573.  
  2574. return $r; 
  2575.  
  2576. if ( null === $this->posts ) { 
  2577. $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 ); 
  2578.  
  2579. /** 
  2580. * Filters whether to split the query. 
  2581. * 
  2582. * Splitting the query will cause it to fetch just the IDs of the found posts 
  2583. * (and then individually fetch each post by ID), rather than fetching every 
  2584. * complete row at once. One massive result vs. many small results. 
  2585. * 
  2586. * @since 3.4.0 
  2587. * 
  2588. * @param bool $split_the_query Whether or not to split the query. 
  2589. * @param WP_Query $this The WP_Query instance. 
  2590. */ 
  2591. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); 
  2592.  
  2593. if ( $split_the_query ) { 
  2594. // First get the IDs and then fill in the objects 
  2595.  
  2596. $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; 
  2597.  
  2598. /** 
  2599. * Filters the Post IDs SQL request before sending. 
  2600. * 
  2601. * @since 3.4.0 
  2602. * 
  2603. * @param string $request The post ID request. 
  2604. * @param WP_Query $this The WP_Query instance. 
  2605. */ 
  2606. $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); 
  2607.  
  2608. $ids = $wpdb->get_col( $this->request ); 
  2609.  
  2610. if ( $ids ) { 
  2611. $this->posts = $ids; 
  2612. $this->set_found_posts( $q, $limits ); 
  2613. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); 
  2614. } else { 
  2615. $this->posts = array(); 
  2616. } else { 
  2617. $this->posts = $wpdb->get_results( $this->request ); 
  2618. $this->set_found_posts( $q, $limits ); 
  2619.  
  2620. // Convert to WP_Post objects. 
  2621. if ( $this->posts ) { 
  2622. $this->posts = array_map( 'get_post', $this->posts ); 
  2623.  
  2624. if ( ! $q['suppress_filters'] ) { 
  2625. /** 
  2626. * Filters the raw post results array, prior to status checks. 
  2627. * 
  2628. * @since 2.3.0 
  2629. * 
  2630. * @param array $posts The post results array. 
  2631. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2632. */ 
  2633. $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); 
  2634.  
  2635. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) { 
  2636. /** This filter is documented in wp-includes/query.php */ 
  2637. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); 
  2638.  
  2639. /** This filter is documented in wp-includes/query.php */ 
  2640. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); 
  2641.  
  2642. /** This filter is documented in wp-includes/query.php */ 
  2643. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); 
  2644. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; 
  2645.  
  2646. /** This filter is documented in wp-includes/query.php */ 
  2647. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); 
  2648. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; 
  2649.  
  2650. /** This filter is documented in wp-includes/query.php */ 
  2651. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); 
  2652.  
  2653. $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; 
  2654. $comments = $wpdb->get_results($comments_request); 
  2655. // Convert to WP_Comment 
  2656. $this->comments = array_map( 'get_comment', $comments ); 
  2657. $this->comment_count = count($this->comments); 
  2658.  
  2659. // Check post status to determine if post should be displayed. 
  2660. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { 
  2661. $status = get_post_status($this->posts[0]); 
  2662. if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { 
  2663. $this->is_page = false; 
  2664. $this->is_single = true; 
  2665. $this->is_attachment = true; 
  2666. $post_status_obj = get_post_status_object($status); 
  2667.  
  2668. // If the post_status was specifically requested, let it pass through. 
  2669. if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) { 
  2670.  
  2671. if ( ! is_user_logged_in() ) { 
  2672. // User must be logged in to view unpublished posts. 
  2673. $this->posts = array(); 
  2674. } else { 
  2675. if ( $post_status_obj->protected ) { 
  2676. // User must have edit permissions on the draft to preview. 
  2677. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) { 
  2678. $this->posts = array(); 
  2679. } else { 
  2680. $this->is_preview = true; 
  2681. if ( 'future' != $status ) 
  2682. $this->posts[0]->post_date = current_time('mysql'); 
  2683. } elseif ( $post_status_obj->private ) { 
  2684. if ( ! current_user_can($read_cap, $this->posts[0]->ID) ) 
  2685. $this->posts = array(); 
  2686. } else { 
  2687. $this->posts = array(); 
  2688.  
  2689. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { 
  2690. /** 
  2691. * Filters the single post for preview mode. 
  2692. * 
  2693. * @since 2.7.0 
  2694. * 
  2695. * @param WP_Post $post_preview The Post object. 
  2696. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2697. */ 
  2698. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); 
  2699.  
  2700. // Put sticky posts at the top of the posts array 
  2701. $sticky_posts = get_option('sticky_posts'); 
  2702. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { 
  2703. $num_posts = count($this->posts); 
  2704. $sticky_offset = 0; 
  2705. // Loop over posts and relocate stickies to the front. 
  2706. for ( $i = 0; $i < $num_posts; $i++ ) { 
  2707. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { 
  2708. $sticky_post = $this->posts[$i]; 
  2709. // Remove sticky from current position 
  2710. array_splice($this->posts, $i, 1); 
  2711. // Move to front, after other stickies 
  2712. array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); 
  2713. // Increment the sticky offset. The next sticky will be placed at this offset. 
  2714. $sticky_offset++; 
  2715. // Remove post from sticky posts array 
  2716. $offset = array_search($sticky_post->ID, $sticky_posts); 
  2717. unset( $sticky_posts[$offset] ); 
  2718.  
  2719. // If any posts have been excluded specifically, Ignore those that are sticky. 
  2720. if ( !empty($sticky_posts) && !empty($q['post__not_in']) ) 
  2721. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); 
  2722.  
  2723. // Fetch sticky posts that weren't in the query results 
  2724. if ( !empty($sticky_posts) ) { 
  2725. $stickies = get_posts( array( 
  2726. 'post__in' => $sticky_posts,  
  2727. 'post_type' => $post_type,  
  2728. 'post_status' => 'publish',  
  2729. 'nopaging' => true 
  2730. ) ); 
  2731.  
  2732. foreach ( $stickies as $sticky_post ) { 
  2733. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); 
  2734. $sticky_offset++; 
  2735.  
  2736. // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. 
  2737. if ( ! empty( $this->comments ) ) { 
  2738. wp_queue_comments_for_comment_meta_lazyload( $this->comments ); 
  2739.  
  2740. if ( ! $q['suppress_filters'] ) { 
  2741. /** 
  2742. * Filters the array of retrieved posts after they've been fetched and 
  2743. * internally processed. 
  2744. * 
  2745. * @since 1.5.0 
  2746. * 
  2747. * @param array $posts The array of retrieved posts. 
  2748. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2749. */ 
  2750. $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); 
  2751.  
  2752. // Ensure that any posts added/modified via one of the filters above are 
  2753. // of the type WP_Post and are filtered. 
  2754. if ( $this->posts ) { 
  2755. $this->post_count = count( $this->posts ); 
  2756.  
  2757. $this->posts = array_map( 'get_post', $this->posts ); 
  2758.  
  2759. if ( $q['cache_results'] ) 
  2760. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']); 
  2761.  
  2762. $this->post = reset( $this->posts ); 
  2763. } else { 
  2764. $this->post_count = 0; 
  2765. $this->posts = array(); 
  2766.  
  2767. if ( $q['lazy_load_term_meta'] ) { 
  2768. wp_queue_posts_for_term_meta_lazyload( $this->posts ); 
  2769.  
  2770. return $this->posts; 
  2771.  
  2772. /** 
  2773. * Set up the amount of found posts and the number of pages (if limit clause was used) 
  2774. * for the current query. 
  2775. * 
  2776. * @since 3.5.0 
  2777. * @access private 
  2778. * 
  2779. * @param array $q Query variables. 
  2780. * @param string $limits LIMIT clauses of the query. 
  2781. */ 
  2782. private function set_found_posts( $q, $limits ) { 
  2783. global $wpdb; 
  2784. // Bail if posts is an empty array. Continue if posts is an empty string,  
  2785. // null, or false to accommodate caching plugins that fill posts later. 
  2786. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) 
  2787. return; 
  2788.  
  2789. if ( ! empty( $limits ) ) { 
  2790. /** 
  2791. * Filters the query to run for retrieving the found posts. 
  2792. * 
  2793. * @since 2.1.0 
  2794. * 
  2795. * @param string $found_posts The query to run to find the found posts. 
  2796. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2797. */ 
  2798. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) ); 
  2799. } else { 
  2800. $this->found_posts = count( $this->posts ); 
  2801.  
  2802. /** 
  2803. * Filters the number of found posts for the query. 
  2804. * 
  2805. * @since 2.1.0 
  2806. * 
  2807. * @param int $found_posts The number of posts found. 
  2808. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2809. */ 
  2810. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 
  2811.  
  2812. if ( ! empty( $limits ) ) 
  2813. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 
  2814.  
  2815. /** 
  2816. * Set up the next post and iterate current post index. 
  2817. * 
  2818. * @since 1.5.0 
  2819. * @access public 
  2820. * 
  2821. * @return WP_Post Next post. 
  2822. */ 
  2823. public function next_post() { 
  2824.  
  2825. $this->current_post++; 
  2826.  
  2827. $this->post = $this->posts[$this->current_post]; 
  2828. return $this->post; 
  2829.  
  2830. /** 
  2831. * Sets up the current post. 
  2832. * 
  2833. * Retrieves the next post, sets up the post, sets the 'in the loop' 
  2834. * property to true. 
  2835. * 
  2836. * @since 1.5.0 
  2837. * @access public 
  2838. * 
  2839. * @global WP_Post $post 
  2840. */ 
  2841. public function the_post() { 
  2842. global $post; 
  2843. $this->in_the_loop = true; 
  2844.  
  2845. if ( $this->current_post == -1 ) // loop has just started 
  2846. /** 
  2847. * Fires once the loop is started. 
  2848. * 
  2849. * @since 2.0.0 
  2850. * 
  2851. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2852. */ 
  2853. do_action_ref_array( 'loop_start', array( &$this ) ); 
  2854.  
  2855. $post = $this->next_post(); 
  2856. $this->setup_postdata( $post ); 
  2857.  
  2858. /** 
  2859. * Determines whether there are more posts available in the loop. 
  2860. * 
  2861. * Calls the {@see 'loop_end'} action when the loop is complete. 
  2862. * 
  2863. * @since 1.5.0 
  2864. * @access public 
  2865. * 
  2866. * @return bool True if posts are available, false if end of loop. 
  2867. */ 
  2868. public function have_posts() { 
  2869. if ( $this->current_post + 1 < $this->post_count ) { 
  2870. return true; 
  2871. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) { 
  2872. /** 
  2873. * Fires once the loop has ended. 
  2874. * 
  2875. * @since 2.0.0 
  2876. * 
  2877. * @param WP_Query &$this The WP_Query instance (passed by reference). 
  2878. */ 
  2879. do_action_ref_array( 'loop_end', array( &$this ) ); 
  2880. // Do some cleaning up after the loop 
  2881. $this->rewind_posts(); 
  2882.  
  2883. $this->in_the_loop = false; 
  2884. return false; 
  2885.  
  2886. /** 
  2887. * Rewind the posts and reset post index. 
  2888. * 
  2889. * @since 1.5.0 
  2890. * @access public 
  2891. */ 
  2892. public function rewind_posts() { 
  2893. $this->current_post = -1; 
  2894. if ( $this->post_count > 0 ) { 
  2895. $this->post = $this->posts[0]; 
  2896.  
  2897. /** 
  2898. * Iterate current comment index and return WP_Comment object. 
  2899. * 
  2900. * @since 2.2.0 
  2901. * @access public 
  2902. * 
  2903. * @return WP_Comment Comment object. 
  2904. */ 
  2905. public function next_comment() { 
  2906. $this->current_comment++; 
  2907.  
  2908. $this->comment = $this->comments[$this->current_comment]; 
  2909. return $this->comment; 
  2910.  
  2911. /** 
  2912. * Sets up the current comment. 
  2913. * 
  2914. * @since 2.2.0 
  2915. * @access public 
  2916. * @global WP_Comment $comment Current comment. 
  2917. */ 
  2918. public function the_comment() { 
  2919. global $comment; 
  2920.  
  2921. $comment = $this->next_comment(); 
  2922.  
  2923. if ( $this->current_comment == 0 ) { 
  2924. /** 
  2925. * Fires once the comment loop is started. 
  2926. * 
  2927. * @since 2.2.0 
  2928. */ 
  2929. do_action( 'comment_loop_start' ); 
  2930.  
  2931. /** 
  2932. * Whether there are more comments available. 
  2933. * 
  2934. * Automatically rewinds comments when finished. 
  2935. * 
  2936. * @since 2.2.0 
  2937. * @access public 
  2938. * 
  2939. * @return bool True, if more comments. False, if no more posts. 
  2940. */ 
  2941. public function have_comments() { 
  2942. if ( $this->current_comment + 1 < $this->comment_count ) { 
  2943. return true; 
  2944. } elseif ( $this->current_comment + 1 == $this->comment_count ) { 
  2945. $this->rewind_comments(); 
  2946.  
  2947. return false; 
  2948.  
  2949. /** 
  2950. * Rewind the comments, resets the comment index and comment to first. 
  2951. * 
  2952. * @since 2.2.0 
  2953. * @access public 
  2954. */ 
  2955. public function rewind_comments() { 
  2956. $this->current_comment = -1; 
  2957. if ( $this->comment_count > 0 ) { 
  2958. $this->comment = $this->comments[0]; 
  2959.  
  2960. /** 
  2961. * Sets up the WordPress query by parsing query string. 
  2962. * 
  2963. * @since 1.5.0 
  2964. * @access public 
  2965. * 
  2966. * @param string $query URL query string. 
  2967. * @return array List of posts. 
  2968. */ 
  2969. public function query( $query ) { 
  2970. $this->init(); 
  2971. $this->query = $this->query_vars = wp_parse_args( $query ); 
  2972. return $this->get_posts(); 
  2973.  
  2974. /** 
  2975. * Retrieve queried object. 
  2976. * 
  2977. * If queried object is not set, then the queried object will be set from 
  2978. * the category, tag, taxonomy, posts page, single post, page, or author 
  2979. * query variable. After it is set up, it will be returned. 
  2980. * 
  2981. * @since 1.5.0 
  2982. * @access public 
  2983. * 
  2984. * @return object 
  2985. */ 
  2986. public function get_queried_object() { 
  2987. if ( isset($this->queried_object) ) 
  2988. return $this->queried_object; 
  2989.  
  2990. $this->queried_object = null; 
  2991. $this->queried_object_id = null; 
  2992.  
  2993. if ( $this->is_category || $this->is_tag || $this->is_tax ) { 
  2994. if ( $this->is_category ) { 
  2995. if ( $this->get( 'cat' ) ) { 
  2996. $term = get_term( $this->get( 'cat' ), 'category' ); 
  2997. } elseif ( $this->get( 'category_name' ) ) { 
  2998. $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); 
  2999. } elseif ( $this->is_tag ) { 
  3000. if ( $this->get( 'tag_id' ) ) { 
  3001. $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); 
  3002. } elseif ( $this->get( 'tag' ) ) { 
  3003. $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); 
  3004. } else { 
  3005. // For other tax queries, grab the first term from the first clause. 
  3006. if ( ! empty( $this->tax_query->queried_terms ) ) { 
  3007. $queried_taxonomies = array_keys( $this->tax_query->queried_terms ); 
  3008. $matched_taxonomy = reset( $queried_taxonomies ); 
  3009. $query = $this->tax_query->queried_terms[ $matched_taxonomy ]; 
  3010.  
  3011. if ( ! empty( $query['terms'] ) ) { 
  3012. if ( 'term_id' == $query['field'] ) { 
  3013. $term = get_term( reset( $query['terms'] ), $matched_taxonomy ); 
  3014. } else { 
  3015. $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy ); 
  3016.  
  3017. if ( ! empty( $term ) && ! is_wp_error( $term ) ) { 
  3018. $this->queried_object = $term; 
  3019. $this->queried_object_id = (int) $term->term_id; 
  3020.  
  3021. if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) 
  3022. _make_cat_compat( $this->queried_object ); 
  3023. } elseif ( $this->is_post_type_archive ) { 
  3024. $post_type = $this->get( 'post_type' ); 
  3025. if ( is_array( $post_type ) ) 
  3026. $post_type = reset( $post_type ); 
  3027. $this->queried_object = get_post_type_object( $post_type ); 
  3028. } elseif ( $this->is_posts_page ) { 
  3029. $page_for_posts = get_option('page_for_posts'); 
  3030. $this->queried_object = get_post( $page_for_posts ); 
  3031. $this->queried_object_id = (int) $this->queried_object->ID; 
  3032. } elseif ( $this->is_singular && ! empty( $this->post ) ) { 
  3033. $this->queried_object = $this->post; 
  3034. $this->queried_object_id = (int) $this->post->ID; 
  3035. } elseif ( $this->is_author ) { 
  3036. $this->queried_object_id = (int) $this->get('author'); 
  3037. $this->queried_object = get_userdata( $this->queried_object_id ); 
  3038.  
  3039. return $this->queried_object; 
  3040.  
  3041. /** 
  3042. * Retrieve ID of the current queried object. 
  3043. * 
  3044. * @since 1.5.0 
  3045. * @access public 
  3046. * 
  3047. * @return int 
  3048. */ 
  3049. public function get_queried_object_id() { 
  3050. $this->get_queried_object(); 
  3051.  
  3052. if ( isset($this->queried_object_id) ) { 
  3053. return $this->queried_object_id; 
  3054.  
  3055. return 0; 
  3056.  
  3057. /** 
  3058. * Constructor. 
  3059. * 
  3060. * Sets up the WordPress query, if parameter is not empty. 
  3061. * 
  3062. * @since 1.5.0 
  3063. * @access public 
  3064. * 
  3065. * @param string|array $query URL query string or array of vars. 
  3066. */ 
  3067. public function __construct( $query = '' ) { 
  3068. if ( ! empty( $query ) ) { 
  3069. $this->query( $query ); 
  3070.  
  3071. /** 
  3072. * Make private properties readable for backward compatibility. 
  3073. * 
  3074. * @since 4.0.0 
  3075. * @access public 
  3076. * 
  3077. * @param string $name Property to get. 
  3078. * @return mixed Property. 
  3079. */ 
  3080. public function __get( $name ) { 
  3081. if ( in_array( $name, $this->compat_fields ) ) { 
  3082. return $this->$name; 
  3083.  
  3084. /** 
  3085. * Make private properties checkable for backward compatibility. 
  3086. * 
  3087. * @since 4.0.0 
  3088. * @access public 
  3089. * 
  3090. * @param string $name Property to check if set. 
  3091. * @return bool Whether the property is set. 
  3092. */ 
  3093. public function __isset( $name ) { 
  3094. if ( in_array( $name, $this->compat_fields ) ) { 
  3095. return isset( $this->$name ); 
  3096.  
  3097. /** 
  3098. * Make private/protected methods readable for backward compatibility. 
  3099. * 
  3100. * @since 4.0.0 
  3101. * @access public 
  3102. * 
  3103. * @param callable $name Method to call. 
  3104. * @param array $arguments Arguments to pass when calling. 
  3105. * @return mixed|false Return value of the callback, false otherwise. 
  3106. */ 
  3107. public function __call( $name, $arguments ) { 
  3108. if ( in_array( $name, $this->compat_methods ) ) { 
  3109. return call_user_func_array( array( $this, $name ), $arguments ); 
  3110. return false; 
  3111.  
  3112. /** 
  3113. * Is the query for an existing archive page? 
  3114. * 
  3115. * Month, Year, Category, Author, Post Type archive... 
  3116. * 
  3117. * @since 3.1.0 
  3118. * 
  3119. * @return bool 
  3120. */ 
  3121. public function is_archive() { 
  3122. return (bool) $this->is_archive; 
  3123.  
  3124. /** 
  3125. * Is the query for an existing post type archive page? 
  3126. * 
  3127. * @since 3.1.0 
  3128. * 
  3129. * @param mixed $post_types Optional. Post type or array of posts types to check against. 
  3130. * @return bool 
  3131. */ 
  3132. public function is_post_type_archive( $post_types = '' ) { 
  3133. if ( empty( $post_types ) || ! $this->is_post_type_archive ) 
  3134. return (bool) $this->is_post_type_archive; 
  3135.  
  3136. $post_type = $this->get( 'post_type' ); 
  3137. if ( is_array( $post_type ) ) 
  3138. $post_type = reset( $post_type ); 
  3139. $post_type_object = get_post_type_object( $post_type ); 
  3140.  
  3141. return in_array( $post_type_object->name, (array) $post_types ); 
  3142.  
  3143. /** 
  3144. * Is the query for an existing attachment page? 
  3145. * 
  3146. * @since 3.1.0 
  3147. * 
  3148. * @param mixed $attachment Attachment ID, title, slug, or array of such. 
  3149. * @return bool 
  3150. */ 
  3151. public function is_attachment( $attachment = '' ) { 
  3152. if ( ! $this->is_attachment ) { 
  3153. return false; 
  3154.  
  3155. if ( empty( $attachment ) ) { 
  3156. return true; 
  3157.  
  3158. $attachment = array_map( 'strval', (array) $attachment ); 
  3159.  
  3160. $post_obj = $this->get_queried_object(); 
  3161.  
  3162. if ( in_array( (string) $post_obj->ID, $attachment ) ) { 
  3163. return true; 
  3164. } elseif ( in_array( $post_obj->post_title, $attachment ) ) { 
  3165. return true; 
  3166. } elseif ( in_array( $post_obj->post_name, $attachment ) ) { 
  3167. return true; 
  3168. return false; 
  3169.  
  3170. /** 
  3171. * Is the query for an existing author archive page? 
  3172. * 
  3173. * If the $author parameter is specified, this function will additionally 
  3174. * check if the query is for one of the authors specified. 
  3175. * 
  3176. * @since 3.1.0 
  3177. * 
  3178. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames 
  3179. * @return bool 
  3180. */ 
  3181. public function is_author( $author = '' ) { 
  3182. if ( !$this->is_author ) 
  3183. return false; 
  3184.  
  3185. if ( empty($author) ) 
  3186. return true; 
  3187.  
  3188. $author_obj = $this->get_queried_object(); 
  3189.  
  3190. $author = array_map( 'strval', (array) $author ); 
  3191.  
  3192. if ( in_array( (string) $author_obj->ID, $author ) ) 
  3193. return true; 
  3194. elseif ( in_array( $author_obj->nickname, $author ) ) 
  3195. return true; 
  3196. elseif ( in_array( $author_obj->user_nicename, $author ) ) 
  3197. return true; 
  3198.  
  3199. return false; 
  3200.  
  3201. /** 
  3202. * Is the query for an existing category archive page? 
  3203. * 
  3204. * If the $category parameter is specified, this function will additionally 
  3205. * check if the query is for one of the categories specified. 
  3206. * 
  3207. * @since 3.1.0 
  3208. * 
  3209. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs. 
  3210. * @return bool 
  3211. */ 
  3212. public function is_category( $category = '' ) { 
  3213. if ( !$this->is_category ) 
  3214. return false; 
  3215.  
  3216. if ( empty($category) ) 
  3217. return true; 
  3218.  
  3219. $cat_obj = $this->get_queried_object(); 
  3220.  
  3221. $category = array_map( 'strval', (array) $category ); 
  3222.  
  3223. if ( in_array( (string) $cat_obj->term_id, $category ) ) 
  3224. return true; 
  3225. elseif ( in_array( $cat_obj->name, $category ) ) 
  3226. return true; 
  3227. elseif ( in_array( $cat_obj->slug, $category ) ) 
  3228. return true; 
  3229.  
  3230. return false; 
  3231.  
  3232. /** 
  3233. * Is the query for an existing tag archive page? 
  3234. * 
  3235. * If the $tag parameter is specified, this function will additionally 
  3236. * check if the query is for one of the tags specified. 
  3237. * 
  3238. * @since 3.1.0 
  3239. * 
  3240. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. 
  3241. * @return bool 
  3242. */ 
  3243. public function is_tag( $tag = '' ) { 
  3244. if ( ! $this->is_tag ) 
  3245. return false; 
  3246.  
  3247. if ( empty( $tag ) ) 
  3248. return true; 
  3249.  
  3250. $tag_obj = $this->get_queried_object(); 
  3251.  
  3252. $tag = array_map( 'strval', (array) $tag ); 
  3253.  
  3254. if ( in_array( (string) $tag_obj->term_id, $tag ) ) 
  3255. return true; 
  3256. elseif ( in_array( $tag_obj->name, $tag ) ) 
  3257. return true; 
  3258. elseif ( in_array( $tag_obj->slug, $tag ) ) 
  3259. return true; 
  3260.  
  3261. return false; 
  3262.  
  3263. /** 
  3264. * Is the query for an existing custom taxonomy archive page? 
  3265. * 
  3266. * If the $taxonomy parameter is specified, this function will additionally 
  3267. * check if the query is for that specific $taxonomy. 
  3268. * 
  3269. * If the $term parameter is specified in addition to the $taxonomy parameter,  
  3270. * this function will additionally check if the query is for one of the terms 
  3271. * specified. 
  3272. * 
  3273. * @since 3.1.0 
  3274. * 
  3275. * @global array $wp_taxonomies 
  3276. * 
  3277. * @param mixed $taxonomy Optional. Taxonomy slug or slugs. 
  3278. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs. 
  3279. * @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives). 
  3280. */ 
  3281. public function is_tax( $taxonomy = '', $term = '' ) { 
  3282. global $wp_taxonomies; 
  3283.  
  3284. if ( !$this->is_tax ) 
  3285. return false; 
  3286.  
  3287. if ( empty( $taxonomy ) ) 
  3288. return true; 
  3289.  
  3290. $queried_object = $this->get_queried_object(); 
  3291. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy ); 
  3292. $term_array = (array) $term; 
  3293.  
  3294. // Check that the taxonomy matches. 
  3295. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) ) 
  3296. return false; 
  3297.  
  3298. // Only a Taxonomy provided. 
  3299. if ( empty( $term ) ) 
  3300. return true; 
  3301.  
  3302. return isset( $queried_object->term_id ) && 
  3303. count( array_intersect( 
  3304. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),  
  3305. $term_array 
  3306. ) ); 
  3307.  
  3308. /** 
  3309. * Whether the current URL is within the comments popup window. 
  3310. * 
  3311. * @since 3.1.0 
  3312. * @deprecated 4.5.0 
  3313. * 
  3314. * @return bool 
  3315. */ 
  3316. public function is_comments_popup() { 
  3317. _deprecated_function( __FUNCTION__, '4.5.0' ); 
  3318.  
  3319. return false; 
  3320.  
  3321. /** 
  3322. * Is the query for an existing date archive? 
  3323. * 
  3324. * @since 3.1.0 
  3325. * 
  3326. * @return bool 
  3327. */ 
  3328. public function is_date() { 
  3329. return (bool) $this->is_date; 
  3330.  
  3331. /** 
  3332. * Is the query for an existing day archive? 
  3333. * 
  3334. * @since 3.1.0 
  3335. * 
  3336. * @return bool 
  3337. */ 
  3338. public function is_day() { 
  3339. return (bool) $this->is_day; 
  3340.  
  3341. /** 
  3342. * Is the query for a feed? 
  3343. * 
  3344. * @since 3.1.0 
  3345. * 
  3346. * @param string|array $feeds Optional feed types to check. 
  3347. * @return bool 
  3348. */ 
  3349. public function is_feed( $feeds = '' ) { 
  3350. if ( empty( $feeds ) || ! $this->is_feed ) 
  3351. return (bool) $this->is_feed; 
  3352. $qv = $this->get( 'feed' ); 
  3353. if ( 'feed' == $qv ) 
  3354. $qv = get_default_feed(); 
  3355. return in_array( $qv, (array) $feeds ); 
  3356.  
  3357. /** 
  3358. * Is the query for a comments feed? 
  3359. * 
  3360. * @since 3.1.0 
  3361. * 
  3362. * @return bool 
  3363. */ 
  3364. public function is_comment_feed() { 
  3365. return (bool) $this->is_comment_feed; 
  3366.  
  3367. /** 
  3368. * Is the query for the front page of the site? 
  3369. * 
  3370. * This is for what is displayed at your site's main URL. 
  3371. * 
  3372. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. 
  3373. * 
  3374. * If you set a static page for the front page of your site, this function will return 
  3375. * true when viewing that page. 
  3376. * 
  3377. * Otherwise the same as @see WP_Query::is_home() 
  3378. * 
  3379. * @since 3.1.0 
  3380. * 
  3381. * @return bool True, if front of site. 
  3382. */ 
  3383. public function is_front_page() { 
  3384. // most likely case 
  3385. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() ) 
  3386. return true; 
  3387. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) ) 
  3388. return true; 
  3389. else 
  3390. return false; 
  3391.  
  3392. /** 
  3393. * Is the query for the blog homepage? 
  3394. * 
  3395. * This is the page which shows the time based blog content of your site. 
  3396. * 
  3397. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'. 
  3398. * 
  3399. * If you set a static page for the front page of your site, this function will return 
  3400. * true only on the page you set as the "Posts page". 
  3401. * 
  3402. * @see WP_Query::is_front_page() 
  3403. * 
  3404. * @since 3.1.0 
  3405. * 
  3406. * @return bool True if blog view homepage. 
  3407. */ 
  3408. public function is_home() { 
  3409. return (bool) $this->is_home; 
  3410.  
  3411. /** 
  3412. * Is the query for an existing month archive? 
  3413. * 
  3414. * @since 3.1.0 
  3415. * 
  3416. * @return bool 
  3417. */ 
  3418. public function is_month() { 
  3419. return (bool) $this->is_month; 
  3420.  
  3421. /** 
  3422. * Is the query for an existing single page? 
  3423. * 
  3424. * If the $page parameter is specified, this function will additionally 
  3425. * check if the query is for one of the pages specified. 
  3426. * 
  3427. * @see WP_Query::is_single() 
  3428. * @see WP_Query::is_singular() 
  3429. * 
  3430. * @since 3.1.0 
  3431. * 
  3432. * @param int|string|array $page Optional. Page ID, title, slug, path, or array of such. Default empty. 
  3433. * @return bool Whether the query is for an existing single page. 
  3434. */ 
  3435. public function is_page( $page = '' ) { 
  3436. if ( !$this->is_page ) 
  3437. return false; 
  3438.  
  3439. if ( empty( $page ) ) 
  3440. return true; 
  3441.  
  3442. $page_obj = $this->get_queried_object(); 
  3443.  
  3444. $page = array_map( 'strval', (array) $page ); 
  3445.  
  3446. if ( in_array( (string) $page_obj->ID, $page ) ) { 
  3447. return true; 
  3448. } elseif ( in_array( $page_obj->post_title, $page ) ) { 
  3449. return true; 
  3450. } elseif ( in_array( $page_obj->post_name, $page ) ) { 
  3451. return true; 
  3452. } else { 
  3453. foreach ( $page as $pagepath ) { 
  3454. if ( ! strpos( $pagepath, '/' ) ) { 
  3455. continue; 
  3456. $pagepath_obj = get_page_by_path( $pagepath ); 
  3457.  
  3458. if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { 
  3459. return true; 
  3460.  
  3461. return false; 
  3462.  
  3463. /** 
  3464. * Is the query for paged result and not for the first page? 
  3465. * 
  3466. * @since 3.1.0 
  3467. * 
  3468. * @return bool 
  3469. */ 
  3470. public function is_paged() { 
  3471. return (bool) $this->is_paged; 
  3472.  
  3473. /** 
  3474. * Is the query for a post or page preview? 
  3475. * 
  3476. * @since 3.1.0 
  3477. * 
  3478. * @return bool 
  3479. */ 
  3480. public function is_preview() { 
  3481. return (bool) $this->is_preview; 
  3482.  
  3483. /** 
  3484. * Is the query for the robots file? 
  3485. * 
  3486. * @since 3.1.0 
  3487. * 
  3488. * @return bool 
  3489. */ 
  3490. public function is_robots() { 
  3491. return (bool) $this->is_robots; 
  3492.  
  3493. /** 
  3494. * Is the query for a search? 
  3495. * 
  3496. * @since 3.1.0 
  3497. * 
  3498. * @return bool 
  3499. */ 
  3500. public function is_search() { 
  3501. return (bool) $this->is_search; 
  3502.  
  3503. /** 
  3504. * Is the query for an existing single post? 
  3505. * 
  3506. * Works for any post type excluding pages. 
  3507. * 
  3508. * If the $post parameter is specified, this function will additionally 
  3509. * check if the query is for one of the Posts specified. 
  3510. * 
  3511. * @see WP_Query::is_page() 
  3512. * @see WP_Query::is_singular() 
  3513. * 
  3514. * @since 3.1.0 
  3515. * 
  3516. * @param int|string|array $post Optional. Post ID, title, slug, path, or array of such. Default empty. 
  3517. * @return bool Whether the query is for an existing single post. 
  3518. */ 
  3519. public function is_single( $post = '' ) { 
  3520. if ( !$this->is_single ) 
  3521. return false; 
  3522.  
  3523. if ( empty($post) ) 
  3524. return true; 
  3525.  
  3526. $post_obj = $this->get_queried_object(); 
  3527.  
  3528. $post = array_map( 'strval', (array) $post ); 
  3529.  
  3530. if ( in_array( (string) $post_obj->ID, $post ) ) { 
  3531. return true; 
  3532. } elseif ( in_array( $post_obj->post_title, $post ) ) { 
  3533. return true; 
  3534. } elseif ( in_array( $post_obj->post_name, $post ) ) { 
  3535. return true; 
  3536. } else { 
  3537. foreach ( $post as $postpath ) { 
  3538. if ( ! strpos( $postpath, '/' ) ) { 
  3539. continue; 
  3540. $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); 
  3541.  
  3542. if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { 
  3543. return true; 
  3544. return false; 
  3545.  
  3546. /** 
  3547. * Is the query for an existing single post of any post type (post, attachment, page, ... )? 
  3548. * 
  3549. * If the $post_types parameter is specified, this function will additionally 
  3550. * check if the query is for one of the Posts Types specified. 
  3551. * 
  3552. * @see WP_Query::is_page() 
  3553. * @see WP_Query::is_single() 
  3554. * 
  3555. * @since 3.1.0 
  3556. * 
  3557. * @param string|array $post_types Optional. Post type or array of post types. Default empty. 
  3558. * @return bool Whether the query is for an existing single post of any of the given post types. 
  3559. */ 
  3560. public function is_singular( $post_types = '' ) { 
  3561. if ( empty( $post_types ) || !$this->is_singular ) 
  3562. return (bool) $this->is_singular; 
  3563.  
  3564. $post_obj = $this->get_queried_object(); 
  3565.  
  3566. return in_array( $post_obj->post_type, (array) $post_types ); 
  3567.  
  3568. /** 
  3569. * Is the query for a specific time? 
  3570. * 
  3571. * @since 3.1.0 
  3572. * 
  3573. * @return bool 
  3574. */ 
  3575. public function is_time() { 
  3576. return (bool) $this->is_time; 
  3577.  
  3578. /** 
  3579. * Is the query for a trackback endpoint call? 
  3580. * 
  3581. * @since 3.1.0 
  3582. * 
  3583. * @return bool 
  3584. */ 
  3585. public function is_trackback() { 
  3586. return (bool) $this->is_trackback; 
  3587.  
  3588. /** 
  3589. * Is the query for an existing year archive? 
  3590. * 
  3591. * @since 3.1.0 
  3592. * 
  3593. * @return bool 
  3594. */ 
  3595. public function is_year() { 
  3596. return (bool) $this->is_year; 
  3597.  
  3598. /** 
  3599. * Is the query a 404 (returns no results)? 
  3600. * 
  3601. * @since 3.1.0 
  3602. * 
  3603. * @return bool 
  3604. */ 
  3605. public function is_404() { 
  3606. return (bool) $this->is_404; 
  3607.  
  3608. /** 
  3609. * Is the query for an embedded post? 
  3610. * 
  3611. * @since 4.4.0 
  3612. * 
  3613. * @return bool 
  3614. */ 
  3615. public function is_embed() { 
  3616. return (bool) $this->is_embed; 
  3617.  
  3618. /** 
  3619. * Is the query the main query? 
  3620. * 
  3621. * @since 3.3.0 
  3622. * 
  3623. * @global WP_Query $wp_query Global WP_Query instance. 
  3624. * 
  3625. * @return bool 
  3626. */ 
  3627. public function is_main_query() { 
  3628. global $wp_the_query; 
  3629. return $wp_the_query === $this; 
  3630.  
  3631. /** 
  3632. * Set up global post data. 
  3633. * 
  3634. * @since 4.1.0 
  3635. * @since 4.4.0 Added the ability to pass a post ID to `$post`. 
  3636. * 
  3637. * @global int $id 
  3638. * @global WP_User $authordata 
  3639. * @global string|int|bool $currentday 
  3640. * @global string|int|bool $currentmonth 
  3641. * @global int $page 
  3642. * @global array $pages 
  3643. * @global int $multipage 
  3644. * @global int $more 
  3645. * @global int $numpages 
  3646. * 
  3647. * @param WP_Post|object|int $post WP_Post instance or Post ID/object. 
  3648. * @return true True when finished. 
  3649. */ 
  3650. public function setup_postdata( $post ) { 
  3651. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages; 
  3652.  
  3653. if ( ! ( $post instanceof WP_Post ) ) { 
  3654. $post = get_post( $post ); 
  3655.  
  3656. if ( ! $post ) { 
  3657. return; 
  3658.  
  3659. $id = (int) $post->ID; 
  3660.  
  3661. $authordata = get_userdata($post->post_author); 
  3662.  
  3663. $currentday = mysql2date('d.m.y', $post->post_date, false); 
  3664. $currentmonth = mysql2date('m', $post->post_date, false); 
  3665. $numpages = 1; 
  3666. $multipage = 0; 
  3667. $page = $this->get( 'page' ); 
  3668. if ( ! $page ) 
  3669. $page = 1; 
  3670.  
  3671. /** 
  3672. * Force full post content when viewing the permalink for the $post,  
  3673. * or when on an RSS feed. Otherwise respect the 'more' tag. 
  3674. */ 
  3675. if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) { 
  3676. $more = 1; 
  3677. } elseif ( $this->is_feed() ) { 
  3678. $more = 1; 
  3679. } else { 
  3680. $more = 0; 
  3681.  
  3682. $content = $post->post_content; 
  3683. if ( false !== strpos( $content, '<!--nextpage-->' ) ) { 
  3684. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); 
  3685. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); 
  3686. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 
  3687.  
  3688. // Ignore nextpage at the beginning of the content. 
  3689. if ( 0 === strpos( $content, '<!--nextpage-->' ) ) 
  3690. $content = substr( $content, 15 ); 
  3691.  
  3692. $pages = explode('<!--nextpage-->', $content); 
  3693. } else { 
  3694. $pages = array( $post->post_content ); 
  3695.  
  3696. /** 
  3697. * Filters the "pages" derived from splitting the post content. 
  3698. * 
  3699. * "Pages" are determined by splitting the post content based on the presence 
  3700. * of `<!-- nextpage -->` tags. 
  3701. * 
  3702. * @since 4.4.0 
  3703. * 
  3704. * @param array $pages Array of "pages" derived from the post content. 
  3705. * of `<!-- nextpage -->` tags.. 
  3706. * @param WP_Post $post Current post object. 
  3707. */ 
  3708. $pages = apply_filters( 'content_pagination', $pages, $post ); 
  3709.  
  3710. $numpages = count( $pages ); 
  3711.  
  3712. if ( $numpages > 1 ) { 
  3713. if ( $page > 1 ) { 
  3714. $more = 1; 
  3715. $multipage = 1; 
  3716. } else { 
  3717. $multipage = 0; 
  3718.  
  3719. /** 
  3720. * Fires once the post data has been setup. 
  3721. * 
  3722. * @since 2.8.0 
  3723. * @since 4.1.0 Introduced `$this` parameter. 
  3724. * 
  3725. * @param WP_Post &$post The Post object (passed by reference). 
  3726. * @param WP_Query &$this The current Query object (passed by reference). 
  3727. */ 
  3728. do_action_ref_array( 'the_post', array( &$post, &$this ) ); 
  3729.  
  3730. return true; 
  3731. /** 
  3732. * After looping through a nested query, this function 
  3733. * restores the $post global to the current post in this query. 
  3734. * 
  3735. * @since 3.7.0 
  3736. * 
  3737. * @global WP_Post $post 
  3738. */ 
  3739. public function reset_postdata() { 
  3740. if ( ! empty( $this->post ) ) { 
  3741. $GLOBALS['post'] = $this->post; 
  3742. $this->setup_postdata( $this->post ); 
  3743.  
  3744. /** 
  3745. * Lazyload term meta for posts in the loop. 
  3746. * 
  3747. * @since 4.4.0 
  3748. * @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload(). 
  3749. * 
  3750. * @param mixed $check 
  3751. * @param int $term_id 
  3752. * @return mixed 
  3753. */ 
  3754. public function lazyload_term_meta( $check, $term_id ) { 
  3755. _deprecated_function( __METHOD__, '4.5.0' ); 
  3756. return $check; 
  3757.  
  3758. /** 
  3759. * Lazyload comment meta for comments in the loop. 
  3760. * 
  3761. * @since 4.4.0 
  3762. * @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload(). 
  3763. * 
  3764. * @param mixed $check 
  3765. * @param int $comment_id 
  3766. * @return mixed 
  3767. */ 
  3768. public function lazyload_comment_meta( $check, $comment_id ) { 
  3769. _deprecated_function( __METHOD__, '4.5.0' ); 
  3770. return $check; 
.