PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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