PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sandbox/wp-includes/query.php

https://bitbucket.org/stephenharris/stephenharris
PHP | 4725 lines | 2329 code | 533 blank | 1863 comment | 594 complexity | 3b8e714e0f01cae90afcc81062acf39c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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 https://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 int|string|array|object $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 string|array $taxonomy Optional. Taxonomy slug or slugs.
  250. * @param int|string|array $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. *
  362. * @return bool True, if front of site.
  363. */
  364. function is_front_page() {
  365. global $wp_query;
  366. if ( ! isset( $wp_query ) ) {
  367. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  368. return false;
  369. }
  370. return $wp_query->is_front_page();
  371. }
  372. /**
  373. * Is the query for the blog homepage?
  374. *
  375. * This is the page which shows the time based blog content of your site.
  376. *
  377. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  378. *
  379. * If you set a static page for the front page of your site, this function will return
  380. * true only on the page you set as the "Posts page".
  381. *
  382. * @see is_front_page()
  383. *
  384. * @see WP_Query::is_home()
  385. * @since 1.5.0
  386. * @uses $wp_query
  387. *
  388. * @return bool True if blog view homepage.
  389. */
  390. function is_home() {
  391. global $wp_query;
  392. if ( ! isset( $wp_query ) ) {
  393. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  394. return false;
  395. }
  396. return $wp_query->is_home();
  397. }
  398. /**
  399. * Is the query for an existing month archive?
  400. *
  401. * @see WP_Query::is_month()
  402. * @since 1.5.0
  403. * @uses $wp_query
  404. *
  405. * @return bool
  406. */
  407. function is_month() {
  408. global $wp_query;
  409. if ( ! isset( $wp_query ) ) {
  410. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  411. return false;
  412. }
  413. return $wp_query->is_month();
  414. }
  415. /**
  416. * Is the query for an existing single page?
  417. *
  418. * If the $page parameter is specified, this function will additionally
  419. * check if the query is for one of the pages specified.
  420. *
  421. * @see is_single()
  422. * @see is_singular()
  423. *
  424. * @see WP_Query::is_page()
  425. * @since 1.5.0
  426. * @uses $wp_query
  427. *
  428. * @param mixed $page Page ID, title, slug, or array of such.
  429. * @return bool
  430. */
  431. function is_page( $page = '' ) {
  432. global $wp_query;
  433. if ( ! isset( $wp_query ) ) {
  434. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  435. return false;
  436. }
  437. return $wp_query->is_page( $page );
  438. }
  439. /**
  440. * Is the query for paged result and not for the first page?
  441. *
  442. * @see WP_Query::is_paged()
  443. * @since 1.5.0
  444. * @uses $wp_query
  445. *
  446. * @return bool
  447. */
  448. function is_paged() {
  449. global $wp_query;
  450. if ( ! isset( $wp_query ) ) {
  451. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  452. return false;
  453. }
  454. return $wp_query->is_paged();
  455. }
  456. /**
  457. * Is the query for a post or page preview?
  458. *
  459. * @see WP_Query::is_preview()
  460. * @since 2.0.0
  461. * @uses $wp_query
  462. *
  463. * @return bool
  464. */
  465. function is_preview() {
  466. global $wp_query;
  467. if ( ! isset( $wp_query ) ) {
  468. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  469. return false;
  470. }
  471. return $wp_query->is_preview();
  472. }
  473. /**
  474. * Is the query for the robots file?
  475. *
  476. * @see WP_Query::is_robots()
  477. * @since 2.1.0
  478. * @uses $wp_query
  479. *
  480. * @return bool
  481. */
  482. function is_robots() {
  483. global $wp_query;
  484. if ( ! isset( $wp_query ) ) {
  485. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  486. return false;
  487. }
  488. return $wp_query->is_robots();
  489. }
  490. /**
  491. * Is the query for a search?
  492. *
  493. * @see WP_Query::is_search()
  494. * @since 1.5.0
  495. * @uses $wp_query
  496. *
  497. * @return bool
  498. */
  499. function is_search() {
  500. global $wp_query;
  501. if ( ! isset( $wp_query ) ) {
  502. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  503. return false;
  504. }
  505. return $wp_query->is_search();
  506. }
  507. /**
  508. * Is the query for an existing single post?
  509. *
  510. * Works for any post type, except attachments and pages
  511. *
  512. * If the $post parameter is specified, this function will additionally
  513. * check if the query is for one of the Posts specified.
  514. *
  515. * @see is_page()
  516. * @see is_singular()
  517. *
  518. * @see WP_Query::is_single()
  519. * @since 1.5.0
  520. * @uses $wp_query
  521. *
  522. * @param mixed $post Post ID, title, slug, or array of such.
  523. * @return bool
  524. */
  525. function is_single( $post = '' ) {
  526. global $wp_query;
  527. if ( ! isset( $wp_query ) ) {
  528. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  529. return false;
  530. }
  531. return $wp_query->is_single( $post );
  532. }
  533. /**
  534. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  535. *
  536. * If the $post_types parameter is specified, this function will additionally
  537. * check if the query is for one of the Posts Types specified.
  538. *
  539. * @see is_page()
  540. * @see is_single()
  541. *
  542. * @see WP_Query::is_singular()
  543. * @since 1.5.0
  544. * @uses $wp_query
  545. *
  546. * @param mixed $post_types Optional. Post Type or array of Post Types
  547. * @return bool
  548. */
  549. function is_singular( $post_types = '' ) {
  550. global $wp_query;
  551. if ( ! isset( $wp_query ) ) {
  552. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  553. return false;
  554. }
  555. return $wp_query->is_singular( $post_types );
  556. }
  557. /**
  558. * Is the query for a specific time?
  559. *
  560. * @see WP_Query::is_time()
  561. * @since 1.5.0
  562. * @uses $wp_query
  563. *
  564. * @return bool
  565. */
  566. function is_time() {
  567. global $wp_query;
  568. if ( ! isset( $wp_query ) ) {
  569. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  570. return false;
  571. }
  572. return $wp_query->is_time();
  573. }
  574. /**
  575. * Is the query for a trackback endpoint call?
  576. *
  577. * @see WP_Query::is_trackback()
  578. * @since 1.5.0
  579. * @uses $wp_query
  580. *
  581. * @return bool
  582. */
  583. function is_trackback() {
  584. global $wp_query;
  585. if ( ! isset( $wp_query ) ) {
  586. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  587. return false;
  588. }
  589. return $wp_query->is_trackback();
  590. }
  591. /**
  592. * Is the query for an existing year archive?
  593. *
  594. * @see WP_Query::is_year()
  595. * @since 1.5.0
  596. * @uses $wp_query
  597. *
  598. * @return bool
  599. */
  600. function is_year() {
  601. global $wp_query;
  602. if ( ! isset( $wp_query ) ) {
  603. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  604. return false;
  605. }
  606. return $wp_query->is_year();
  607. }
  608. /**
  609. * Is the query a 404 (returns no results)?
  610. *
  611. * @see WP_Query::is_404()
  612. * @since 1.5.0
  613. * @uses $wp_query
  614. *
  615. * @return bool
  616. */
  617. function is_404() {
  618. global $wp_query;
  619. if ( ! isset( $wp_query ) ) {
  620. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  621. return false;
  622. }
  623. return $wp_query->is_404();
  624. }
  625. /**
  626. * Is the query the main query?
  627. *
  628. * @since 3.3.0
  629. *
  630. * @return bool
  631. */
  632. function is_main_query() {
  633. if ( 'pre_get_posts' === current_filter() ) {
  634. $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
  635. 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'https://codex.wordpress.org/Function_Reference/is_main_query' ) );
  636. _doing_it_wrong( __FUNCTION__, $message, '3.7' );
  637. }
  638. global $wp_query;
  639. return $wp_query->is_main_query();
  640. }
  641. /*
  642. * The Loop. Post loop control.
  643. */
  644. /**
  645. * Whether current WordPress query has results to loop over.
  646. *
  647. * @see WP_Query::have_posts()
  648. * @since 1.5.0
  649. * @uses $wp_query
  650. *
  651. * @return bool
  652. */
  653. function have_posts() {
  654. global $wp_query;
  655. return $wp_query->have_posts();
  656. }
  657. /**
  658. * Whether the caller is in the Loop.
  659. *
  660. * @since 2.0.0
  661. * @uses $wp_query
  662. *
  663. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  664. */
  665. function in_the_loop() {
  666. global $wp_query;
  667. return $wp_query->in_the_loop;
  668. }
  669. /**
  670. * Rewind the loop posts.
  671. *
  672. * @see WP_Query::rewind_posts()
  673. * @since 1.5.0
  674. * @uses $wp_query
  675. *
  676. * @return null
  677. */
  678. function rewind_posts() {
  679. global $wp_query;
  680. return $wp_query->rewind_posts();
  681. }
  682. /**
  683. * Iterate the post index in the loop.
  684. *
  685. * @see WP_Query::the_post()
  686. * @since 1.5.0
  687. * @uses $wp_query
  688. */
  689. function the_post() {
  690. global $wp_query;
  691. $wp_query->the_post();
  692. }
  693. /*
  694. * Comments loop.
  695. */
  696. /**
  697. * Whether there are comments to loop over.
  698. *
  699. * @see WP_Query::have_comments()
  700. * @since 2.2.0
  701. * @uses $wp_query
  702. *
  703. * @return bool
  704. */
  705. function have_comments() {
  706. global $wp_query;
  707. return $wp_query->have_comments();
  708. }
  709. /**
  710. * Iterate comment index in the comment loop.
  711. *
  712. * @see WP_Query::the_comment()
  713. * @since 2.2.0
  714. * @uses $wp_query
  715. *
  716. * @return object
  717. */
  718. function the_comment() {
  719. global $wp_query;
  720. return $wp_query->the_comment();
  721. }
  722. /*
  723. * WP_Query
  724. */
  725. /**
  726. * The WordPress Query class.
  727. *
  728. * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  729. *
  730. * @since 1.5.0
  731. */
  732. class WP_Query {
  733. /**
  734. * Query vars set by the user
  735. *
  736. * @since 1.5.0
  737. * @access public
  738. * @var array
  739. */
  740. public $query;
  741. /**
  742. * Query vars, after parsing
  743. *
  744. * @since 1.5.0
  745. * @access public
  746. * @var array
  747. */
  748. public $query_vars = array();
  749. /**
  750. * Taxonomy query, as passed to get_tax_sql()
  751. *
  752. * @since 3.1.0
  753. * @access public
  754. * @var object WP_Tax_Query
  755. */
  756. public $tax_query;
  757. /**
  758. * Metadata query container
  759. *
  760. * @since 3.2.0
  761. * @access public
  762. * @var object WP_Meta_Query
  763. */
  764. public $meta_query = false;
  765. /**
  766. * Date query container
  767. *
  768. * @since 3.7.0
  769. * @access public
  770. * @var object WP_Date_Query
  771. */
  772. public $date_query = false;
  773. /**
  774. * Holds the data for a single object that is queried.
  775. *
  776. * Holds the contents of a post, page, category, attachment.
  777. *
  778. * @since 1.5.0
  779. * @access public
  780. * @var object|array
  781. */
  782. public $queried_object;
  783. /**
  784. * The ID of the queried object.
  785. *
  786. * @since 1.5.0
  787. * @access public
  788. * @var int
  789. */
  790. public $queried_object_id;
  791. /**
  792. * Get post database query.
  793. *
  794. * @since 2.0.1
  795. * @access public
  796. * @var string
  797. */
  798. public $request;
  799. /**
  800. * List of posts.
  801. *
  802. * @since 1.5.0
  803. * @access public
  804. * @var array
  805. */
  806. public $posts;
  807. /**
  808. * The amount of posts for the current query.
  809. *
  810. * @since 1.5.0
  811. * @access public
  812. * @var int
  813. */
  814. public $post_count = 0;
  815. /**
  816. * Index of the current item in the loop.
  817. *
  818. * @since 1.5.0
  819. * @access public
  820. * @var int
  821. */
  822. public $current_post = -1;
  823. /**
  824. * Whether the loop has started and the caller is in the loop.
  825. *
  826. * @since 2.0.0
  827. * @access public
  828. * @var bool
  829. */
  830. public $in_the_loop = false;
  831. /**
  832. * The current post.
  833. *
  834. * @since 1.5.0
  835. * @access public
  836. * @var WP_Post
  837. */
  838. public $post;
  839. /**
  840. * The list of comments for current post.
  841. *
  842. * @since 2.2.0
  843. * @access public
  844. * @var array
  845. */
  846. public $comments;
  847. /**
  848. * The amount of comments for the posts.
  849. *
  850. * @since 2.2.0
  851. * @access public
  852. * @var int
  853. */
  854. public $comment_count = 0;
  855. /**
  856. * The index of the comment in the comment loop.
  857. *
  858. * @since 2.2.0
  859. * @access public
  860. * @var int
  861. */
  862. public $current_comment = -1;
  863. /**
  864. * Current comment ID.
  865. *
  866. * @since 2.2.0
  867. * @access public
  868. * @var int
  869. */
  870. public $comment;
  871. /**
  872. * The amount of found posts for the current query.
  873. *
  874. * If limit clause was not used, equals $post_count.
  875. *
  876. * @since 2.1.0
  877. * @access public
  878. * @var int
  879. */
  880. public $found_posts = 0;
  881. /**
  882. * The amount of pages.
  883. *
  884. * @since 2.1.0
  885. * @access public
  886. * @var int
  887. */
  888. public $max_num_pages = 0;
  889. /**
  890. * The amount of comment pages.
  891. *
  892. * @since 2.7.0
  893. * @access public
  894. * @var int
  895. */
  896. public $max_num_comment_pages = 0;
  897. /**
  898. * Set if query is single post.
  899. *
  900. * @since 1.5.0
  901. * @access public
  902. * @var bool
  903. */
  904. public $is_single = false;
  905. /**
  906. * Set if query is preview of blog.
  907. *
  908. * @since 2.0.0
  909. * @access public
  910. * @var bool
  911. */
  912. public $is_preview = false;
  913. /**
  914. * Set if query returns a page.
  915. *
  916. * @since 1.5.0
  917. * @access public
  918. * @var bool
  919. */
  920. public $is_page = false;
  921. /**
  922. * Set if query is an archive list.
  923. *
  924. * @since 1.5.0
  925. * @access public
  926. * @var bool
  927. */
  928. public $is_archive = false;
  929. /**
  930. * Set if query is part of a date.
  931. *
  932. * @since 1.5.0
  933. * @access public
  934. * @var bool
  935. */
  936. public $is_date = false;
  937. /**
  938. * Set if query contains a year.
  939. *
  940. * @since 1.5.0
  941. * @access public
  942. * @var bool
  943. */
  944. public $is_year = false;
  945. /**
  946. * Set if query contains a month.
  947. *
  948. * @since 1.5.0
  949. * @access public
  950. * @var bool
  951. */
  952. public $is_month = false;
  953. /**
  954. * Set if query contains a day.
  955. *
  956. * @since 1.5.0
  957. * @access public
  958. * @var bool
  959. */
  960. public $is_day = false;
  961. /**
  962. * Set if query contains time.
  963. *
  964. * @since 1.5.0
  965. * @access public
  966. * @var bool
  967. */
  968. public $is_time = false;
  969. /**
  970. * Set if query contains an author.
  971. *
  972. * @since 1.5.0
  973. * @access public
  974. * @var bool
  975. */
  976. public $is_author = false;
  977. /**
  978. * Set if query contains category.
  979. *
  980. * @since 1.5.0
  981. * @access public
  982. * @var bool
  983. */
  984. public $is_category = false;
  985. /**
  986. * Set if query contains tag.
  987. *
  988. * @since 2.3.0
  989. * @access public
  990. * @var bool
  991. */
  992. public $is_tag = false;
  993. /**
  994. * Set if query contains taxonomy.
  995. *
  996. * @since 2.5.0
  997. * @access public
  998. * @var bool
  999. */
  1000. public $is_tax = false;
  1001. /**
  1002. * Set if query was part of a search result.
  1003. *
  1004. * @since 1.5.0
  1005. * @access public
  1006. * @var bool
  1007. */
  1008. public $is_search = false;
  1009. /**
  1010. * Set if query is feed display.
  1011. *
  1012. * @since 1.5.0
  1013. * @access public
  1014. * @var bool
  1015. */
  1016. public $is_feed = false;
  1017. /**
  1018. * Set if query is comment feed display.
  1019. *
  1020. * @since 2.2.0
  1021. * @access public
  1022. * @var bool
  1023. */
  1024. public $is_comment_feed = false;
  1025. /**
  1026. * Set if query is trackback.
  1027. *
  1028. * @since 1.5.0
  1029. * @access public
  1030. * @var bool
  1031. */
  1032. public $is_trackback = false;
  1033. /**
  1034. * Set if query is blog homepage.
  1035. *
  1036. * @since 1.5.0
  1037. * @access public
  1038. * @var bool
  1039. */
  1040. public $is_home = false;
  1041. /**
  1042. * Set if query couldn't found anything.
  1043. *
  1044. * @since 1.5.0
  1045. * @access public
  1046. * @var bool
  1047. */
  1048. public $is_404 = false;
  1049. /**
  1050. * Set if query is within comments popup window.
  1051. *
  1052. * @since 1.5.0
  1053. * @access public
  1054. * @var bool
  1055. */
  1056. public $is_comments_popup = false;
  1057. /**
  1058. * Set if query is paged
  1059. *
  1060. * @since 1.5.0
  1061. * @access public
  1062. * @var bool
  1063. */
  1064. public $is_paged = false;
  1065. /**
  1066. * Set if query is part of administration page.
  1067. *
  1068. * @since 1.5.0
  1069. * @access public
  1070. * @var bool
  1071. */
  1072. public $is_admin = false;
  1073. /**
  1074. * Set if query is an attachment.
  1075. *
  1076. * @since 2.0.0
  1077. * @access public
  1078. * @var bool
  1079. */
  1080. public $is_attachment = false;
  1081. /**
  1082. * Set if is single, is a page, or is an attachment.
  1083. *
  1084. * @since 2.1.0
  1085. * @access public
  1086. * @var bool
  1087. */
  1088. public $is_singular = false;
  1089. /**
  1090. * Set if query is for robots.
  1091. *
  1092. * @since 2.1.0
  1093. * @access public
  1094. * @var bool
  1095. */
  1096. public $is_robots = false;
  1097. /**
  1098. * Set if query contains posts.
  1099. *
  1100. * Basically, the homepage if the option isn't set for the static homepage.
  1101. *
  1102. * @since 2.1.0
  1103. * @access public
  1104. * @var bool
  1105. */
  1106. public $is_posts_page = false;
  1107. /**
  1108. * Set if query is for a post type archive.
  1109. *
  1110. * @since 3.1.0
  1111. * @access public
  1112. * @var bool
  1113. */
  1114. public $is_post_type_archive = false;
  1115. /**
  1116. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1117. * whether we have to re-parse because something has changed
  1118. *
  1119. * @since 3.1.0
  1120. * @access private
  1121. * @var bool|string
  1122. */
  1123. private $query_vars_hash = false;
  1124. /**
  1125. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1126. * via pre_get_posts hooks.
  1127. *
  1128. * @since 3.1.1
  1129. * @access private
  1130. */
  1131. private $query_vars_changed = true;
  1132. /**
  1133. * Set if post thumbnails are cached
  1134. *
  1135. * @since 3.2.0
  1136. * @access public
  1137. * @var bool
  1138. */
  1139. public $thumbnails_cached = false;
  1140. /**
  1141. * Cached list of search stopwords.
  1142. *
  1143. * @since 3.7.0
  1144. * @var array
  1145. */
  1146. private $stopwords;
  1147. private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
  1148. private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
  1149. /**
  1150. * Resets query flags to false.
  1151. *
  1152. * The query flags are what page info WordPress was able to figure out.
  1153. *
  1154. * @since 2.0.0
  1155. * @access private
  1156. */
  1157. private function init_query_flags() {
  1158. $this->is_single = false;
  1159. $this->is_preview = false;
  1160. $this->is_page = false;
  1161. $this->is_archive = false;
  1162. $this->is_date = false;
  1163. $this->is_year = false;
  1164. $this->is_month = false;
  1165. $this->is_day = false;
  1166. $this->is_time = false;
  1167. $this->is_author = false;
  1168. $this->is_category = false;
  1169. $this->is_tag = false;
  1170. $this->is_tax = false;
  1171. $this->is_search = false;
  1172. $this->is_feed = false;
  1173. $this->is_comment_feed = false;
  1174. $this->is_trackback = false;
  1175. $this->is_home = false;
  1176. $this->is_404 = false;
  1177. $this->is_comments_popup = false;
  1178. $this->is_paged = false;
  1179. $this->is_admin = false;
  1180. $this->is_attachment = false;
  1181. $this->is_singular = false;
  1182. $this->is_robots = false;
  1183. $this->is_posts_page = false;
  1184. $this->is_post_type_archive = false;
  1185. }
  1186. /**
  1187. * Initiates object properties and sets default values.
  1188. *
  1189. * @since 1.5.0
  1190. * @access public
  1191. */
  1192. public function init() {
  1193. unset($this->posts);
  1194. unset($this->query);
  1195. $this->query_vars = array();
  1196. unset($this->queried_object);
  1197. unset($this->queried_object_id);
  1198. $this->post_count = 0;
  1199. $this->current_post = -1;
  1200. $this->in_the_loop = false;
  1201. unset( $this->request );
  1202. unset( $this->post );
  1203. unset( $this->comments );
  1204. unset( $this->comment );
  1205. $this->comment_count = 0;
  1206. $this->current_comment = -1;
  1207. $this->found_posts = 0;
  1208. $this->max_num_pages = 0;
  1209. $this->max_num_comment_pages = 0;
  1210. $this->init_query_flags();
  1211. }
  1212. /**
  1213. * Reparse the query vars.
  1214. *
  1215. * @since 1.5.0
  1216. * @access public
  1217. */
  1218. public function parse_query_vars() {
  1219. $this->parse_query();
  1220. }
  1221. /**
  1222. * Fills in the query variables, which do not exist within the parameter.
  1223. *
  1224. * @since 2.1.0
  1225. * @access public
  1226. *
  1227. * @param array $array Defined query variables.
  1228. * @return array Complete query variables with undefined ones filled in empty.
  1229. */
  1230. public function fill_query_vars($array) {
  1231. $keys = array(
  1232. 'error'
  1233. , 'm'
  1234. , 'p'
  1235. , 'post_parent'
  1236. , 'subpost'
  1237. , 'subpost_id'
  1238. , 'attachment'
  1239. , 'attachment_id'
  1240. , 'name'
  1241. , 'static'
  1242. , 'pagename'
  1243. , 'page_id'
  1244. , 'second'
  1245. , 'minute'
  1246. , 'hour'
  1247. , 'day'
  1248. , 'monthnum'
  1249. , 'year'
  1250. , 'w'
  1251. , 'category_name'
  1252. , 'tag'
  1253. , 'cat'
  1254. , 'tag_id'
  1255. , 'author'
  1256. , 'author_name'
  1257. , 'feed'
  1258. , 'tb'
  1259. , 'paged'
  1260. , 'comments_popup'
  1261. , 'meta_key'
  1262. , 'meta_value'
  1263. , 'preview'
  1264. , 's'
  1265. , 'sentence'
  1266. , 'fields'
  1267. , 'menu_order'
  1268. );
  1269. foreach ( $keys as $key ) {
  1270. if ( !isset($array[$key]) )
  1271. $array[$key] = '';
  1272. }
  1273. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1274. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  1275. 'author__in', 'author__not_in' );
  1276. foreach ( $array_keys as $key ) {
  1277. if ( !isset($array[$key]) )
  1278. $array[$key] = array();
  1279. }
  1280. return $array;
  1281. }
  1282. /**
  1283. * Parse a query string and set query type booleans.
  1284. *
  1285. * @since 1.5.0
  1286. * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
  1287. * array key to `$orderby`.
  1288. * @access public
  1289. *
  1290. * @param string|array $query {
  1291. * Optional. Array or string of Query parameters.
  1292. *
  1293. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  1294. * @type int|string $author Author ID, or comma-separated list of IDs.
  1295. * @type string $author_name User 'user_nicename'.
  1296. * @type array $author__in An array of author IDs to query from.
  1297. * @type array $author__not_in An array of author IDs not to query from.
  1298. * @type bool $cache_results Whether to cache post information. Default true.
  1299. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  1300. * @type array $category__and An array of category IDs (AND in).
  1301. * @type array $category__in An array of category IDs (OR in, no children).
  1302. * @type array $category__not_in An array of category IDs (NOT in).
  1303. * @type string $category_name Use category slug (not name, this or any children).
  1304. * @type int $comments_per_page The number of comments to return per page.
  1305. * Default 'comments_per_page' option.
  1306. * @type int|string $comments_popup Whether the query is within the comments popup. Default empty.
  1307. * @type array $date_query An associative array of WP_Date_Query arguments.
  1308. * {@see WP_Date_Query::__construct()}
  1309. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  1310. * @type bool $exact Whether to search by exact keyword. Default false.
  1311. * @type string|array $fields Which fields to return. Single field or all fields (string),
  1312. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  1313. * Default all fields. Accepts 'ids', 'id=>parent'.
  1314. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  1315. * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  1316. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  1317. * Default 0|false.
  1318. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  1319. * numbers 1-12. Default empty.
  1320. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  1321. * @type string $meta_key Custom field key.
  1322. * @type array $meta_query An associative array of WP_Meta_Query arguments.
  1323. * {@see WP_Meta_Query->queries}
  1324. * @type string $meta_value Custom field value.
  1325. * @type int $meta_value_num Custom field value number.
  1326. * @type int $menu_order The menu order of the posts.
  1327. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  1328. * @type string $name Post slug.
  1329. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  1330. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  1331. * performance. Default false.
  1332. * @type int $offset The number of posts to offset before retrieval.
  1333. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  1334. * Accepts 'ASC', 'DESC'.
  1335. * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
  1336. * passed. To use 'meta_value', or 'meta_value_num',
  1337. * 'meta_key=keyname' must be also be defined. To sort by a
  1338. * specific `$meta_query` clause, use that clause's array key.
  1339. * Default 'date'. Accepts 'none', 'name', 'author', 'date',
  1340. * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
  1341. * 'comment_count', 'meta_value', 'meta_value_num', and the
  1342. * array keys of `$meta_query`.
  1343. * @type int $p Post ID.
  1344. * @type int $page Show the number of posts that would show up on page X of a
  1345. * static front page.
  1346. * @type int $paged The number of the current page.
  1347. * @type int $page_id Page ID.
  1348. * @type string $pagename Page slug.
  1349. * @type string $perm Show posts if user has the appropriate capability.
  1350. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  1351. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  1352. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  1353. * separated IDs will NOT work.
  1354. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  1355. * top-level pages.
  1356. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  1357. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  1358. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  1359. * Default 'any' if using 'tax_query'.
  1360. * @type string|array $post_status A post status (string) or array of post statuses.
  1361. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  1362. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  1363. * 'posts_per_page' when is_archive(), or is_search() are true.
  1364. * @type string $s Search keyword.
  1365. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  1366. * @type array $search_terms Array of search terms.
  1367. * @type bool $sentence Whether to search by phrase. Default false.
  1368. * @type bool $suppress_filters Whether to suppress filters. Default false.
  1369. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  1370. * @type array $tag__and An array of tag ids (AND in).
  1371. * @type array $tag__in An array of tag ids (OR in).
  1372. * @type array $tag__not_in An array of tag ids (NOT in).
  1373. * @type int $tag_id Tag id or comma-separated list of IDs.
  1374. * @type array $tag_slug__and An array of tag slugs (AND in).
  1375. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  1376. * true. Note: a string of comma-separated IDs will NOT work.
  1377. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  1378. * {@see WP_Tax_Query->queries}
  1379. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  1380. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  1381. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  1382. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  1383. * }
  1384. */
  1385. public function parse_query( $query = '' ) {
  1386. if ( ! empty( $query ) ) {
  1387. $this->init();
  1388. $this->query = $this->query_vars = wp_parse_args( $query );
  1389. } elseif ( ! isset( $this->query ) ) {
  1390. $this->query = $this->query_vars;
  1391. }
  1392. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1393. $qv = &$this->query_vars;
  1394. $this->query_vars_changed = true;
  1395. if ( ! empty($qv['robots']) )
  1396. $this->is_robots = true;
  1397. $qv['p'] = absint($qv['p']);
  1398. $qv['page_id'] = absint($qv['page_id']);
  1399. $qv['year'] = absint($qv['year']);
  1400. $qv['monthnum'] = absint($qv['monthnum']);
  1401. $qv['day'] = absint($qv['day']);
  1402. $qv['w'] = absint($qv['w']);
  1403. $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
  1404. $qv['paged'] = absint($qv['paged']);
  1405. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1406. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  1407. $qv['pagename'] = trim( $qv['pagename'] );
  1408. $qv['name'] = trim( $qv['name'] );
  1409. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1410. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1411. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1412. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1413. // Fairly insane upper bound for search string lengths.
  1414. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  1415. $qv['s'] = '';
  1416. }
  1417. // Compat. Map subpost to attachment.
  1418. if ( '' != $qv['subpost'] )
  1419. $qv['attachment'] = $qv['subpost'];
  1420. if ( '' != $qv['subpost_id'] )
  1421. $qv['attachment_id'] = $qv['subpost_id'];
  1422. $qv['attachment_id'] = absint($qv['attachment_id']);
  1423. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1424. $this->is_single = true;
  1425. $this->is_attachment = true;
  1426. } elseif ( '' != $qv['name'] ) {
  1427. $this->is_single = true;
  1428. } elseif ( $qv['p'] ) {
  1429. $this->is_single = true;
  1430. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1431. // If year, month, day, hour, minute, and second are set, a single
  1432. // post is being queried.
  1433. $this->is_single = true;
  1434. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1435. $this->is_page = true;
  1436. $this->is_single = false;
  1437. } else {
  1438. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1439. if ( isset( $this->query['s'] ) ) {
  1440. $this->is_search = true;
  1441. }
  1442. if ( '' !== $qv['second'] ) {
  1443. $this->is_time = true;
  1444. $this->is_date = true;
  1445. }
  1446. if ( '' !== $qv['minute'] ) {
  1447. $this->is_time = true;
  1448. $this->is_date = true;
  1449. }
  1450. if ( '' !== $qv['hour'] ) {
  1451. $this->is_time = true;
  1452. $this->is_date = true;
  1453. }
  1454. if ( $qv['day'] ) {
  1455. if ( ! $this->is_date ) {
  1456. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  1457. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  1458. $qv['error'] = '404';
  1459. } else {
  1460. $this->is_day = true;
  1461. $this->is_date = true;
  1462. }
  1463. }
  1464. }
  1465. if ( $qv['monthnum'] ) {
  1466. if ( ! $this->is_date ) {
  1467. if ( 12 < $qv['monthnum'] ) {
  1468. $qv['error'] = '404';
  1469. } else {
  1470. $this->is_month = true;
  1471. $this->is_date = true;
  1472. }
  1473. }
  1474. }
  1475. if ( $qv['year'] ) {
  1476. if ( ! $this->is_date ) {
  1477. $this->is_year = true;
  1478. $this->is_date = true;
  1479. }
  1480. }
  1481. if ( $qv['m'] ) {
  1482. $this->is_date = true;
  1483. if ( strlen($qv['m']) > 9 ) {
  1484. $this->is_time = true;
  1485. } elseif ( strlen( $qv['m'] ) > 7 ) {
  1486. $this->is_day = true;
  1487. } elseif ( strlen( $qv['m'] ) > 5 ) {
  1488. $this->is_month = true;
  1489. } else {
  1490. $this->is_year = true;
  1491. }
  1492. }
  1493. if ( '' != $qv['w'] ) {
  1494. $this->is_date = true;
  1495. }
  1496. $this->query_vars_hash = false;
  1497. $this->parse_tax_query( $qv );
  1498. foreach ( $this->tax_query->queries as $tax_query ) {
  1499. if ( ! is_array( $tax_query ) ) {
  1500. continue;
  1501. }
  1502. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  1503. switch ( $tax_query['taxonomy'] ) {
  1504. case 'category':
  1505. $this->is_category = true;
  1506. break;
  1507. case 'post_tag':
  1508. $this->is_tag = true;
  1509. break;
  1510. default:
  1511. $this->is_tax = true;
  1512. }
  1513. }
  1514. }
  1515. unset( $tax_query );
  1516. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1517. $this->is_author = false;
  1518. } else {
  1519. $this->is_author = true;
  1520. }
  1521. if ( '' != $qv['author_name'] )
  1522. $this->is_author = true;
  1523. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1524. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1525. if ( ! empty( $post_type_obj->has_archive ) )
  1526. $this->is_post_type_archive = true;
  1527. }
  1528. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1529. $this->is_archive = true;
  1530. }
  1531. if ( '' != $qv['feed'] )
  1532. $this->is_feed = true;
  1533. if ( '' != $qv['tb'] )
  1534. $this->is_trackback = true;
  1535. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1536. $this->is_paged = true;
  1537. if ( '' != $qv['comments_popup'] )
  1538. $this->is_comments_popup = true;
  1539. // if we're previewing inside the write screen
  1540. if ( '' != $qv['preview'] )
  1541. $this->is_preview = true;
  1542. if ( is_admin() )
  1543. $this->is_admin = true;
  1544. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1545. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1546. $qv['withcomments'] = 1;
  1547. }
  1548. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1549. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1550. $this->is_comment_feed = true;
  1551. 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 ) )
  1552. $this->is_home = true;
  1553. // Correct is_* for page_on_front and page_for_posts
  1554. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1555. $_query = wp_parse_args($this->query);
  1556. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1557. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1558. unset($_query['pagename']);
  1559. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1560. $this->is_page = true;
  1561. $this->is_home = false;
  1562. $qv['page_id'] = get_option('page_on_front');
  1563. // Correct <!--nextpage--> for page_on_front
  1564. if ( !empty($qv['paged']) ) {
  1565. $qv['page'] = $qv['paged'];
  1566. unset($qv['paged']);
  1567. }
  1568. }
  1569. }
  1570. if ( '' != $qv['pagename'] ) {
  1571. $this->queried_object = get_page_by_path($qv['pagename']);
  1572. if ( !empty($this->queried_object) )
  1573. $this->queried_object_id = (int) $this->queried_object->ID;
  1574. else
  1575. unset($this->queried_object);
  1576. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1577. $this->is_page = false;
  1578. $this->is_home = true;
  1579. $this->is_posts_page = true;
  1580. }
  1581. }
  1582. if ( $qv['page_id'] ) {
  1583. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1584. $this->is_page = false;
  1585. $this->is_home = true;
  1586. $this->is_posts_page = true;
  1587. }
  1588. }
  1589. if ( !empty($qv['post_type']) ) {
  1590. if ( is_array($qv['post_type']) )
  1591. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1592. else
  1593. $qv['post_type'] = sanitize_key($qv['post_type']);
  1594. }
  1595. if ( ! empty( $qv['post_status'] ) ) {
  1596. if ( is_array( $qv['post_status'] ) )
  1597. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1598. else
  1599. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1600. }
  1601. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1602. $this->is_comment_feed = false;
  1603. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1604. // Done correcting is_* for page_on_front and page_for_posts
  1605. if ( '404' == $qv['error'] )
  1606. $this->set_404();
  1607. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1608. $this->query_vars_changed = false;
  1609. /**
  1610. * Fires after the main query vars have been parsed.
  1611. *
  1612. * @since 1.5.0
  1613. *
  1614. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1615. */
  1616. do_action_ref_array( 'parse_query', array( &$this ) );
  1617. }
  1618. /**
  1619. * Parses various taxonomy related query vars.
  1620. *
  1621. * For BC, this method is not marked as protected. See [28987].
  1622. *
  1623. * @access protected
  1624. * @since 3.1.0
  1625. *
  1626. * @param array &$q The query variables
  1627. */
  1628. public function parse_tax_query( &$q ) {
  1629. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1630. $tax_query = $q['tax_query'];
  1631. } else {
  1632. $tax_query = array();
  1633. }
  1634. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1635. $tax_query[] = array(
  1636. 'taxonomy' => $q['taxonomy'],
  1637. 'terms' => array( $q['term'] ),
  1638. 'field' => 'slug',
  1639. );
  1640. }
  1641. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  1642. if ( 'post_tag' == $taxonomy )
  1643. continue; // Handled further down in the $q['tag'] block
  1644. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1645. $tax_query_defaults = array(
  1646. 'taxonomy' => $taxonomy,
  1647. 'field' => 'slug',
  1648. );
  1649. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1650. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1651. }
  1652. $term = $q[$t->query_var];
  1653. if ( strpos($term, '+') !== false ) {
  1654. $terms = preg_split( '/[+]+/', $term );
  1655. foreach ( $terms as $term ) {
  1656. $tax_query[] = array_merge( $tax_query_defaults, array(
  1657. 'terms' => array( $term )
  1658. ) );
  1659. }
  1660. } else {
  1661. $tax_query[] = array_merge( $tax_query_defaults, array(
  1662. 'terms' => preg_split( '/[,]+/', $term )
  1663. ) );
  1664. }
  1665. }
  1666. }
  1667. // Category stuff
  1668. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
  1669. $cat_in = $cat_not_in = array();
  1670. $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
  1671. $cat_array = array_map( 'intval', $cat_array );
  1672. $q['cat'] = implode( ',', $cat_array );
  1673. foreach ( $cat_array as $cat ) {
  1674. if ( $cat > 0 )
  1675. $cat_in[] = $cat;
  1676. elseif ( $cat < 0 )
  1677. $cat_not_in[] = abs( $cat );
  1678. }
  1679. if ( ! empty( $cat_in ) ) {
  1680. $tax_query[] = array(
  1681. 'taxonomy' => 'category',
  1682. 'terms' => $cat_in,
  1683. 'field' => 'term_id',
  1684. 'include_children' => true
  1685. );
  1686. }
  1687. if ( ! empty( $cat_not_in ) ) {
  1688. $tax_query[] = array(
  1689. 'taxonomy' => 'category',
  1690. 'terms' => $cat_not_in,
  1691. 'field' => 'term_id',
  1692. 'operator' => 'NOT IN',
  1693. 'include_children' => true
  1694. );
  1695. }
  1696. unset( $cat_array, $cat_in, $cat_not_in );
  1697. }
  1698. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  1699. $q['category__and'] = (array) $q['category__and'];
  1700. if ( ! isset( $q['category__in'] ) )
  1701. $q['category__in'] = array();
  1702. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  1703. unset( $q['category__and'] );
  1704. }
  1705. if ( ! empty( $q['category__in'] ) ) {
  1706. $q['category__in'] = array_map(

Large files files are truncated, but you can click here to view the full file