PageRenderTime 87ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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