WP_Query

The WordPress Query class.

Defined (1)

The class is defined in the following location(s).

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