PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/query.php

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

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