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

/wordpress-3.4.2/wp-includes/query.php

https://bitbucket.org/emallove/bedforddavis.com
PHP | 4394 lines | 2163 code | 510 blank | 1721 comment | 585 complexity | 7d0da7490b2fb08cfe334a400a02db77 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.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. var $query;
  743. /**
  744. * Query vars, after parsing
  745. *
  746. * @since 1.5.0
  747. * @access public
  748. * @var array
  749. */
  750. var $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. var $tax_query;
  759. /**
  760. * Metadata query container
  761. *
  762. * @since 3.2.0
  763. * @access public
  764. * @var object WP_Meta_Query
  765. */
  766. var $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. var $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. var $queried_object;
  785. /**
  786. * The ID of the queried object.
  787. *
  788. * @since 1.5.0
  789. * @access public
  790. * @var int
  791. */
  792. var $queried_object_id;
  793. /**
  794. * Get post database query.
  795. *
  796. * @since 2.0.1
  797. * @access public
  798. * @var string
  799. */
  800. var $request;
  801. /**
  802. * List of posts.
  803. *
  804. * @since 1.5.0
  805. * @access public
  806. * @var array
  807. */
  808. var $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. var $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. var $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. var $in_the_loop = false;
  833. /**
  834. * The current post.
  835. *
  836. * @since 1.5.0
  837. * @access public
  838. * @var WP_Post
  839. */
  840. var $post;
  841. /**
  842. * The list of comments for current post.
  843. *
  844. * @since 2.2.0
  845. * @access public
  846. * @var array
  847. */
  848. var $comments;
  849. /**
  850. * The amount of comments for the posts.
  851. *
  852. * @since 2.2.0
  853. * @access public
  854. * @var int
  855. */
  856. var $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. var $current_comment = -1;
  865. /**
  866. * Current comment ID.
  867. *
  868. * @since 2.2.0
  869. * @access public
  870. * @var int
  871. */
  872. var $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. var $found_posts = 0;
  883. /**
  884. * The amount of pages.
  885. *
  886. * @since 2.1.0
  887. * @access public
  888. * @var int
  889. */
  890. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $is_day = false;
  963. /**
  964. * Set if query contains time.
  965. *
  966. * @since 1.5.0
  967. * @access public
  968. * @var bool
  969. */
  970. var $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. var $is_author = false;
  979. /**
  980. * Set if query contains category.
  981. *
  982. * @since 1.5.0
  983. * @access public
  984. * @var bool
  985. */
  986. var $is_category = false;
  987. /**
  988. * Set if query contains tag.
  989. *
  990. * @since 2.3.0
  991. * @access public
  992. * @var bool
  993. */
  994. var $is_tag = false;
  995. /**
  996. * Set if query contains taxonomy.
  997. *
  998. * @since 2.5.0
  999. * @access public
  1000. * @var bool
  1001. */
  1002. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. var $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. 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. 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. 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. 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. 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 ( !empty($qv['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. 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. }
  1612. if ( ! empty($q['category__not_in']) ) {
  1613. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  1614. $tax_query[] = array(
  1615. 'taxonomy' => 'category',
  1616. 'terms' => $q['category__not_in'],
  1617. 'operator' => 'NOT IN',
  1618. 'include_children' => false
  1619. );
  1620. }
  1621. if ( ! empty($q['category__and']) ) {
  1622. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
  1623. $tax_query[] = array(
  1624. 'taxonomy' => 'category',
  1625. 'terms' => $q['category__and'],
  1626. 'field' => 'term_id',
  1627. 'operator' => 'AND',
  1628. 'include_children' => false
  1629. );
  1630. }
  1631. // Tag stuff
  1632. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1633. if ( strpos($q['tag'], ',') !== false ) {
  1634. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1635. foreach ( (array) $tags as $tag ) {
  1636. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1637. $q['tag_slug__in'][] = $tag;
  1638. }
  1639. } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
  1640. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1641. foreach ( (array) $tags as $tag ) {
  1642. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1643. $q['tag_slug__and'][] = $tag;
  1644. }
  1645. } else {
  1646. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1647. $q['tag_slug__in'][] = $q['tag'];
  1648. }
  1649. }
  1650. if ( !empty($q['tag_id']) ) {
  1651. $q['tag_id'] = absint( $q['tag_id'] );
  1652. $tax_query[] = array(
  1653. 'taxonomy' => 'post_tag',
  1654. 'terms' => $q['tag_id']
  1655. );
  1656. }
  1657. if ( !empty($q['tag__in']) ) {
  1658. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1659. $tax_query[] = array(
  1660. 'taxonomy' => 'post_tag',
  1661. 'terms' => $q['tag__in']
  1662. );
  1663. }
  1664. if ( !empty($q['tag__not_in']) ) {
  1665. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1666. $tax_query[] = array(
  1667. 'taxonomy' => 'post_tag',
  1668. 'terms' => $q['tag__not_in'],
  1669. 'operator' => 'NOT IN'
  1670. );
  1671. }
  1672. if ( !empty($q['tag__and']) ) {
  1673. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1674. $tax_query[] = array(
  1675. 'taxonomy' => 'post_tag',
  1676. 'terms' => $q['tag__and'],
  1677. 'operator' => 'AND'
  1678. );
  1679. }
  1680. if ( !empty($q['tag_slug__in']) ) {
  1681. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1682. $tax_query[] = array(
  1683. 'taxonomy' => 'post_tag',
  1684. 'terms' => $q['tag_slug__in'],
  1685. 'field' => 'slug'
  1686. );
  1687. }
  1688. if ( !empty($q['tag_slug__and']) ) {
  1689. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1690. $tax_query[] = array(
  1691. 'taxonomy' => 'post_tag',
  1692. 'terms' => $q['tag_slug__and'],
  1693. 'field' => 'slug',
  1694. 'operator' => 'AND'
  1695. );
  1696. }
  1697. $this->tax_query = new WP_Tax_Query( $tax_query );
  1698. /**
  1699. * Fires after taxonomy-related query vars have been parsed.
  1700. *
  1701. * @since 3.7.0
  1702. *
  1703. * @param WP_Query $this The WP_Query instance.
  1704. */
  1705. do_action( 'parse_tax_query', $this );
  1706. }
  1707. /**
  1708. * Generate SQL for the WHERE clause based on passed search terms.
  1709. *
  1710. * @since 3.7.0
  1711. *
  1712. * @global wpdb $wpdb
  1713. * @param array $q Query variables.
  1714. * @return string WHERE clause.
  1715. */
  1716. protected function parse_search( &$q ) {
  1717. global $wpdb;
  1718. $search = '';
  1719. // added slashes screw with quote grouping when done early, so done later
  1720. $q['s'] = stripslashes( $q['s'] );
  1721. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1722. $q['s'] = urldecode( $q['s'] );
  1723. // there are no line breaks in <input /> fields
  1724. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
  1725. $q['search_terms_count'] = 1;
  1726. if ( ! empty( $q['sentence'] ) ) {
  1727. $q['search_terms'] = array( $q['s'] );
  1728. } else {
  1729. if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
  1730. $q['search_terms_count'] = count( $matches[0] );
  1731. $q['search_terms'] = $this->parse_search_terms( $matches[0] );
  1732. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
  1733. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
  1734. $q['search_terms'] = array( $q['s'] );
  1735. } else {
  1736. $q['search_terms'] = array( $q['s'] );
  1737. }
  1738. }
  1739. $n = ! empty( $q['exact'] ) ? '' : '%';
  1740. $searchand = '';
  1741. $q['search_orderby_title'] = array();
  1742. foreach ( $q['search_terms'] as $term ) {
  1743. $term = like_escape( esc_sql( $term ) );
  1744. if ( $n )
  1745. $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
  1746. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1747. $searchand = ' AND ';
  1748. }
  1749. if ( ! empty( $search ) ) {
  1750. $search = " AND ({$search}) ";
  1751. if ( ! is_user_logged_in() )
  1752. $search .= " AND ($wpdb->posts.post_password = '') ";
  1753. }
  1754. return $search;
  1755. }
  1756. /**
  1757. * Check if the terms are suitable for searching.
  1758. *
  1759. * Uses an array of stopwords (terms) that are excluded from the separate
  1760. * term matching when searching for posts. The list of English stopwords is
  1761. * the approximate search engines list, and is translatable.
  1762. *
  1763. * @since 3.7.0
  1764. *
  1765. * @param array Terms to check.
  1766. * @return array Terms that are not stopwords.
  1767. */
  1768. protected function parse_search_terms( $terms ) {
  1769. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
  1770. $checked = array();
  1771. $stopwords = $this->get_search_stopwords();
  1772. foreach ( $terms as $term ) {
  1773. // keep before/after spaces when term is for exact match
  1774. if ( preg_match( '/^".+"$/', $term ) )
  1775. $term = trim( $term, "\"'" );
  1776. else
  1777. $term = trim( $term, "\"' " );
  1778. // Avoid single A-Z.
  1779. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
  1780. continue;
  1781. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
  1782. continue;
  1783. $checked[] = $term;
  1784. }
  1785. return $checked;
  1786. }
  1787. /**
  1788. * Retrieve stopwords used when parsing search terms.
  1789. *
  1790. * @since 3.7.0
  1791. *
  1792. * @return array Stopwords.
  1793. */
  1794. protected function get_search_stopwords() {
  1795. if ( isset( $this->stopwords ) )
  1796. return $this->stopwords;
  1797. /* translators: This is a comma-separated list of very common words that should be excluded from a search,
  1798. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
  1799. * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
  1800. */
  1801. $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',
  1802. 'Comma-separated list of search stopwords in your language' ) );
  1803. $stopwords = array();
  1804. foreach( $words as $word ) {
  1805. $word = trim( $word, "\r\n\t " );
  1806. if ( $word )
  1807. $stopwords[] = $word;
  1808. }
  1809. /**
  1810. * Filter stopwords used when parsing search terms.
  1811. *
  1812. * @since 3.7.0
  1813. *
  1814. * @param array $stopwords Stopwords.
  1815. */
  1816. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
  1817. return $this->stopwords;
  1818. }
  1819. /**
  1820. * Generate SQL for the ORDER BY condition based on passed search terms.
  1821. *
  1822. * @global wpdb $wpdb
  1823. * @param array $q Query variables.
  1824. * @return string ORDER BY clause.
  1825. */
  1826. protected function parse_search_order( &$q ) {
  1827. global $wpdb;
  1828. $search_orderby = '';
  1829. if ( $q['search_terms_count'] > 1 ) {
  1830. $num_terms = count( $q['search_orderby_title'] );
  1831. $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
  1832. $search_orderby = '(CASE ';
  1833. // sentence match in 'post_title'
  1834. $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
  1835. // sanity limit, sort as sentence when more than 6 terms
  1836. // (few searches are longer than 6 terms and most titles are not)
  1837. if ( $num_terms < 7 ) {
  1838. // all words in title
  1839. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
  1840. // any word in title, not needed when $num_terms == 1
  1841. if ( $num_terms > 1 )
  1842. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
  1843. }
  1844. // sentence match in 'post_content'
  1845. $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
  1846. $search_orderby .= 'ELSE 5 END)';
  1847. } else {
  1848. // single word or sentence search
  1849. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
  1850. }
  1851. return $search_orderby;
  1852. }
  1853. /**
  1854. * Sets the 404 property and saves whether query is feed.
  1855. *
  1856. * @since 2.0.0
  1857. * @access public
  1858. */
  1859. function set_404() {
  1860. $is_feed = $this->is_feed;
  1861. $this->init_query_flags();
  1862. $this->is_404 = true;
  1863. $this->is_feed = $is_feed;
  1864. }
  1865. /**
  1866. * Retrieve query variable.
  1867. *
  1868. * @since 1.5.0
  1869. * @access public
  1870. *
  1871. * @param string $query_var Query variable key.
  1872. * @param mixed $default Value to return if the query variable is not set. Default ''.
  1873. * @return mixed
  1874. */
  1875. function get( $query_var, $default = '' ) {
  1876. if ( isset( $this->query_vars[ $query_var ] ) ) {
  1877. return $this->query_vars[ $query_var ];
  1878. }
  1879. return $default;
  1880. }
  1881. /**
  1882. * Set query variable.
  1883. *
  1884. * @since 1.5.0
  1885. * @access public
  1886. *
  1887. * @param string $query_var Query variable key.
  1888. * @param mixed $value Query variable value.
  1889. */
  1890. function set($query_var, $value) {
  1891. $this->query_vars[$query_var] = $value;
  1892. }
  1893. /**
  1894. * Retrieve the posts based on query variables.
  1895. *
  1896. * There are a few filters and actions that can be used to modify the post
  1897. * database query.
  1898. *
  1899. * @since 1.5.0
  1900. * @access public
  1901. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1902. *
  1903. * @return array List of posts.
  1904. */
  1905. function get_posts() {
  1906. global $wpdb;
  1907. $this->parse_query();
  1908. /**
  1909. * Fires after the query variable object is created, but before the actual query is run.
  1910. *
  1911. * Note: If using conditional tags, use the method versions within the passed instance
  1912. * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
  1913. * like is_main_query() test against the global $wp_query instance, not the passed one.
  1914. *
  1915. * @since 2.0.0
  1916. *
  1917. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1918. */
  1919. do_action_ref_array( 'pre_get_posts', array( &$this ) );
  1920. // Shorthand.
  1921. $q = &$this->query_vars;
  1922. // Fill again in case pre_get_posts unset some vars.
  1923. $q = $this->fill_query_vars($q);
  1924. // Parse meta query
  1925. $this->meta_query = new WP_Meta_Query();
  1926. $this->meta_query->parse_query_vars( $q );
  1927. // Set a flag if a pre_get_posts hook changed the query vars.
  1928. $hash = md5( serialize( $this->query_vars ) );
  1929. if ( $hash != $this->query_vars_hash ) {
  1930. $this->query_vars_changed = true;
  1931. $this->query_vars_hash = $hash;
  1932. }
  1933. unset($hash);
  1934. // First let's clear some variables
  1935. $distinct = '';
  1936. $whichauthor = '';
  1937. $whichmimetype = '';
  1938. $where = '';
  1939. $limits = '';
  1940. $join = '';
  1941. $search = '';
  1942. $groupby = '';
  1943. $fields = '';
  1944. $post_status_join = false;
  1945. $page = 1;
  1946. if ( isset( $q['caller_get_posts'] ) ) {
  1947. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1948. if ( !isset( $q['ignore_sticky_posts'] ) )
  1949. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1950. }
  1951. if ( !isset( $q['ignore_sticky_posts'] ) )
  1952. $q['ignore_sticky_posts'] = false;
  1953. if ( !isset($q['suppress_filters']) )
  1954. $q['suppress_filters'] = false;
  1955. if ( !isset($q['cache_results']) ) {
  1956. if ( wp_using_ext_object_cache() )
  1957. $q['cache_results'] = false;
  1958. else
  1959. $q['cache_results'] = true;
  1960. }
  1961. if ( !isset($q['update_post_term_cache']) )
  1962. $q['update_post_term_cache'] = true;
  1963. if ( !isset($q['update_post_meta_cache']) )
  1964. $q['update_post_meta_cache'] = true;
  1965. if ( !isset($q['post_type']) ) {
  1966. if ( $this->is_search )
  1967. $q['post_type'] = 'any';
  1968. else
  1969. $q['post_type'] = '';
  1970. }
  1971. $post_type = $q['post_type'];
  1972. if ( empty( $q['posts_per_page'] ) ) {
  1973. $q['posts_per_page'] = get_option( 'posts_per_page' );
  1974. }
  1975. if ( isset($q['showposts']) && $q['showposts'] ) {
  1976. $q['showposts'] = (int) $q['showposts'];
  1977. $q['posts_per_page'] = $q['showposts'];
  1978. }
  1979. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1980. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1981. if ( !isset($q['nopaging']) ) {
  1982. if ( $q['posts_per_page'] == -1 ) {
  1983. $q['nopaging'] = true;
  1984. } else {
  1985. $q['nopaging'] = false;
  1986. }
  1987. }
  1988. if ( $this->is_feed ) {
  1989. // This overrides posts_per_page.
  1990. if ( ! empty( $q['posts_per_rss'] ) ) {
  1991. $q['posts_per_page'] = $q['posts_per_rss'];
  1992. } else {
  1993. $q['posts_per_page'] = get_option( 'posts_per_rss' );
  1994. }
  1995. $q['nopaging'] = false;
  1996. }
  1997. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1998. if ( $q['posts_per_page'] < -1 )
  1999. $q['posts_per_page'] = abs($q['posts_per_page']);
  2000. else if ( $q['posts_per_page'] == 0 )
  2001. $q['posts_per_page'] = 1;
  2002. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  2003. $q['comments_per_page'] = get_option('comments_per_page');
  2004. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  2005. $this->is_page = true;
  2006. $this->is_home = false;
  2007. $q['page_id'] = get_option('page_on_front');
  2008. }
  2009. if ( isset($q['page']) ) {
  2010. $q['page'] = trim($q['page'], '/');
  2011. $q['page'] = absint($q['page']);
  2012. }
  2013. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  2014. if ( isset($q['no_found_rows']) )
  2015. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  2016. else
  2017. $q['no_found_rows'] = false;
  2018. switch ( $q['fields'] ) {
  2019. case 'ids':
  2020. $fields = "$wpdb->posts.ID";
  2021. break;
  2022. case 'id=>parent':
  2023. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  2024. break;
  2025. default:
  2026. $fields = "$wpdb->posts.*";
  2027. }
  2028. if ( '' !== $q['menu_order'] )
  2029. $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
  2030. // The "m" parameter is meant for months but accepts datetimes of varying specificity
  2031. if ( $q['m'] ) {
  2032. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  2033. if ( strlen($q['m']) > 5 )
  2034. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  2035. if ( strlen($q['m']) > 7 )
  2036. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  2037. if ( strlen($q['m']) > 9 )
  2038. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  2039. if ( strlen($q['m']) > 11 )
  2040. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  2041. if ( strlen($q['m']) > 13 )
  2042. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  2043. }
  2044. // Handle the other individual date parameters
  2045. $date_parameters = array();
  2046. if ( '' !== $q['hour'] )
  2047. $date_parameters['hour'] = $q['hour'];
  2048. if ( '' !== $q['minute'] )
  2049. $date_parameters['minute'] = $q['minute'];
  2050. if ( '' !== $q['second'] )
  2051. $date_parameters['second'] = $q['second'];
  2052. if ( $q['year'] )
  2053. $date_parameters['year'] = $q['year'];
  2054. if ( $q['monthnum'] )
  2055. $date_parameters['monthnum'] = $q['monthnum'];
  2056. if ( $q['w'] )
  2057. $date_parameters['week'] = $q['w'];
  2058. if ( $q['day'] )
  2059. $date_parameters['day'] = $q['day'];
  2060. if ( $date_parameters ) {
  2061. $date_query = new WP_Date_Query( array( $date_parameters ) );
  2062. $where .= $date_query->get_sql();
  2063. }
  2064. unset( $date_parameters, $date_query );
  2065. // Handle complex date queries
  2066. if ( ! empty( $q['date_query'] ) ) {
  2067. $this->date_query = new WP_Date_Query( $q['date_query'] );
  2068. $where .= $this->date_query->get_sql();
  2069. }
  2070. // If we've got a post_type AND it's not "any" post_type.
  2071. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  2072. foreach ( (array)$q['post_type'] as $_post_type ) {
  2073. $ptype_obj = get_post_type_object($_post_type);
  2074. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  2075. continue;
  2076. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  2077. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  2078. $q['name'] = $q[ $ptype_obj->query_var ];
  2079. } else {
  2080. // Hierarchical post_types will operate through the
  2081. $q['pagename'] = $q[ $ptype_obj->query_var ];
  2082. $q['name'] = '';
  2083. }
  2084. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  2085. break;
  2086. } //end foreach
  2087. unset($ptype_obj);
  2088. }
  2089. if ( '' != $q['name'] ) {
  2090. $q['name'] = sanitize_title_for_query( $q['name'] );
  2091. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  2092. } elseif ( '' != $q['pagename'] ) {
  2093. if ( isset($this->queried_object_id) ) {
  2094. $reqpage = $this->queried_object_id;
  2095. } else {
  2096. if ( 'page' != $q['post_type'] ) {
  2097. foreach ( (array)$q['post_type'] as $_post_type ) {
  2098. $ptype_obj = get_post_type_object($_post_type);
  2099. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  2100. continue;
  2101. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  2102. if ( $reqpage )
  2103. break;
  2104. }
  2105. unset($ptype_obj);
  2106. } else {
  2107. $reqpage = get_page_by_path($q['pagename']);
  2108. }
  2109. if ( !empty($reqpage) )
  2110. $reqpage = $reqpage->ID;
  2111. else
  2112. $reqpage = 0;
  2113. }
  2114. $page_for_posts = get_option('page_for_posts');
  2115. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  2116. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  2117. $q['name'] = $q['pagename'];
  2118. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  2119. $reqpage_obj = get_post( $reqpage );
  2120. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  2121. $this->is_attachment = true;
  2122. $post_type = $q['post_type'] = 'attachment';
  2123. $this->is_page = true;
  2124. $q['attachment_id'] = $reqpage;
  2125. }
  2126. }
  2127. } elseif ( '' != $q['attachment'] ) {
  2128. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  2129. $q['name'] = $q['attachment'];
  2130. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  2131. }
  2132. if ( intval($q['comments_popup']) )
  2133. $q['p'] = absint($q['comments_popup']);
  2134. // If an attachment is requested by number, let it supersede any post number.
  2135. if ( $q['attachment_id'] )
  2136. $q['p'] = absint($q['attachment_id']);
  2137. // If a post number is specified, load that post
  2138. if ( $q['p'] ) {
  2139. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  2140. } elseif ( $q['post__in'] ) {
  2141. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  2142. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  2143. } elseif ( $q['post__not_in'] ) {
  2144. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  2145. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  2146. }
  2147. if ( is_numeric( $q['post_parent'] ) ) {
  2148. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  2149. } elseif ( $q['post_parent__in'] ) {
  2150. $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
  2151. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
  2152. } elseif ( $q['post_parent__not_in'] ) {
  2153. $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
  2154. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
  2155. }
  2156. if ( $q['page_id'] ) {
  2157. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  2158. $q['p'] = $q['page_id'];
  2159. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  2160. }
  2161. }
  2162. // If a search pattern is specified, load the posts that match.
  2163. if ( ! empty( $q['s'] ) )
  2164. $search = $this->parse_search( $q );
  2165. /**
  2166. * Filter the search SQL that is used in the WHERE clause of WP_Query.
  2167. *
  2168. * @since 3.0.0
  2169. *
  2170. * @param string $search Search SQL for WHERE clause.
  2171. * @param WP_Query $this The current WP_Query object.
  2172. */
  2173. $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
  2174. // Taxonomies
  2175. if ( !$this->is_singular ) {
  2176. $this->parse_tax_query( $q );
  2177. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  2178. $join .= $clauses['join'];
  2179. $where .= $clauses['where'];
  2180. }
  2181. if ( $this->is_tax ) {
  2182. if ( empty($post_type) ) {
  2183. // Do a fully inclusive search for currently registered post types of queried taxonomies
  2184. $post_type = array();
  2185. $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
  2186. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
  2187. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
  2188. if ( array_intersect( $taxonomies, $object_taxonomies ) )
  2189. $post_type[] = $pt;
  2190. }
  2191. if ( ! $post_type )
  2192. $post_type = 'any';
  2193. elseif ( count( $post_type ) == 1 )
  2194. $post_type = $post_type[0];
  2195. $post_status_join = true;
  2196. } elseif ( in_array('attachment', (array) $post_type) ) {
  2197. $post_status_join = true;
  2198. }
  2199. }
  2200. // Back-compat
  2201. if ( !empty($this->tax_query->queries) ) {
  2202. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2203. if ( !empty( $tax_query_in_and ) ) {
  2204. if ( !isset( $q['taxonomy'] ) ) {
  2205. foreach ( $tax_query_in_and as $a_tax_query ) {
  2206. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  2207. $q['taxonomy'] = $a_tax_query['taxonomy'];
  2208. if ( 'slug' == $a_tax_query['field'] )
  2209. $q['term'] = $a_tax_query['terms'][0];
  2210. else
  2211. $q['term_id'] = $a_tax_query['terms'][0];
  2212. break;
  2213. }
  2214. }
  2215. }
  2216. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  2217. if ( ! empty( $cat_query ) ) {
  2218. $cat_query = reset( $cat_query );
  2219. if ( ! empty( $cat_query['terms'][0] ) ) {
  2220. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  2221. if ( $the_cat ) {
  2222. $this->set( 'cat', $the_cat->term_id );
  2223. $this->set( 'category_name', $the_cat->slug );
  2224. }
  2225. unset( $the_cat );
  2226. }
  2227. }
  2228. unset( $cat_query );
  2229. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  2230. if ( ! empty( $tag_query ) ) {
  2231. $tag_query = reset( $tag_query );
  2232. if ( ! empty( $tag_query['terms'][0] ) ) {
  2233. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  2234. if ( $the_tag )
  2235. $this->set( 'tag_id', $the_tag->term_id );
  2236. unset( $the_tag );
  2237. }
  2238. }
  2239. unset( $tag_query );
  2240. }
  2241. }
  2242. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  2243. $groupby = "{$wpdb->posts}.ID";
  2244. }
  2245. // Author/user stuff
  2246. if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
  2247. $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
  2248. $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
  2249. foreach ( $authors as $author ) {
  2250. $key = $author > 0 ? 'author__in' : 'author__not_in';
  2251. $q[$key][] = abs( $author );
  2252. }
  2253. $q['author'] = implode( ',', $authors );
  2254. }
  2255. if ( ! empty( $q['author__not_in'] ) ) {
  2256. $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
  2257. $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
  2258. } elseif ( ! empty( $q['author__in'] ) ) {
  2259. $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
  2260. $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
  2261. }
  2262. // Author stuff for nice URLs
  2263. if ( '' != $q['author_name'] ) {
  2264. if ( strpos($q['author_name'], '/') !== false ) {
  2265. $q['author_name'] = explode('/', $q['author_name']);
  2266. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2267. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2268. } else {
  2269. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2270. }
  2271. }
  2272. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2273. $q['author'] = get_user_by('slug', $q['author_name']);
  2274. if ( $q['author'] )
  2275. $q['author'] = $q['author']->ID;
  2276. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2277. }
  2278. // MIME-Type stuff for attachment browsing
  2279. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  2280. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  2281. $where .= $search . $whichauthor . $whichmimetype;
  2282. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2283. $q['order'] = 'DESC';
  2284. // Order by
  2285. if ( empty($q['orderby']) ) {
  2286. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2287. } elseif ( 'none' == $q['orderby'] ) {
  2288. $orderby = '';
  2289. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  2290. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  2291. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  2292. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  2293. } else {
  2294. // Used to filter values
  2295. $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2296. if ( !empty($q['meta_key']) ) {
  2297. $allowed_keys[] = $q['meta_key'];
  2298. $allowed_keys[] = 'meta_value';
  2299. $allowed_keys[] = 'meta_value_num';
  2300. }
  2301. $q['orderby'] = urldecode($q['orderby']);
  2302. $q['orderby'] = addslashes_gpc($q['orderby']);
  2303. $orderby_array = array();
  2304. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2305. // Only allow certain values for safety
  2306. if ( ! in_array($orderby, $allowed_keys) )
  2307. continue;
  2308. switch ( $orderby ) {
  2309. case 'menu_order':
  2310. $orderby = "$wpdb->posts.menu_order";
  2311. break;
  2312. case 'ID':
  2313. $orderby = "$wpdb->posts.ID";
  2314. break;
  2315. case 'rand':
  2316. $orderby = 'RAND()';
  2317. break;
  2318. case $q['meta_key']:
  2319. case 'meta_value':
  2320. if ( isset( $q['meta_type'] ) ) {
  2321. $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
  2322. $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
  2323. } else {
  2324. $orderby = "$wpdb->postmeta.meta_value";
  2325. }
  2326. break;
  2327. case 'meta_value_num':
  2328. $orderby = "$wpdb->postmeta.meta_value+0";
  2329. break;
  2330. case 'comment_count':
  2331. $orderby = "$wpdb->posts.comment_count";
  2332. break;
  2333. default:
  2334. $orderby = "$wpdb->posts.post_" . $orderby;
  2335. }
  2336. $orderby_array[] = $orderby;
  2337. }
  2338. $orderby = implode( ',', $orderby_array );
  2339. if ( empty( $orderby ) )
  2340. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2341. else
  2342. $orderby .= " {$q['order']}";
  2343. }
  2344. // Order search results by relevance only when another "orderby" is not specified in the query.
  2345. if ( ! empty( $q['s'] ) ) {
  2346. $search_orderby = '';
  2347. if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
  2348. $search_orderby = $this->parse_search_order( $q );
  2349. /**
  2350. * Filter the ORDER BY used when ordering search results.
  2351. *
  2352. * @since 3.7.0
  2353. *
  2354. * @param string $search_orderby The ORDER BY clause.
  2355. * @param WP_Query $this The current WP_Query instance.
  2356. */
  2357. $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
  2358. if ( $search_orderby )
  2359. $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
  2360. }
  2361. if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
  2362. $post_type_cap = 'multiple_post_type';
  2363. } else {
  2364. if ( is_array( $post_type ) )
  2365. $post_type = reset( $post_type );
  2366. $post_type_object = get_post_type_object( $post_type );
  2367. if ( empty( $post_type_object ) )
  2368. $post_type_cap = $post_type;
  2369. }
  2370. if ( isset( $q['post_password'] ) ) {
  2371. $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
  2372. if ( empty( $q['perm'] ) ) {
  2373. $q['perm'] = 'readable';
  2374. }
  2375. } elseif ( isset( $q['has_password'] ) ) {
  2376. $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
  2377. }
  2378. if ( 'any' == $post_type ) {
  2379. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2380. if ( empty( $in_search_post_types ) )
  2381. $where .= ' AND 1=0 ';
  2382. else
  2383. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
  2384. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2385. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2386. } elseif ( ! empty( $post_type ) ) {
  2387. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2388. $post_type_object = get_post_type_object ( $post_type );
  2389. } elseif ( $this->is_attachment ) {
  2390. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2391. $post_type_object = get_post_type_object ( 'attachment' );
  2392. } elseif ( $this->is_page ) {
  2393. $where .= " AND $wpdb->posts.post_type = 'page'";
  2394. $post_type_object = get_post_type_object ( 'page' );
  2395. } else {
  2396. $where .= " AND $wpdb->posts.post_type = 'post'";
  2397. $post_type_object = get_post_type_object ( 'post' );
  2398. }
  2399. $edit_cap = 'edit_post';
  2400. $read_cap = 'read_post';
  2401. if ( ! empty( $post_type_object ) ) {
  2402. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2403. $read_private_cap = $post_type_object->cap->read_private_posts;
  2404. } else {
  2405. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2406. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2407. }
  2408. $user_id = get_current_user_id();
  2409. if ( ! empty( $q['post_status'] ) ) {
  2410. $statuswheres = array();
  2411. $q_status = $q['post_status'];
  2412. if ( ! is_array( $q_status ) )
  2413. $q_status = explode(',', $q_status);
  2414. $r_status = array();
  2415. $p_status = array();
  2416. $e_status = array();
  2417. if ( in_array('any', $q_status) ) {
  2418. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2419. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2420. } else {
  2421. foreach ( get_post_stati() as $status ) {
  2422. if ( in_array( $status, $q_status ) ) {
  2423. if ( 'private' == $status )
  2424. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2425. else
  2426. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2427. }
  2428. }
  2429. }
  2430. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2431. $r_status = array_merge($r_status, $p_status);
  2432. unset($p_status);
  2433. }
  2434. if ( !empty($e_status) ) {
  2435. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2436. }
  2437. if ( !empty($r_status) ) {
  2438. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2439. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
  2440. else
  2441. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2442. }
  2443. if ( !empty($p_status) ) {
  2444. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2445. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
  2446. else
  2447. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2448. }
  2449. if ( $post_status_join ) {
  2450. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2451. foreach ( $statuswheres as $index => $statuswhere )
  2452. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2453. }
  2454. $where_status = implode( ' OR ', $statuswheres );
  2455. if ( ! empty( $where_status ) ) {
  2456. $where .= " AND ($where_status)";
  2457. }
  2458. } elseif ( !$this->is_singular ) {
  2459. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2460. // Add public states.
  2461. $public_states = get_post_stati( array('public' => true) );
  2462. foreach ( (array) $public_states as $state ) {
  2463. if ( 'publish' == $state ) // Publish is hard-coded above.
  2464. continue;
  2465. $where .= " OR $wpdb->posts.post_status = '$state'";
  2466. }
  2467. if ( $this->is_admin ) {
  2468. // Add protected states that should show in the admin all list.
  2469. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2470. foreach ( (array) $admin_all_states as $state )
  2471. $where .= " OR $wpdb->posts.post_status = '$state'";
  2472. }
  2473. if ( is_user_logged_in() ) {
  2474. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2475. $private_states = get_post_stati( array('private' => true) );
  2476. foreach ( (array) $private_states as $state )
  2477. $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'";
  2478. }
  2479. $where .= ')';
  2480. }
  2481. if ( !empty( $this->meta_query->queries ) ) {
  2482. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2483. $join .= $clauses['join'];
  2484. $where .= $clauses['where'];
  2485. }
  2486. /*
  2487. * Apply filters on where and join prior to paging so that any
  2488. * manipulations to them are reflected in the paging by day queries.
  2489. */
  2490. if ( !$q['suppress_filters'] ) {
  2491. /**
  2492. * Filter the WHERE clause of the query.
  2493. *
  2494. * @since 1.5.0
  2495. *
  2496. * @param string $where The WHERE clause of the query.
  2497. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2498. */
  2499. $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) );
  2500. /**
  2501. * Filter the JOIN clause of the query.
  2502. *
  2503. * @since 1.5.0
  2504. *
  2505. * @param string $where The JOIN clause of the query.
  2506. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2507. */
  2508. $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) );
  2509. }
  2510. // Paging
  2511. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2512. $page = absint($q['paged']);
  2513. if ( !$page )
  2514. $page = 1;
  2515. if ( empty($q['offset']) ) {
  2516. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2517. } else { // we're ignoring $page and using 'offset'
  2518. $q['offset'] = absint($q['offset']);
  2519. $pgstrt = $q['offset'] . ', ';
  2520. }
  2521. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2522. }
  2523. // Comments feeds
  2524. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2525. if ( $this->is_archive || $this->is_search ) {
  2526. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2527. $cwhere = "WHERE comment_approved = '1' $where";
  2528. $cgroupby = "$wpdb->comments.comment_id";
  2529. } else { // Other non singular e.g. front
  2530. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2531. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2532. $cgroupby = '';
  2533. }
  2534. if ( !$q['suppress_filters'] ) {
  2535. /**
  2536. * Filter the JOIN clause of the comments feed query before sending.
  2537. *
  2538. * @since 2.2.0
  2539. *
  2540. * @param string $cjoin The JOIN clause of the query.
  2541. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2542. */
  2543. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
  2544. /**
  2545. * Filter the WHERE clause of the comments feed query before sending.
  2546. *
  2547. * @since 2.2.0
  2548. *
  2549. * @param string $cwhere The WHERE clause of the query.
  2550. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2551. */
  2552. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
  2553. /**
  2554. * Filter the GROUP BY clause of the comments feed query before sending.
  2555. *
  2556. * @since 2.2.0
  2557. *
  2558. * @param string $cgroupby The GROUP BY clause of the query.
  2559. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2560. */
  2561. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
  2562. /**
  2563. * Filter the ORDER BY clause of the comments feed query before sending.
  2564. *
  2565. * @since 2.8.0
  2566. *
  2567. * @param string $corderby The ORDER BY clause of the query.
  2568. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2569. */
  2570. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2571. /**
  2572. * Filter the LIMIT clause of the comments feed query before sending.
  2573. *
  2574. * @since 2.8.0
  2575. *
  2576. * @param string $climits The JOIN clause of the query.
  2577. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2578. */
  2579. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2580. }
  2581. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2582. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2583. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2584. $this->comment_count = count($this->comments);
  2585. $post_ids = array();
  2586. foreach ( $this->comments as $comment )
  2587. $post_ids[] = (int) $comment->comment_post_ID;
  2588. $post_ids = join(',', $post_ids);
  2589. $join = '';
  2590. if ( $post_ids )
  2591. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2592. else
  2593. $where = "AND 0";
  2594. }
  2595. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2596. /*
  2597. * Apply post-paging filters on where and join. Only plugins that
  2598. * manipulate paging queries should use these hooks.
  2599. */
  2600. if ( !$q['suppress_filters'] ) {
  2601. /**
  2602. * Filter the WHERE clause of the query.
  2603. *
  2604. * Specifically for manipulating paging queries.
  2605. *
  2606. * @since 1.5.0
  2607. *
  2608. * @param string $where The WHERE clause of the query.
  2609. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2610. */
  2611. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2612. /**
  2613. * Filter the GROUP BY clause of the query.
  2614. *
  2615. * @since 2.0.0
  2616. *
  2617. * @param string $groupby The GROUP BY clause of the query.
  2618. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2619. */
  2620. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2621. /**
  2622. * Filter the JOIN clause of the query.
  2623. *
  2624. * Specifically for manipulating paging queries.
  2625. *
  2626. * @since 1.5.0
  2627. *
  2628. * @param string $join The JOIN clause of the query.
  2629. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2630. */
  2631. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2632. /**
  2633. * Filter the ORDER BY clause of the query.
  2634. *
  2635. * @since 1.5.1
  2636. *
  2637. * @param string $orderby The ORDER BY clause of the query.
  2638. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2639. */
  2640. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2641. /**
  2642. * Filter the DISTINCT clause of the query.
  2643. *
  2644. * @since 2.1.0
  2645. *
  2646. * @param string $distinct The DISTINCT clause of the query.
  2647. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2648. */
  2649. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2650. /**
  2651. * Filter the LIMIT clause of the query.
  2652. *
  2653. * @since 2.1.0
  2654. *
  2655. * @param string $limits The LIMIT clause of the query.
  2656. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2657. */
  2658. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2659. /**
  2660. * Filter the SELECT clause of the query.
  2661. *
  2662. * @since 2.1.0
  2663. *
  2664. * @param string $fields The SELECT clause of the query.
  2665. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2666. */
  2667. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2668. /**
  2669. * Filter all query clauses at once, for convenience.
  2670. *
  2671. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2672. * fields (SELECT), and LIMITS clauses.
  2673. *
  2674. * @since 3.1.0
  2675. *
  2676. * @param array $clauses The list of clauses for the query.
  2677. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2678. */
  2679. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2680. foreach ( $pieces as $piece ) {
  2681. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2682. }
  2683. }
  2684. /**
  2685. * Fires to announce the query's current selection parameters.
  2686. *
  2687. * For use by caching plugins.
  2688. *
  2689. * @since 2.3.0
  2690. *
  2691. * @param string $selection The assembled selection query.
  2692. */
  2693. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2694. /*
  2695. * Filter again for the benefit of caching plugins.
  2696. * Regular plugins should use the hooks above.
  2697. */
  2698. if ( !$q['suppress_filters'] ) {
  2699. /**
  2700. * Filter the WHERE clause of the query.
  2701. *
  2702. * For use by caching plugins.
  2703. *
  2704. * @since 2.5.0
  2705. *
  2706. * @param string $where The WHERE clause of the query.
  2707. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2708. */
  2709. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2710. /**
  2711. * Filter the GROUP BY clause of the query.
  2712. *
  2713. * For use by caching plugins.
  2714. *
  2715. * @since 2.5.0
  2716. *
  2717. * @param string $groupby The GROUP BY clause of the query.
  2718. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2719. */
  2720. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2721. /**
  2722. * Filter the JOIN clause of the query.
  2723. *
  2724. * For use by caching plugins.
  2725. *
  2726. * @since 2.5.0
  2727. *
  2728. * @param string $join The JOIN clause of the query.
  2729. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2730. */
  2731. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2732. /**
  2733. * Filter the ORDER BY clause of the query.
  2734. *
  2735. * For use by caching plugins.
  2736. *
  2737. * @since 2.5.0
  2738. *
  2739. * @param string $orderby The ORDER BY clause of the query.
  2740. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2741. */
  2742. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2743. /**
  2744. * Filter the DISTINCT clause of the query.
  2745. *
  2746. * For use by caching plugins.
  2747. *
  2748. * @since 2.5.0
  2749. *
  2750. * @param string $distinct The DISTINCT clause of the query.
  2751. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2752. */
  2753. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2754. /**
  2755. * Filter the SELECT clause of the query.
  2756. *
  2757. * For use by caching plugins.
  2758. *
  2759. * @since 2.5.0
  2760. *
  2761. * @param string $fields The SELECT clause of the query.
  2762. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2763. */
  2764. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2765. /**
  2766. * Filter the LIMIT clause of the query.
  2767. *
  2768. * For use by caching plugins.
  2769. *
  2770. * @since 2.5.0
  2771. *
  2772. * @param string $limits The LIMIT clause of the query.
  2773. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2774. */
  2775. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2776. /**
  2777. * Filter all query clauses at once, for convenience.
  2778. *
  2779. * For use by caching plugins.
  2780. *
  2781. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2782. * fields (SELECT), and LIMITS clauses.
  2783. *
  2784. * @since 3.1.0
  2785. *
  2786. * @param array $pieces The pieces of the query.
  2787. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2788. */
  2789. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2790. foreach ( $pieces as $piece )
  2791. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2792. }
  2793. if ( ! empty($groupby) )
  2794. $groupby = 'GROUP BY ' . $groupby;
  2795. if ( !empty( $orderby ) )
  2796. $orderby = 'ORDER BY ' . $orderby;
  2797. $found_rows = '';
  2798. if ( !$q['no_found_rows'] && !empty($limits) )
  2799. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2800. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2801. if ( !$q['suppress_filters'] ) {
  2802. /**
  2803. * Filter the completed SQL query before sending.
  2804. *
  2805. * @since 2.0.0
  2806. *
  2807. * @param array $request The complete SQL query.
  2808. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2809. */
  2810. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2811. }
  2812. if ( 'ids' == $q['fields'] ) {
  2813. $this->posts = $wpdb->get_col( $this->request );
  2814. $this->post_count = count( $this->posts );
  2815. $this->set_found_posts( $q, $limits );
  2816. return array_map( 'intval', $this->posts );
  2817. }
  2818. if ( 'id=>parent' == $q['fields'] ) {
  2819. $this->posts = $wpdb->get_results( $this->request );
  2820. $this->post_count = count( $this->posts );
  2821. $this->set_found_posts( $q, $limits );
  2822. $r = array();
  2823. foreach ( $this->posts as $post ) {
  2824. $r[ (int) $post->ID ] = (int) $post->post_parent;
  2825. }
  2826. return $r;
  2827. }
  2828. $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  2829. /**
  2830. * Filter whether to split the query.
  2831. *
  2832. * Splitting the query will cause it to fetch just the IDs of the found posts
  2833. * (and then individually fetch each post by ID), rather than fetching every
  2834. * complete row at once. One massive result vs. many small results.
  2835. *
  2836. * @since 3.4.0
  2837. *
  2838. * @param bool $split_the_query Whether or not to split the query.
  2839. * @param WP_Query $this The WP_Query instance.
  2840. */
  2841. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  2842. if ( $split_the_query ) {
  2843. // First get the IDs and then fill in the objects
  2844. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2845. /**
  2846. * Filter the Post IDs SQL request before sending.
  2847. *
  2848. * @since 3.4.0
  2849. *
  2850. * @param string $request The post ID request.
  2851. * @param WP_Query $this The WP_Query instance.
  2852. */
  2853. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2854. $ids = $wpdb->get_col( $this->request );
  2855. if ( $ids ) {
  2856. $this->posts = $ids;
  2857. $this->set_found_posts( $q, $limits );
  2858. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2859. } else {
  2860. $this->posts = array();
  2861. }
  2862. } else {
  2863. $this->posts = $wpdb->get_results( $this->request );
  2864. $this->set_found_posts( $q, $limits );
  2865. }
  2866. // Convert to WP_Post objects
  2867. if ( $this->posts )
  2868. $this->posts = array_map( 'get_post', $this->posts );
  2869. if ( ! $q['suppress_filters'] ) {
  2870. /**
  2871. * Filter the raw post results array, prior to status checks.
  2872. *
  2873. * @since 2.3.0
  2874. *
  2875. * @param array $posts The post results array.
  2876. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2877. */
  2878. $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
  2879. }
  2880. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2881. /** This filter is documented in wp-includes/query.php */
  2882. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
  2883. /** This filter is documented in wp-includes/query.php */
  2884. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2885. /** This filter is documented in wp-includes/query.php */
  2886. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
  2887. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2888. /** This filter is documented in wp-includes/query.php */
  2889. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2890. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2891. /** This filter is documented in wp-includes/query.php */
  2892. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2893. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2894. $this->comments = $wpdb->get_results($comments_request);
  2895. $this->comment_count = count($this->comments);
  2896. }
  2897. // Check post status to determine if post should be displayed.
  2898. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2899. $status = get_post_status($this->posts[0]);
  2900. $post_status_obj = get_post_status_object($status);
  2901. //$type = get_post_type($this->posts[0]);
  2902. if ( !$post_status_obj->public ) {
  2903. if ( ! is_user_logged_in() ) {
  2904. // User must be logged in to view unpublished posts.
  2905. $this->posts = array();
  2906. } else {
  2907. if ( $post_status_obj->protected ) {
  2908. // User must have edit permissions on the draft to preview.
  2909. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2910. $this->posts = array();
  2911. } else {
  2912. $this->is_preview = true;
  2913. if ( 'future' != $status )
  2914. $this->posts[0]->post_date = current_time('mysql');
  2915. }
  2916. } elseif ( $post_status_obj->private ) {
  2917. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2918. $this->posts = array();
  2919. } else {
  2920. $this->posts = array();
  2921. }
  2922. }
  2923. }
  2924. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
  2925. /**
  2926. * Filter the single post for preview mode.
  2927. *
  2928. * @since 2.7.0
  2929. *
  2930. * @param WP_Post $post_preview The Post object.
  2931. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2932. */
  2933. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  2934. }
  2935. }
  2936. // Put sticky posts at the top of the posts array
  2937. $sticky_posts = get_option('sticky_posts');
  2938. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2939. $num_posts = count($this->posts);
  2940. $sticky_offset = 0;
  2941. // Loop over posts and relocate stickies to the front.
  2942. for ( $i = 0; $i < $num_posts; $i++ ) {
  2943. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2944. $sticky_post = $this->posts[$i];
  2945. // Remove sticky from current position
  2946. array_splice($this->posts, $i, 1);
  2947. // Move to front, after other stickies
  2948. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2949. // Increment the sticky offset. The next sticky will be placed at this offset.
  2950. $sticky_offset++;
  2951. // Remove post from sticky posts array
  2952. $offset = array_search($sticky_post->ID, $sticky_posts);
  2953. unset( $sticky_posts[$offset] );
  2954. }
  2955. }
  2956. // If any posts have been excluded specifically, Ignore those that are sticky.
  2957. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2958. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2959. // Fetch sticky posts that weren't in the query results
  2960. if ( !empty($sticky_posts) ) {
  2961. $stickies = get_posts( array(
  2962. 'post__in' => $sticky_posts,
  2963. 'post_type' => $post_type,
  2964. 'post_status' => 'publish',
  2965. 'nopaging' => true
  2966. ) );
  2967. foreach ( $stickies as $sticky_post ) {
  2968. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  2969. $sticky_offset++;
  2970. }
  2971. }
  2972. }
  2973. if ( ! $q['suppress_filters'] ) {
  2974. /**
  2975. * Filter the array of retrieved posts after they've been fetched and
  2976. * internally processed.
  2977. *
  2978. * @since 1.5.0
  2979. *
  2980. * @param array $posts The array of retrieved posts.
  2981. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2982. */
  2983. $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) );
  2984. }
  2985. // Ensure that any posts added/modified via one of the filters above are
  2986. // of the type WP_Post and are filtered.
  2987. if ( $this->posts ) {
  2988. $this->post_count = count( $this->posts );
  2989. $this->posts = array_map( 'get_post', $this->posts );
  2990. if ( $q['cache_results'] )
  2991. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2992. $this->post = reset( $this->posts );
  2993. } else {
  2994. $this->post_count = 0;
  2995. $this->posts = array();
  2996. }
  2997. return $this->posts;
  2998. }
  2999. /**
  3000. * Set up the amount of found posts and the number of pages (if limit clause was used)
  3001. * for the current query.
  3002. *
  3003. * @since 3.5.0
  3004. * @access private
  3005. */
  3006. function set_found_posts( $q, $limits ) {
  3007. global $wpdb;
  3008. // Bail if posts is an empty array. Continue if posts is an empty string,
  3009. // null, or false to accommodate caching plugins that fill posts later.
  3010. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  3011. return;
  3012. if ( ! empty( $limits ) ) {
  3013. /**
  3014. * Filter the query to run for retrieving the found posts.
  3015. *
  3016. * @since 2.1.0
  3017. *
  3018. * @param string $found_posts The query to run to find the found posts.
  3019. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3020. */
  3021. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  3022. } else {
  3023. $this->found_posts = count( $this->posts );
  3024. }
  3025. /**
  3026. * Filter the number of found posts for the query.
  3027. *
  3028. * @since 2.1.0
  3029. *
  3030. * @param int $found_posts The number of posts found.
  3031. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3032. */
  3033. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  3034. if ( ! empty( $limits ) )
  3035. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  3036. }
  3037. /**
  3038. * Set up the next post and iterate current post index.
  3039. *
  3040. * @since 1.5.0
  3041. * @access public
  3042. *
  3043. * @return WP_Post Next post.
  3044. */
  3045. function next_post() {
  3046. $this->current_post++;
  3047. $this->post = $this->posts[$this->current_post];
  3048. return $this->post;
  3049. }
  3050. /**
  3051. * Sets up the current post.
  3052. *
  3053. * Retrieves the next post, sets up the post, sets the 'in the loop'
  3054. * property to true.
  3055. *
  3056. * @since 1.5.0
  3057. * @access public
  3058. * @uses $post
  3059. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  3060. */
  3061. function the_post() {
  3062. global $post;
  3063. $this->in_the_loop = true;
  3064. if ( $this->current_post == -1 ) // loop has just started
  3065. /**
  3066. * Fires once the loop is started.
  3067. *
  3068. * @since 2.0.0
  3069. *
  3070. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3071. */
  3072. do_action_ref_array( 'loop_start', array( &$this ) );
  3073. $post = $this->next_post();
  3074. setup_postdata($post);
  3075. }
  3076. /**
  3077. * Whether there are more posts available in the loop.
  3078. *
  3079. * Calls action 'loop_end', when the loop is complete.
  3080. *
  3081. * @since 1.5.0
  3082. * @access public
  3083. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  3084. *
  3085. * @return bool True if posts are available, false if end of loop.
  3086. */
  3087. function have_posts() {
  3088. if ( $this->current_post + 1 < $this->post_count ) {
  3089. return true;
  3090. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  3091. /**
  3092. * Fires once the loop has ended.
  3093. *
  3094. * @since 2.0.0
  3095. *
  3096. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3097. */
  3098. do_action_ref_array( 'loop_end', array( &$this ) );
  3099. // Do some cleaning up after the loop
  3100. $this->rewind_posts();
  3101. }
  3102. $this->in_the_loop = false;
  3103. return false;
  3104. }
  3105. /**
  3106. * Rewind the posts and reset post index.
  3107. *
  3108. * @since 1.5.0
  3109. * @access public
  3110. */
  3111. function rewind_posts() {
  3112. $this->current_post = -1;
  3113. if ( $this->post_count > 0 ) {
  3114. $this->post = $this->posts[0];
  3115. }
  3116. }
  3117. /**
  3118. * Iterate current comment index and return comment object.
  3119. *
  3120. * @since 2.2.0
  3121. * @access public
  3122. *
  3123. * @return object Comment object.
  3124. */
  3125. function next_comment() {
  3126. $this->current_comment++;
  3127. $this->comment = $this->comments[$this->current_comment];
  3128. return $this->comment;
  3129. }
  3130. /**
  3131. * Sets up the current comment.
  3132. *
  3133. * @since 2.2.0
  3134. * @access public
  3135. * @global object $comment Current comment.
  3136. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  3137. */
  3138. function the_comment() {
  3139. global $comment;
  3140. $comment = $this->next_comment();
  3141. if ( $this->current_comment == 0 ) {
  3142. /**
  3143. * Fires once the comment loop is started.
  3144. *
  3145. * @since 2.2.0
  3146. */
  3147. do_action( 'comment_loop_start' );
  3148. }
  3149. }
  3150. /**
  3151. * Whether there are more comments available.
  3152. *
  3153. * Automatically rewinds comments when finished.
  3154. *
  3155. * @since 2.2.0
  3156. * @access public
  3157. *
  3158. * @return bool True, if more comments. False, if no more posts.
  3159. */
  3160. function have_comments() {
  3161. if ( $this->current_comment + 1 < $this->comment_count ) {
  3162. return true;
  3163. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  3164. $this->rewind_comments();
  3165. }
  3166. return false;
  3167. }
  3168. /**
  3169. * Rewind the comments, resets the comment index and comment to first.
  3170. *
  3171. * @since 2.2.0
  3172. * @access public
  3173. */
  3174. function rewind_comments() {
  3175. $this->current_comment = -1;
  3176. if ( $this->comment_count > 0 ) {
  3177. $this->comment = $this->comments[0];
  3178. }
  3179. }
  3180. /**
  3181. * Sets up the WordPress query by parsing query string.
  3182. *
  3183. * @since 1.5.0
  3184. * @access public
  3185. *
  3186. * @param string $query URL query string.
  3187. * @return array List of posts.
  3188. */
  3189. function query( $query ) {
  3190. $this->init();
  3191. $this->query = $this->query_vars = wp_parse_args( $query );
  3192. return $this->get_posts();
  3193. }
  3194. /**
  3195. * Retrieve queried object.
  3196. *
  3197. * If queried object is not set, then the queried object will be set from
  3198. * the category, tag, taxonomy, posts page, single post, page, or author
  3199. * query variable. After it is set up, it will be returned.
  3200. *
  3201. * @since 1.5.0
  3202. * @access public
  3203. *
  3204. * @return object
  3205. */
  3206. function get_queried_object() {
  3207. if ( isset($this->queried_object) )
  3208. return $this->queried_object;
  3209. $this->queried_object = null;
  3210. $this->queried_object_id = 0;
  3211. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  3212. if ( $this->is_category ) {
  3213. if ( $this->get( 'cat' ) ) {
  3214. $term = get_term( $this->get( 'cat' ), 'category' );
  3215. } elseif ( $this->get( 'category_name' ) ) {
  3216. $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
  3217. }
  3218. } elseif ( $this->is_tag ) {
  3219. if ( $this->get( 'tag_id' ) ) {
  3220. $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
  3221. } elseif ( $this->get( 'tag' ) ) {
  3222. $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
  3223. }
  3224. } else {
  3225. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  3226. $query = reset( $tax_query_in_and );
  3227. if ( $query['terms'] ) {
  3228. if ( 'term_id' == $query['field'] ) {
  3229. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  3230. } else {
  3231. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  3232. }
  3233. }
  3234. }
  3235. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  3236. $this->queried_object = $term;
  3237. $this->queried_object_id = (int) $term->term_id;
  3238. if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
  3239. _make_cat_compat( $this->queried_object );
  3240. }
  3241. } elseif ( $this->is_post_type_archive ) {
  3242. $post_type = $this->get( 'post_type' );
  3243. if ( is_array( $post_type ) )
  3244. $post_type = reset( $post_type );
  3245. $this->queried_object = get_post_type_object( $post_type );
  3246. } elseif ( $this->is_posts_page ) {
  3247. $page_for_posts = get_option('page_for_posts');
  3248. $this->queried_object = get_post( $page_for_posts );
  3249. $this->queried_object_id = (int) $this->queried_object->ID;
  3250. } elseif ( $this->is_singular && !is_null($this->post) ) {
  3251. $this->queried_object = $this->post;
  3252. $this->queried_object_id = (int) $this->post->ID;
  3253. } elseif ( $this->is_author ) {
  3254. $this->queried_object_id = (int) $this->get('author');
  3255. $this->queried_object = get_userdata( $this->queried_object_id );
  3256. }
  3257. return $this->queried_object;
  3258. }
  3259. /**
  3260. * Retrieve ID of the current queried object.
  3261. *
  3262. * @since 1.5.0
  3263. * @access public
  3264. *
  3265. * @return int
  3266. */
  3267. function get_queried_object_id() {
  3268. $this->get_queried_object();
  3269. if ( isset($this->queried_object_id) ) {
  3270. return $this->queried_object_id;
  3271. }
  3272. return 0;
  3273. }
  3274. /**
  3275. * Constructor.
  3276. *
  3277. * Sets up the WordPress query, if parameter is not empty.
  3278. *
  3279. * @since 1.5.0
  3280. * @access public
  3281. *
  3282. * @param string $query URL query string.
  3283. * @return WP_Query
  3284. */
  3285. function __construct($query = '') {
  3286. if ( ! empty($query) ) {
  3287. $this->query($query);
  3288. }
  3289. }
  3290. /**
  3291. * Is the query for an existing archive page?
  3292. *
  3293. * Month, Year, Category, Author, Post Type archive...
  3294. *
  3295. * @since 3.1.0
  3296. *
  3297. * @return bool
  3298. */
  3299. function is_archive() {
  3300. return (bool) $this->is_archive;
  3301. }
  3302. /**
  3303. * Is the query for an existing post type archive page?
  3304. *
  3305. * @since 3.1.0
  3306. *
  3307. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  3308. * @return bool
  3309. */
  3310. function is_post_type_archive( $post_types = '' ) {
  3311. if ( empty( $post_types ) || ! $this->is_post_type_archive )
  3312. return (bool) $this->is_post_type_archive;
  3313. $post_type = $this->get( 'post_type' );
  3314. if ( is_array( $post_type ) )
  3315. $post_type = reset( $post_type );
  3316. $post_type_object = get_post_type_object( $post_type );
  3317. return in_array( $post_type_object->name, (array) $post_types );
  3318. }
  3319. /**
  3320. * Is the query for an existing attachment page?
  3321. *
  3322. * @since 3.1.0
  3323. *
  3324. * @param mixed $attachment Attachment ID, title, slug, or array of such.
  3325. * @return bool
  3326. */
  3327. function is_attachment( $attachment = '' ) {
  3328. if ( ! $this->is_attachment ) {
  3329. return false;
  3330. }
  3331. if ( empty( $attachment ) ) {
  3332. return true;
  3333. }
  3334. $attachment = (array) $attachment;
  3335. $post_obj = $this->get_queried_object();
  3336. if ( in_array( $post_obj->ID, $attachment ) ) {
  3337. return true;
  3338. } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
  3339. return true;
  3340. } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
  3341. return true;
  3342. }
  3343. return false;
  3344. }
  3345. /**
  3346. * Is the query for an existing author archive page?
  3347. *
  3348. * If the $author parameter is specified, this function will additionally
  3349. * check if the query is for one of the authors specified.
  3350. *
  3351. * @since 3.1.0
  3352. *
  3353. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  3354. * @return bool
  3355. */
  3356. function is_author( $author = '' ) {
  3357. if ( !$this->is_author )
  3358. return false;
  3359. if ( empty($author) )
  3360. return true;
  3361. $author_obj = $this->get_queried_object();
  3362. $author = (array) $author;
  3363. if ( in_array( $author_obj->ID, $author ) )
  3364. return true;
  3365. elseif ( in_array( $author_obj->nickname, $author ) )
  3366. return true;
  3367. elseif ( in_array( $author_obj->user_nicename, $author ) )
  3368. return true;
  3369. return false;
  3370. }
  3371. /**
  3372. * Is the query for an existing category archive page?
  3373. *
  3374. * If the $category parameter is specified, this function will additionally
  3375. * check if the query is for one of the categories specified.
  3376. *
  3377. * @since 3.1.0
  3378. *
  3379. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  3380. * @return bool
  3381. */
  3382. function is_category( $category = '' ) {
  3383. if ( !$this->is_category )
  3384. return false;
  3385. if ( empty($category) )
  3386. return true;
  3387. $cat_obj = $this->get_queried_object();
  3388. $category = (array) $category;
  3389. if ( in_array( $cat_obj->term_id, $category ) )
  3390. return true;
  3391. elseif ( in_array( $cat_obj->name, $category ) )
  3392. return true;
  3393. elseif ( in_array( $cat_obj->slug, $category ) )
  3394. return true;
  3395. return false;
  3396. }
  3397. /**
  3398. * Is the query for an existing tag archive page?
  3399. *
  3400. * If the $tag parameter is specified, this function will additionally
  3401. * check if the query is for one of the tags specified.
  3402. *
  3403. * @since 3.1.0
  3404. *
  3405. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  3406. * @return bool
  3407. */
  3408. function is_tag( $tag = '' ) {
  3409. if ( ! $this->is_tag )
  3410. return false;
  3411. if ( empty( $tag ) )
  3412. return true;
  3413. $tag_obj = $this->get_queried_object();
  3414. $tag = (array) $tag;
  3415. if ( in_array( $tag_obj->term_id, $tag ) )
  3416. return true;
  3417. elseif ( in_array( $tag_obj->name, $tag ) )
  3418. return true;
  3419. elseif ( in_array( $tag_obj->slug, $tag ) )
  3420. return true;
  3421. return false;
  3422. }
  3423. /**
  3424. * Is the query for an existing taxonomy archive page?
  3425. *
  3426. * If the $taxonomy parameter is specified, this function will additionally
  3427. * check if the query is for that specific $taxonomy.
  3428. *
  3429. * If the $term parameter is specified in addition to the $taxonomy parameter,
  3430. * this function will additionally check if the query is for one of the terms
  3431. * specified.
  3432. *
  3433. * @since 3.1.0
  3434. *
  3435. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  3436. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  3437. * @return bool
  3438. */
  3439. function is_tax( $taxonomy = '', $term = '' ) {
  3440. global $wp_taxonomies;
  3441. if ( !$this->is_tax )
  3442. return false;
  3443. if ( empty( $taxonomy ) )
  3444. return true;
  3445. $queried_object = $this->get_queried_object();
  3446. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  3447. $term_array = (array) $term;
  3448. // Check that the taxonomy matches.
  3449. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  3450. return false;
  3451. // Only a Taxonomy provided.
  3452. if ( empty( $term ) )
  3453. return true;
  3454. return isset( $queried_object->term_id ) &&
  3455. count( array_intersect(
  3456. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  3457. $term_array
  3458. ) );
  3459. }
  3460. /**
  3461. * Whether the current URL is within the comments popup window.
  3462. *
  3463. * @since 3.1.0
  3464. *
  3465. * @return bool
  3466. */
  3467. function is_comments_popup() {
  3468. return (bool) $this->is_comments_popup;
  3469. }
  3470. /**
  3471. * Is the query for an existing date archive?
  3472. *
  3473. * @since 3.1.0
  3474. *
  3475. * @return bool
  3476. */
  3477. function is_date() {
  3478. return (bool) $this->is_date;
  3479. }
  3480. /**
  3481. * Is the query for an existing day archive?
  3482. *
  3483. * @since 3.1.0
  3484. *
  3485. * @return bool
  3486. */
  3487. function is_day() {
  3488. return (bool) $this->is_day;
  3489. }
  3490. /**
  3491. * Is the query for a feed?
  3492. *
  3493. * @since 3.1.0
  3494. *
  3495. * @param string|array $feeds Optional feed types to check.
  3496. * @return bool
  3497. */
  3498. function is_feed( $feeds = '' ) {
  3499. if ( empty( $feeds ) || ! $this->is_feed )
  3500. return (bool) $this->is_feed;
  3501. $qv = $this->get( 'feed' );
  3502. if ( 'feed' == $qv )
  3503. $qv = get_default_feed();
  3504. return in_array( $qv, (array) $feeds );
  3505. }
  3506. /**
  3507. * Is the query for a comments feed?
  3508. *
  3509. * @since 3.1.0
  3510. *
  3511. * @return bool
  3512. */
  3513. function is_comment_feed() {
  3514. return (bool) $this->is_comment_feed;
  3515. }
  3516. /**
  3517. * Is the query for the front page of the site?
  3518. *
  3519. * This is for what is displayed at your site's main URL.
  3520. *
  3521. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  3522. *
  3523. * If you set a static page for the front page of your site, this function will return
  3524. * true when viewing that page.
  3525. *
  3526. * Otherwise the same as @see WP_Query::is_home()
  3527. *
  3528. * @since 3.1.0
  3529. * @uses is_home()
  3530. * @uses get_option()
  3531. *
  3532. * @return bool True, if front of site.
  3533. */
  3534. function is_front_page() {
  3535. // most likely case
  3536. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  3537. return true;
  3538. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  3539. return true;
  3540. else
  3541. return false;
  3542. }
  3543. /**
  3544. * Is the query for the blog homepage?
  3545. *
  3546. * This is the page which shows the time based blog content of your site.
  3547. *
  3548. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  3549. *
  3550. * If you set a static page for the front page of your site, this function will return
  3551. * true only on the page you set as the "Posts page".
  3552. *
  3553. * @see WP_Query::is_front_page()
  3554. *
  3555. * @since 3.1.0
  3556. *
  3557. * @return bool True if blog view homepage.
  3558. */
  3559. function is_home() {
  3560. return (bool) $this->is_home;
  3561. }
  3562. /**
  3563. * Is the query for an existing month archive?
  3564. *
  3565. * @since 3.1.0
  3566. *
  3567. * @return bool
  3568. */
  3569. function is_month() {
  3570. return (bool) $this->is_month;
  3571. }
  3572. /**
  3573. * Is the query for an existing single page?
  3574. *
  3575. * If the $page parameter is specified, this function will additionally
  3576. * check if the query is for one of the pages specified.
  3577. *
  3578. * @see WP_Query::is_single()
  3579. * @see WP_Query::is_singular()
  3580. *
  3581. * @since 3.1.0
  3582. *
  3583. * @param mixed $page Page ID, title, slug, or array of such.
  3584. * @return bool
  3585. */
  3586. function is_page( $page = '' ) {
  3587. if ( !$this->is_page )
  3588. return false;
  3589. if ( empty( $page ) )
  3590. return true;
  3591. $page_obj = $this->get_queried_object();
  3592. $page = (array) $page;
  3593. if ( in_array( $page_obj->ID, $page ) )
  3594. return true;
  3595. elseif ( in_array( $page_obj->post_title, $page ) )
  3596. return true;
  3597. else if ( in_array( $page_obj->post_name, $page ) )
  3598. return true;
  3599. return false;
  3600. }
  3601. /**
  3602. * Is the query for paged result and not for the first page?
  3603. *
  3604. * @since 3.1.0
  3605. *
  3606. * @return bool
  3607. */
  3608. function is_paged() {
  3609. return (bool) $this->is_paged;
  3610. }
  3611. /**
  3612. * Is the query for a post or page preview?
  3613. *
  3614. * @since 3.1.0
  3615. *
  3616. * @return bool
  3617. */
  3618. function is_preview() {
  3619. return (bool) $this->is_preview;
  3620. }
  3621. /**
  3622. * Is the query for the robots file?
  3623. *
  3624. * @since 3.1.0
  3625. *
  3626. * @return bool
  3627. */
  3628. function is_robots() {
  3629. return (bool) $this->is_robots;
  3630. }
  3631. /**
  3632. * Is the query for a search?
  3633. *
  3634. * @since 3.1.0
  3635. *
  3636. * @return bool
  3637. */
  3638. function is_search() {
  3639. return (bool) $this->is_search;
  3640. }
  3641. /**
  3642. * Is the query for an existing single post?
  3643. *
  3644. * Works for any post type, except attachments and pages
  3645. *
  3646. * If the $post parameter is specified, this function will additionally
  3647. * check if the query is for one of the Posts specified.
  3648. *
  3649. * @see WP_Query::is_page()
  3650. * @see WP_Query::is_singular()
  3651. *
  3652. * @since 3.1.0
  3653. *
  3654. * @param mixed $post Post ID, title, slug, or array of such.
  3655. * @return bool
  3656. */
  3657. function is_single( $post = '' ) {
  3658. if ( !$this->is_single )
  3659. return false;
  3660. if ( empty($post) )
  3661. return true;
  3662. $post_obj = $this->get_queried_object();
  3663. $post = (array) $post;
  3664. if ( in_array( $post_obj->ID, $post ) )
  3665. return true;
  3666. elseif ( in_array( $post_obj->post_title, $post ) )
  3667. return true;
  3668. elseif ( in_array( $post_obj->post_name, $post ) )
  3669. return true;
  3670. return false;
  3671. }
  3672. /**
  3673. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  3674. *
  3675. * If the $post_types parameter is specified, this function will additionally
  3676. * check if the query is for one of the Posts Types specified.
  3677. *
  3678. * @see WP_Query::is_page()
  3679. * @see WP_Query::is_single()
  3680. *
  3681. * @since 3.1.0
  3682. *
  3683. * @param mixed $post_types Optional. Post Type or array of Post Types
  3684. * @return bool
  3685. */
  3686. function is_singular( $post_types = '' ) {
  3687. if ( empty( $post_types ) || !$this->is_singular )
  3688. return (bool) $this->is_singular;
  3689. $post_obj = $this->get_queried_object();
  3690. return in_array( $post_obj->post_type, (array) $post_types );
  3691. }
  3692. /**
  3693. * Is the query for a specific time?
  3694. *
  3695. * @since 3.1.0
  3696. *
  3697. * @return bool
  3698. */
  3699. function is_time() {
  3700. return (bool) $this->is_time;
  3701. }
  3702. /**
  3703. * Is the query for a trackback endpoint call?
  3704. *
  3705. * @since 3.1.0
  3706. *
  3707. * @return bool
  3708. */
  3709. function is_trackback() {
  3710. return (bool) $this->is_trackback;
  3711. }
  3712. /**
  3713. * Is the query for an existing year archive?
  3714. *
  3715. * @since 3.1.0
  3716. *
  3717. * @return bool
  3718. */
  3719. function is_year() {
  3720. return (bool) $this->is_year;
  3721. }
  3722. /**
  3723. * Is the query a 404 (returns no results)?
  3724. *
  3725. * @since 3.1.0
  3726. *
  3727. * @return bool
  3728. */
  3729. function is_404() {
  3730. return (bool) $this->is_404;
  3731. }
  3732. /**
  3733. * Is the query the main query?
  3734. *
  3735. * @since 3.3.0
  3736. *
  3737. * @return bool
  3738. */
  3739. function is_main_query() {
  3740. global $wp_the_query;
  3741. return $wp_the_query === $this;
  3742. }
  3743. /**
  3744. * After looping through a nested query, this function
  3745. * restores the $post global to the current post in this query.
  3746. *
  3747. * @since 3.7.0
  3748. *
  3749. * @return bool
  3750. */
  3751. function reset_postdata() {
  3752. if ( ! empty( $this->post ) ) {
  3753. $GLOBALS['post'] = $this->post;
  3754. setup_postdata( $this->post );
  3755. }
  3756. }
  3757. }
  3758. /**
  3759. * Redirect old slugs to the correct permalink.
  3760. *
  3761. * Attempts to find the current slug from the past slugs.
  3762. *
  3763. * @since 2.1.0
  3764. * @uses $wp_query
  3765. * @uses $wpdb
  3766. *
  3767. * @return null If no link is found, null is returned.
  3768. */
  3769. function wp_old_slug_redirect() {
  3770. global $wp_query;
  3771. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3772. global $wpdb;
  3773. // Guess the current post_type based on the query vars.
  3774. if ( get_query_var('post_type') )
  3775. $post_type = get_query_var('post_type');
  3776. elseif ( !empty($wp_query->query_vars['pagename']) )
  3777. $post_type = 'page';
  3778. else
  3779. $post_type = 'post';
  3780. if ( is_array( $post_type ) ) {
  3781. if ( count( $post_type ) > 1 )
  3782. return;
  3783. $post_type = array_shift( $post_type );
  3784. }
  3785. // Do not attempt redirect for hierarchical post types
  3786. if ( is_post_type_hierarchical( $post_type ) )
  3787. return;
  3788. $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']);
  3789. // if year, monthnum, or day have been specified, make our query more precise
  3790. // just in case there are multiple identical _wp_old_slug values
  3791. if ( '' != $wp_query->query_vars['year'] )
  3792. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3793. if ( '' != $wp_query->query_vars['monthnum'] )
  3794. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3795. if ( '' != $wp_query->query_vars['day'] )
  3796. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3797. $id = (int) $wpdb->get_var($query);
  3798. if ( ! $id )
  3799. return;
  3800. $link = get_permalink($id);
  3801. if ( !$link )
  3802. return;
  3803. wp_redirect( $link, 301 ); // Permanent redirect
  3804. exit;
  3805. endif;
  3806. }
  3807. /**
  3808. * Set up global post data.
  3809. *
  3810. * @since 1.5.0
  3811. *
  3812. * @param object $post Post data.
  3813. * @uses do_action_ref_array() Calls 'the_post'
  3814. * @return bool True when finished.
  3815. */
  3816. function setup_postdata( $post ) {
  3817. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3818. $id = (int) $post->ID;
  3819. $authordata = get_userdata($post->post_author);
  3820. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3821. $currentmonth = mysql2date('m', $post->post_date, false);
  3822. $numpages = 1;
  3823. $multipage = 0;
  3824. $page = get_query_var('page');
  3825. if ( ! $page )
  3826. $page = 1;
  3827. if ( is_single() || is_page() || is_feed() )
  3828. $more = 1;
  3829. $content = $post->post_content;
  3830. if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
  3831. if ( $page > 1 )
  3832. $more = 1;
  3833. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
  3834. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
  3835. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
  3836. // Ignore nextpage at the beginning of the content.
  3837. if ( 0 === strpos( $content, '<!--nextpage-->' ) )
  3838. $content = substr( $content, 15 );
  3839. $pages = explode('<!--nextpage-->', $content);
  3840. $numpages = count($pages);
  3841. if ( $numpages > 1 )
  3842. $multipage = 1;
  3843. } else {
  3844. $pages = array( $post->post_content );
  3845. }
  3846. /**
  3847. * Fires once the post data has been setup.
  3848. *
  3849. * @since 2.8.0
  3850. *
  3851. * @param WP_Post &$post The Post object (passed by reference).
  3852. */
  3853. do_action_ref_array( 'the_post', array( &$post ) );
  3854. return true;
  3855. }