PageRenderTime 67ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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