PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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

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