WP_Query

The WordPress Query class.

Defined (1)

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

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