PageRenderTime 69ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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