PageRenderTime 225ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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