PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 4701 lines | 2308 code | 528 blank | 1865 comment | 601 complexity | 14b4c47ebc867b0918a2893eaccfb8ca MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0

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 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 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()', __( 'http://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 http://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. */
  1122. private $query_vars_hash = false;
  1123. /**
  1124. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1125. * via pre_get_posts hooks.
  1126. *
  1127. * @since 3.1.1
  1128. * @access private
  1129. */
  1130. private $query_vars_changed = true;
  1131. /**
  1132. * Set if post thumbnails are cached
  1133. *
  1134. * @since 3.2.0
  1135. * @access public
  1136. * @var bool
  1137. */
  1138. public $thumbnails_cached = false;
  1139. /**
  1140. * Cached list of search stopwords.
  1141. *
  1142. * @since 3.7.0
  1143. * @var array
  1144. */
  1145. private $stopwords;
  1146. /**
  1147. * Resets query flags to false.
  1148. *
  1149. * The query flags are what page info WordPress was able to figure out.
  1150. *
  1151. * @since 2.0.0
  1152. * @access private
  1153. */
  1154. private function init_query_flags() {
  1155. $this->is_single = false;
  1156. $this->is_preview = false;
  1157. $this->is_page = false;
  1158. $this->is_archive = false;
  1159. $this->is_date = false;
  1160. $this->is_year = false;
  1161. $this->is_month = false;
  1162. $this->is_day = false;
  1163. $this->is_time = false;
  1164. $this->is_author = false;
  1165. $this->is_category = false;
  1166. $this->is_tag = false;
  1167. $this->is_tax = false;
  1168. $this->is_search = false;
  1169. $this->is_feed = false;
  1170. $this->is_comment_feed = false;
  1171. $this->is_trackback = false;
  1172. $this->is_home = false;
  1173. $this->is_404 = false;
  1174. $this->is_comments_popup = false;
  1175. $this->is_paged = false;
  1176. $this->is_admin = false;
  1177. $this->is_attachment = false;
  1178. $this->is_singular = false;
  1179. $this->is_robots = false;
  1180. $this->is_posts_page = false;
  1181. $this->is_post_type_archive = false;
  1182. }
  1183. /**
  1184. * Initiates object properties and sets default values.
  1185. *
  1186. * @since 1.5.0
  1187. * @access public
  1188. */
  1189. public function init() {
  1190. unset($this->posts);
  1191. unset($this->query);
  1192. $this->query_vars = array();
  1193. unset($this->queried_object);
  1194. unset($this->queried_object_id);
  1195. $this->post_count = 0;
  1196. $this->current_post = -1;
  1197. $this->in_the_loop = false;
  1198. unset( $this->request );
  1199. unset( $this->post );
  1200. unset( $this->comments );
  1201. unset( $this->comment );
  1202. $this->comment_count = 0;
  1203. $this->current_comment = -1;
  1204. $this->found_posts = 0;
  1205. $this->max_num_pages = 0;
  1206. $this->max_num_comment_pages = 0;
  1207. $this->init_query_flags();
  1208. }
  1209. /**
  1210. * Reparse the query vars.
  1211. *
  1212. * @since 1.5.0
  1213. * @access public
  1214. */
  1215. public function parse_query_vars() {
  1216. $this->parse_query();
  1217. }
  1218. /**
  1219. * Fills in the query variables, which do not exist within the parameter.
  1220. *
  1221. * @since 2.1.0
  1222. * @access public
  1223. *
  1224. * @param array $array Defined query variables.
  1225. * @return array Complete query variables with undefined ones filled in empty.
  1226. */
  1227. public function fill_query_vars($array) {
  1228. $keys = array(
  1229. 'error'
  1230. , 'm'
  1231. , 'p'
  1232. , 'post_parent'
  1233. , 'subpost'
  1234. , 'subpost_id'
  1235. , 'attachment'
  1236. , 'attachment_id'
  1237. , 'name'
  1238. , 'static'
  1239. , 'pagename'
  1240. , 'page_id'
  1241. , 'second'
  1242. , 'minute'
  1243. , 'hour'
  1244. , 'day'
  1245. , 'monthnum'
  1246. , 'year'
  1247. , 'w'
  1248. , 'category_name'
  1249. , 'tag'
  1250. , 'cat'
  1251. , 'tag_id'
  1252. , 'author'
  1253. , 'author_name'
  1254. , 'feed'
  1255. , 'tb'
  1256. , 'paged'
  1257. , 'comments_popup'
  1258. , 'meta_key'
  1259. , 'meta_value'
  1260. , 'preview'
  1261. , 's'
  1262. , 'sentence'
  1263. , 'fields'
  1264. , 'menu_order'
  1265. );
  1266. foreach ( $keys as $key ) {
  1267. if ( !isset($array[$key]) )
  1268. $array[$key] = '';
  1269. }
  1270. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1271. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  1272. 'author__in', 'author__not_in' );
  1273. foreach ( $array_keys as $key ) {
  1274. if ( !isset($array[$key]) )
  1275. $array[$key] = array();
  1276. }
  1277. return $array;
  1278. }
  1279. /**
  1280. * Parse a query string and set query type booleans.
  1281. *
  1282. * @since 1.5.0
  1283. * @access public
  1284. *
  1285. * @param string|array $query {
  1286. * Optional. Array or string of Query parameters.
  1287. *
  1288. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  1289. * @type int|string $author Author ID, or comma-separated list of IDs.
  1290. * @type string $author_name User 'user_nicename'.
  1291. * @type array $author__in An array of author IDs to query from.
  1292. * @type array $author__not_in An array of author IDs not to query from.
  1293. * @type bool $cache_results Whether to cache post information. Default true.
  1294. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  1295. * @type array $category__and An array of category IDs (AND in).
  1296. * @type array $category__in An array of category IDs (OR in, no children).
  1297. * @type array $category__not_in An array of category IDs (NOT in).
  1298. * @type string $category_name Use category slug (not name, this or any children).
  1299. * @type int $comments_per_page The number of comments to return per page.
  1300. * Default 'comments_per_page' option.
  1301. * @type int|string $comments_popup Whether the query is within the comments popup. Default empty.
  1302. * @type array $date_query An associative array of WP_Date_Query arguments.
  1303. * {@see WP_Date_Query::__construct()}
  1304. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  1305. * @type bool $exact Whether to search by exact keyword. Default false.
  1306. * @type string|array $fields Which fields to return. Single field or all fields (string),
  1307. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  1308. * Default all fields. Accepts 'ids', 'id=>parent'.
  1309. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  1310. * @type bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  1311. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  1312. * Default 0|false.
  1313. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  1314. * numbers 1-12. Default empty.
  1315. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  1316. * @type string $meta_key Custom field key.
  1317. * @type array $meta_query An associative array of WP_Meta_Query arguments.
  1318. * {@see WP_Meta_Query->queries}
  1319. * @type string $meta_value Custom field value.
  1320. * @type int $meta_value_num Custom field value number.
  1321. * @type int $menu_order The menu order of the posts.
  1322. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  1323. * @type string $name Post slug.
  1324. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  1325. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  1326. * performance. Default false.
  1327. * @type int $offset The number of posts to offset before retrieval.
  1328. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  1329. * Accepts 'ASC', 'DESC'.
  1330. * @type string $orderby Sort retrieved posts by parameter. One or more options can be
  1331. * passed. To use 'meta_value', or 'meta_value_num',
  1332. * 'meta_key=keyname' must be also be defined. Default 'date'.
  1333. * Accepts 'none', 'name', 'author', 'date', 'title', 'modified',
  1334. * 'menu_order', 'parent', 'ID', 'rand', 'comment_count'.
  1335. * @type int $p Post ID.
  1336. * @type int $page Show the number of posts that would show up on page X of a
  1337. * static front page.
  1338. * @type int $paged The number of the current page.
  1339. * @type int $page_id Page ID.
  1340. * @type string $pagename Page slug.
  1341. * @type string $perm Show posts if user has the appropriate capability.
  1342. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  1343. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  1344. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  1345. * separated IDs will NOT work.
  1346. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  1347. * top-level pages.
  1348. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  1349. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  1350. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  1351. * Default 'any' if using 'tax_query'.
  1352. * @type string|array $post_status A post status (string) or array of post statuses.
  1353. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  1354. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  1355. * 'posts_per_page' when is_archive(), or is_search() are true.
  1356. * @type string $s Search keyword.
  1357. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  1358. * @type array $search_terms Array of search terms.
  1359. * @type bool $sentence Whether to search by phrase. Default false.
  1360. * @type bool $suppress_filters Whether to suppress filters. Default false.
  1361. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  1362. * @type array $tag__and An array of tag ids (AND in).
  1363. * @type array $tag__in An array of tag ids (OR in).
  1364. * @type array $tag__not_in An array of tag ids (NOT in).
  1365. * @type int $tag_id Tag id or comma-separated list of IDs.
  1366. * @type array $tag_slug__and An array of tag slugs (AND in).
  1367. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  1368. * true. Note: a string of comma-separated IDs will NOT work.
  1369. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  1370. * {@see WP_Tax_Query->queries}
  1371. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  1372. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  1373. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  1374. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  1375. * }
  1376. */
  1377. public function parse_query( $query = '' ) {
  1378. if ( ! empty( $query ) ) {
  1379. $this->init();
  1380. $this->query = $this->query_vars = wp_parse_args( $query );
  1381. } elseif ( ! isset( $this->query ) ) {
  1382. $this->query = $this->query_vars;
  1383. }
  1384. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1385. $qv = &$this->query_vars;
  1386. $this->query_vars_changed = true;
  1387. if ( ! empty($qv['robots']) )
  1388. $this->is_robots = true;
  1389. $qv['p'] = absint($qv['p']);
  1390. $qv['page_id'] = absint($qv['page_id']);
  1391. $qv['year'] = absint($qv['year']);
  1392. $qv['monthnum'] = absint($qv['monthnum']);
  1393. $qv['day'] = absint($qv['day']);
  1394. $qv['w'] = absint($qv['w']);
  1395. $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
  1396. $qv['paged'] = absint($qv['paged']);
  1397. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1398. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  1399. $qv['pagename'] = trim( $qv['pagename'] );
  1400. $qv['name'] = trim( $qv['name'] );
  1401. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1402. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1403. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1404. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1405. // Fairly insane upper bound for search string lengths.
  1406. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  1407. $qv['s'] = '';
  1408. }
  1409. // Compat. Map subpost to attachment.
  1410. if ( '' != $qv['subpost'] )
  1411. $qv['attachment'] = $qv['subpost'];
  1412. if ( '' != $qv['subpost_id'] )
  1413. $qv['attachment_id'] = $qv['subpost_id'];
  1414. $qv['attachment_id'] = absint($qv['attachment_id']);
  1415. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1416. $this->is_single = true;
  1417. $this->is_attachment = true;
  1418. } elseif ( '' != $qv['name'] ) {
  1419. $this->is_single = true;
  1420. } elseif ( $qv['p'] ) {
  1421. $this->is_single = true;
  1422. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1423. // If year, month, day, hour, minute, and second are set, a single
  1424. // post is being queried.
  1425. $this->is_single = true;
  1426. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1427. $this->is_page = true;
  1428. $this->is_single = false;
  1429. } else {
  1430. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1431. if ( isset( $this->query['s'] ) ) {
  1432. $this->is_search = true;
  1433. }
  1434. if ( '' !== $qv['second'] ) {
  1435. $this->is_time = true;
  1436. $this->is_date = true;
  1437. }
  1438. if ( '' !== $qv['minute'] ) {
  1439. $this->is_time = true;
  1440. $this->is_date = true;
  1441. }
  1442. if ( '' !== $qv['hour'] ) {
  1443. $this->is_time = true;
  1444. $this->is_date = true;
  1445. }
  1446. if ( $qv['day'] ) {
  1447. if ( ! $this->is_date ) {
  1448. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  1449. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  1450. $qv['error'] = '404';
  1451. } else {
  1452. $this->is_day = true;
  1453. $this->is_date = true;
  1454. }
  1455. }
  1456. }
  1457. if ( $qv['monthnum'] ) {
  1458. if ( ! $this->is_date ) {
  1459. if ( 12 < $qv['monthnum'] ) {
  1460. $qv['error'] = '404';
  1461. } else {
  1462. $this->is_month = true;
  1463. $this->is_date = true;
  1464. }
  1465. }
  1466. }
  1467. if ( $qv['year'] ) {
  1468. if ( ! $this->is_date ) {
  1469. $this->is_year = true;
  1470. $this->is_date = true;
  1471. }
  1472. }
  1473. if ( $qv['m'] ) {
  1474. $this->is_date = true;
  1475. if ( strlen($qv['m']) > 9 ) {
  1476. $this->is_time = true;
  1477. } else if ( strlen($qv['m']) > 7 ) {
  1478. $this->is_day = true;
  1479. } else if ( strlen($qv['m']) > 5 ) {
  1480. $this->is_month = true;
  1481. } else {
  1482. $this->is_year = true;
  1483. }
  1484. }
  1485. if ( '' != $qv['w'] ) {
  1486. $this->is_date = true;
  1487. }
  1488. $this->query_vars_hash = false;
  1489. $this->parse_tax_query( $qv );
  1490. foreach ( $this->tax_query->queries as $tax_query ) {
  1491. if ( ! is_array( $tax_query ) ) {
  1492. continue;
  1493. }
  1494. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  1495. switch ( $tax_query['taxonomy'] ) {
  1496. case 'category':
  1497. $this->is_category = true;
  1498. break;
  1499. case 'post_tag':
  1500. $this->is_tag = true;
  1501. break;
  1502. default:
  1503. $this->is_tax = true;
  1504. }
  1505. }
  1506. }
  1507. unset( $tax_query );
  1508. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1509. $this->is_author = false;
  1510. } else {
  1511. $this->is_author = true;
  1512. }
  1513. if ( '' != $qv['author_name'] )
  1514. $this->is_author = true;
  1515. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1516. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1517. if ( ! empty( $post_type_obj->has_archive ) )
  1518. $this->is_post_type_archive = true;
  1519. }
  1520. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1521. $this->is_archive = true;
  1522. }
  1523. if ( '' != $qv['feed'] )
  1524. $this->is_feed = true;
  1525. if ( '' != $qv['tb'] )
  1526. $this->is_trackback = true;
  1527. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1528. $this->is_paged = true;
  1529. if ( '' != $qv['comments_popup'] )
  1530. $this->is_comments_popup = true;
  1531. // if we're previewing inside the write screen
  1532. if ( '' != $qv['preview'] )
  1533. $this->is_preview = true;
  1534. if ( is_admin() )
  1535. $this->is_admin = true;
  1536. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1537. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1538. $qv['withcomments'] = 1;
  1539. }
  1540. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1541. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1542. $this->is_comment_feed = true;
  1543. 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 ) )
  1544. $this->is_home = true;
  1545. // Correct is_* for page_on_front and page_for_posts
  1546. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1547. $_query = wp_parse_args($this->query);
  1548. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1549. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1550. unset($_query['pagename']);
  1551. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1552. $this->is_page = true;
  1553. $this->is_home = false;
  1554. $qv['page_id'] = get_option('page_on_front');
  1555. // Correct <!--nextpage--> for page_on_front
  1556. if ( !empty($qv['paged']) ) {
  1557. $qv['page'] = $qv['paged'];
  1558. unset($qv['paged']);
  1559. }
  1560. }
  1561. }
  1562. if ( '' != $qv['pagename'] ) {
  1563. $this->queried_object = get_page_by_path($qv['pagename']);
  1564. if ( !empty($this->queried_object) )
  1565. $this->queried_object_id = (int) $this->queried_object->ID;
  1566. else
  1567. unset($this->queried_object);
  1568. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1569. $this->is_page = false;
  1570. $this->is_home = true;
  1571. $this->is_posts_page = true;
  1572. }
  1573. }
  1574. if ( $qv['page_id'] ) {
  1575. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1576. $this->is_page = false;
  1577. $this->is_home = true;
  1578. $this->is_posts_page = true;
  1579. }
  1580. }
  1581. if ( !empty($qv['post_type']) ) {
  1582. if ( is_array($qv['post_type']) )
  1583. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1584. else
  1585. $qv['post_type'] = sanitize_key($qv['post_type']);
  1586. }
  1587. if ( ! empty( $qv['post_status'] ) ) {
  1588. if ( is_array( $qv['post_status'] ) )
  1589. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1590. else
  1591. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1592. }
  1593. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1594. $this->is_comment_feed = false;
  1595. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1596. // Done correcting is_* for page_on_front and page_for_posts
  1597. if ( '404' == $qv['error'] )
  1598. $this->set_404();
  1599. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1600. $this->query_vars_changed = false;
  1601. /**
  1602. * Fires after the main query vars have been parsed.
  1603. *
  1604. * @since 1.5.0
  1605. *
  1606. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1607. */
  1608. do_action_ref_array( 'parse_query', array( &$this ) );
  1609. }
  1610. /**
  1611. * Parses various taxonomy related query vars.
  1612. *
  1613. * For BC, this method is not marked as protected. See [28987].
  1614. *
  1615. * @access protected
  1616. * @since 3.1.0
  1617. *
  1618. * @param array &$q The query variables
  1619. */
  1620. function parse_tax_query( &$q ) {
  1621. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1622. $tax_query = $q['tax_query'];
  1623. } else {
  1624. $tax_query = array();
  1625. }
  1626. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1627. $tax_query[] = array(
  1628. 'taxonomy' => $q['taxonomy'],
  1629. 'terms' => array( $q['term'] ),
  1630. 'field' => 'slug',
  1631. );
  1632. }
  1633. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  1634. if ( 'post_tag' == $taxonomy )
  1635. continue; // Handled further down in the $q['tag'] block
  1636. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1637. $tax_query_defaults = array(
  1638. 'taxonomy' => $taxonomy,
  1639. 'field' => 'slug',
  1640. );
  1641. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1642. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1643. }
  1644. $term = $q[$t->query_var];
  1645. if ( strpos($term, '+') !== false ) {
  1646. $terms = preg_split( '/[+]+/', $term );
  1647. foreach ( $terms as $term ) {
  1648. $tax_query[] = array_merge( $tax_query_defaults, array(
  1649. 'terms' => array( $term )
  1650. ) );
  1651. }
  1652. } else {
  1653. $tax_query[] = array_merge( $tax_query_defaults, array(
  1654. 'terms' => preg_split( '/[,]+/', $term )
  1655. ) );
  1656. }
  1657. }
  1658. }
  1659. // Category stuff
  1660. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
  1661. $cat_in = $cat_not_in = array();
  1662. $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
  1663. $cat_array = array_map( 'intval', $cat_array );
  1664. $q['cat'] = implode( ',', $cat_array );
  1665. foreach ( $cat_array as $cat ) {
  1666. if ( $cat > 0 )
  1667. $cat_in[] = $cat;
  1668. elseif ( $cat < 0 )
  1669. $cat_not_in[] = abs( $cat );
  1670. }
  1671. if ( ! empty( $cat_in ) ) {
  1672. $tax_query[] = array(
  1673. 'taxonomy' => 'category',
  1674. 'terms' => $cat_in,
  1675. 'field' => 'term_id',
  1676. 'include_children' => true
  1677. );
  1678. }
  1679. if ( ! empty( $cat_not_in ) ) {
  1680. $tax_query[] = array(
  1681. 'taxonomy' => 'category',
  1682. 'terms' => $cat_not_in,
  1683. 'field' => 'term_id',
  1684. 'operator' => 'NOT IN',
  1685. 'include_children' => true
  1686. );
  1687. }
  1688. unset( $cat_array, $cat_in, $cat_not_in );
  1689. }
  1690. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  1691. $q['category__and'] = (array) $q['category__and'];
  1692. if ( ! isset( $q['category__in'] ) )
  1693. $q['category__in'] = array();
  1694. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  1695. unset( $q['category__and'] );
  1696. }
  1697. if ( ! empty( $q['category__in'] ) ) {
  1698. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
  1699. $tax_query[] = array(
  1700. 'taxonomy' => 'category',
  1701. 'terms' => $q['category__in'],
  1702. 'field' => 'term_id',
  1703. 'include_children' => false
  1704. );
  1705. }
  1706. if ( ! empty($q['category__not_in']) ) {
  1707. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  1708. $tax_query[] = array(
  1709. 'taxonomy' => 'category',
  1710. 'terms' => $q['category__not_in'],
  1711. 'operator' => 'NOT IN',
  1712. 'include_children' => false
  1713. );
  1714. }
  1715. if ( ! empty($q['category__and']) ) {
  1716. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['cateā€¦

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