PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/query.php

http://thegamesdb.googlecode.com/
PHP | 2561 lines | 1322 code | 315 blank | 924 comment | 388 complexity | abb14908cd1982a50a6cedae4466722f MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, AGPL-3.0, BSD-3-Clause

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 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 a 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 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 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a specific year?
  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. * The Loop. Post loop control.
  630. */
  631. /**
  632. * Whether current WordPress query has results to loop over.
  633. *
  634. * @see WP_Query::have_posts()
  635. * @since 1.5.0
  636. * @uses $wp_query
  637. *
  638. * @return bool
  639. */
  640. function have_posts() {
  641. global $wp_query;
  642. return $wp_query->have_posts();
  643. }
  644. /**
  645. * Whether the caller is in the Loop.
  646. *
  647. * @since 2.0.0
  648. * @uses $wp_query
  649. *
  650. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  651. */
  652. function in_the_loop() {
  653. global $wp_query;
  654. return $wp_query->in_the_loop;
  655. }
  656. /**
  657. * Rewind the loop posts.
  658. *
  659. * @see WP_Query::rewind_posts()
  660. * @since 1.5.0
  661. * @uses $wp_query
  662. *
  663. * @return null
  664. */
  665. function rewind_posts() {
  666. global $wp_query;
  667. return $wp_query->rewind_posts();
  668. }
  669. /**
  670. * Iterate the post index in the loop.
  671. *
  672. * @see WP_Query::the_post()
  673. * @since 1.5.0
  674. * @uses $wp_query
  675. */
  676. function the_post() {
  677. global $wp_query;
  678. $wp_query->the_post();
  679. }
  680. /*
  681. * Comments loop.
  682. */
  683. /**
  684. * Whether there are comments to loop over.
  685. *
  686. * @see WP_Query::have_comments()
  687. * @since 2.2.0
  688. * @uses $wp_query
  689. *
  690. * @return bool
  691. */
  692. function have_comments() {
  693. global $wp_query;
  694. return $wp_query->have_comments();
  695. }
  696. /**
  697. * Iterate comment index in the comment loop.
  698. *
  699. * @see WP_Query::the_comment()
  700. * @since 2.2.0
  701. * @uses $wp_query
  702. *
  703. * @return object
  704. */
  705. function the_comment() {
  706. global $wp_query;
  707. return $wp_query->the_comment();
  708. }
  709. /*
  710. * WP_Query
  711. */
  712. /**
  713. * The WordPress Query class.
  714. *
  715. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  716. *
  717. * @since 1.5.0
  718. */
  719. class WP_Query {
  720. /**
  721. * Query vars set by the user
  722. *
  723. * @since 1.5.0
  724. * @access public
  725. * @var array
  726. */
  727. var $query;
  728. /**
  729. * Query vars, after parsing
  730. *
  731. * @since 1.5.0
  732. * @access public
  733. * @var array
  734. */
  735. var $query_vars = array();
  736. /**
  737. * Taxonomy query, as passed to get_tax_sql()
  738. *
  739. * @since 3.1.0
  740. * @access public
  741. * @var object WP_Tax_Query
  742. */
  743. var $tax_query;
  744. /**
  745. * Metadata query container
  746. *
  747. * @since 3.2.0
  748. * @access public
  749. * @var object WP_Meta_Query
  750. */
  751. var $meta_query = false;
  752. /**
  753. * Holds the data for a single object that is queried.
  754. *
  755. * Holds the contents of a post, page, category, attachment.
  756. *
  757. * @since 1.5.0
  758. * @access public
  759. * @var object|array
  760. */
  761. var $queried_object;
  762. /**
  763. * The ID of the queried object.
  764. *
  765. * @since 1.5.0
  766. * @access public
  767. * @var int
  768. */
  769. var $queried_object_id;
  770. /**
  771. * Get post database query.
  772. *
  773. * @since 2.0.1
  774. * @access public
  775. * @var string
  776. */
  777. var $request;
  778. /**
  779. * List of posts.
  780. *
  781. * @since 1.5.0
  782. * @access public
  783. * @var array
  784. */
  785. var $posts;
  786. /**
  787. * The amount of posts for the current query.
  788. *
  789. * @since 1.5.0
  790. * @access public
  791. * @var int
  792. */
  793. var $post_count = 0;
  794. /**
  795. * Index of the current item in the loop.
  796. *
  797. * @since 1.5.0
  798. * @access public
  799. * @var int
  800. */
  801. var $current_post = -1;
  802. /**
  803. * Whether the loop has started and the caller is in the loop.
  804. *
  805. * @since 2.0.0
  806. * @access public
  807. * @var bool
  808. */
  809. var $in_the_loop = false;
  810. /**
  811. * The current post ID.
  812. *
  813. * @since 1.5.0
  814. * @access public
  815. * @var object
  816. */
  817. var $post;
  818. /**
  819. * The list of comments for current post.
  820. *
  821. * @since 2.2.0
  822. * @access public
  823. * @var array
  824. */
  825. var $comments;
  826. /**
  827. * The amount of comments for the posts.
  828. *
  829. * @since 2.2.0
  830. * @access public
  831. * @var int
  832. */
  833. var $comment_count = 0;
  834. /**
  835. * The index of the comment in the comment loop.
  836. *
  837. * @since 2.2.0
  838. * @access public
  839. * @var int
  840. */
  841. var $current_comment = -1;
  842. /**
  843. * Current comment ID.
  844. *
  845. * @since 2.2.0
  846. * @access public
  847. * @var int
  848. */
  849. var $comment;
  850. /**
  851. * Amount of posts if limit clause was not used.
  852. *
  853. * @since 2.1.0
  854. * @access public
  855. * @var int
  856. */
  857. var $found_posts = 0;
  858. /**
  859. * The amount of pages.
  860. *
  861. * @since 2.1.0
  862. * @access public
  863. * @var int
  864. */
  865. var $max_num_pages = 0;
  866. /**
  867. * The amount of comment pages.
  868. *
  869. * @since 2.7.0
  870. * @access public
  871. * @var int
  872. */
  873. var $max_num_comment_pages = 0;
  874. /**
  875. * Set if query is single post.
  876. *
  877. * @since 1.5.0
  878. * @access public
  879. * @var bool
  880. */
  881. var $is_single = false;
  882. /**
  883. * Set if query is preview of blog.
  884. *
  885. * @since 2.0.0
  886. * @access public
  887. * @var bool
  888. */
  889. var $is_preview = false;
  890. /**
  891. * Set if query returns a page.
  892. *
  893. * @since 1.5.0
  894. * @access public
  895. * @var bool
  896. */
  897. var $is_page = false;
  898. /**
  899. * Set if query is an archive list.
  900. *
  901. * @since 1.5.0
  902. * @access public
  903. * @var bool
  904. */
  905. var $is_archive = false;
  906. /**
  907. * Set if query is part of a date.
  908. *
  909. * @since 1.5.0
  910. * @access public
  911. * @var bool
  912. */
  913. var $is_date = false;
  914. /**
  915. * Set if query contains a year.
  916. *
  917. * @since 1.5.0
  918. * @access public
  919. * @var bool
  920. */
  921. var $is_year = false;
  922. /**
  923. * Set if query contains a month.
  924. *
  925. * @since 1.5.0
  926. * @access public
  927. * @var bool
  928. */
  929. var $is_month = false;
  930. /**
  931. * Set if query contains a day.
  932. *
  933. * @since 1.5.0
  934. * @access public
  935. * @var bool
  936. */
  937. var $is_day = false;
  938. /**
  939. * Set if query contains time.
  940. *
  941. * @since 1.5.0
  942. * @access public
  943. * @var bool
  944. */
  945. var $is_time = false;
  946. /**
  947. * Set if query contains an author.
  948. *
  949. * @since 1.5.0
  950. * @access public
  951. * @var bool
  952. */
  953. var $is_author = false;
  954. /**
  955. * Set if query contains category.
  956. *
  957. * @since 1.5.0
  958. * @access public
  959. * @var bool
  960. */
  961. var $is_category = false;
  962. /**
  963. * Set if query contains tag.
  964. *
  965. * @since 2.3.0
  966. * @access public
  967. * @var bool
  968. */
  969. var $is_tag = false;
  970. /**
  971. * Set if query contains taxonomy.
  972. *
  973. * @since 2.5.0
  974. * @access public
  975. * @var bool
  976. */
  977. var $is_tax = false;
  978. /**
  979. * Set if query was part of a search result.
  980. *
  981. * @since 1.5.0
  982. * @access public
  983. * @var bool
  984. */
  985. var $is_search = false;
  986. /**
  987. * Set if query is feed display.
  988. *
  989. * @since 1.5.0
  990. * @access public
  991. * @var bool
  992. */
  993. var $is_feed = false;
  994. /**
  995. * Set if query is comment feed display.
  996. *
  997. * @since 2.2.0
  998. * @access public
  999. * @var bool
  1000. */
  1001. var $is_comment_feed = false;
  1002. /**
  1003. * Set if query is trackback.
  1004. *
  1005. * @since 1.5.0
  1006. * @access public
  1007. * @var bool
  1008. */
  1009. var $is_trackback = false;
  1010. /**
  1011. * Set if query is blog homepage.
  1012. *
  1013. * @since 1.5.0
  1014. * @access public
  1015. * @var bool
  1016. */
  1017. var $is_home = false;
  1018. /**
  1019. * Set if query couldn't found anything.
  1020. *
  1021. * @since 1.5.0
  1022. * @access public
  1023. * @var bool
  1024. */
  1025. var $is_404 = false;
  1026. /**
  1027. * Set if query is within comments popup window.
  1028. *
  1029. * @since 1.5.0
  1030. * @access public
  1031. * @var bool
  1032. */
  1033. var $is_comments_popup = false;
  1034. /**
  1035. * Set if query is paged
  1036. *
  1037. * @since 1.5.0
  1038. * @access public
  1039. * @var bool
  1040. */
  1041. var $is_paged = false;
  1042. /**
  1043. * Set if query is part of administration page.
  1044. *
  1045. * @since 1.5.0
  1046. * @access public
  1047. * @var bool
  1048. */
  1049. var $is_admin = false;
  1050. /**
  1051. * Set if query is an attachment.
  1052. *
  1053. * @since 2.0.0
  1054. * @access public
  1055. * @var bool
  1056. */
  1057. var $is_attachment = false;
  1058. /**
  1059. * Set if is single, is a page, or is an attachment.
  1060. *
  1061. * @since 2.1.0
  1062. * @access public
  1063. * @var bool
  1064. */
  1065. var $is_singular = false;
  1066. /**
  1067. * Set if query is for robots.
  1068. *
  1069. * @since 2.1.0
  1070. * @access public
  1071. * @var bool
  1072. */
  1073. var $is_robots = false;
  1074. /**
  1075. * Set if query contains posts.
  1076. *
  1077. * Basically, the homepage if the option isn't set for the static homepage.
  1078. *
  1079. * @since 2.1.0
  1080. * @access public
  1081. * @var bool
  1082. */
  1083. var $is_posts_page = false;
  1084. /**
  1085. * Set if query is for a post type archive.
  1086. *
  1087. * @since 3.1.0
  1088. * @access public
  1089. * @var bool
  1090. */
  1091. var $is_post_type_archive = false;
  1092. /**
  1093. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1094. * whether we have to re-parse because something has changed
  1095. *
  1096. * @since 3.1.0
  1097. * @access private
  1098. */
  1099. var $query_vars_hash = false;
  1100. /**
  1101. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1102. * via pre_get_posts hooks.
  1103. *
  1104. * @since 3.1.1
  1105. * @access private
  1106. */
  1107. var $query_vars_changed = true;
  1108. /**
  1109. * Set if post thumbnails are cached
  1110. *
  1111. * @since 3.2.0
  1112. * @access public
  1113. * @var bool
  1114. */
  1115. var $thumbnails_cached = false;
  1116. /**
  1117. * Resets query flags to false.
  1118. *
  1119. * The query flags are what page info WordPress was able to figure out.
  1120. *
  1121. * @since 2.0.0
  1122. * @access private
  1123. */
  1124. function init_query_flags() {
  1125. $this->is_single = false;
  1126. $this->is_preview = false;
  1127. $this->is_page = false;
  1128. $this->is_archive = false;
  1129. $this->is_date = false;
  1130. $this->is_year = false;
  1131. $this->is_month = false;
  1132. $this->is_day = false;
  1133. $this->is_time = false;
  1134. $this->is_author = false;
  1135. $this->is_category = false;
  1136. $this->is_tag = false;
  1137. $this->is_tax = false;
  1138. $this->is_search = false;
  1139. $this->is_feed = false;
  1140. $this->is_comment_feed = false;
  1141. $this->is_trackback = false;
  1142. $this->is_home = false;
  1143. $this->is_404 = false;
  1144. $this->is_comments_popup = false;
  1145. $this->is_paged = false;
  1146. $this->is_admin = false;
  1147. $this->is_attachment = false;
  1148. $this->is_singular = false;
  1149. $this->is_robots = false;
  1150. $this->is_posts_page = false;
  1151. $this->is_post_type_archive = false;
  1152. }
  1153. /**
  1154. * Initiates object properties and sets default values.
  1155. *
  1156. * @since 1.5.0
  1157. * @access public
  1158. */
  1159. function init() {
  1160. unset($this->posts);
  1161. unset($this->query);
  1162. $this->query_vars = array();
  1163. unset($this->queried_object);
  1164. unset($this->queried_object_id);
  1165. $this->post_count = 0;
  1166. $this->current_post = -1;
  1167. $this->in_the_loop = false;
  1168. unset( $this->request );
  1169. unset( $this->post );
  1170. unset( $this->comments );
  1171. unset( $this->comment );
  1172. $this->comment_count = 0;
  1173. $this->current_comment = -1;
  1174. $this->found_posts = 0;
  1175. $this->max_num_pages = 0;
  1176. $this->max_num_comment_pages = 0;
  1177. $this->init_query_flags();
  1178. }
  1179. /**
  1180. * Reparse the query vars.
  1181. *
  1182. * @since 1.5.0
  1183. * @access public
  1184. */
  1185. function parse_query_vars() {
  1186. $this->parse_query();
  1187. }
  1188. /**
  1189. * Fills in the query variables, which do not exist within the parameter.
  1190. *
  1191. * @since 2.1.0
  1192. * @access public
  1193. *
  1194. * @param array $array Defined query variables.
  1195. * @return array Complete query variables with undefined ones filled in empty.
  1196. */
  1197. function fill_query_vars($array) {
  1198. $keys = array(
  1199. 'error'
  1200. , 'm'
  1201. , 'p'
  1202. , 'post_parent'
  1203. , 'subpost'
  1204. , 'subpost_id'
  1205. , 'attachment'
  1206. , 'attachment_id'
  1207. , 'name'
  1208. , 'static'
  1209. , 'pagename'
  1210. , 'page_id'
  1211. , 'second'
  1212. , 'minute'
  1213. , 'hour'
  1214. , 'day'
  1215. , 'monthnum'
  1216. , 'year'
  1217. , 'w'
  1218. , 'category_name'
  1219. , 'tag'
  1220. , 'cat'
  1221. , 'tag_id'
  1222. , 'author_name'
  1223. , 'feed'
  1224. , 'tb'
  1225. , 'paged'
  1226. , 'comments_popup'
  1227. , 'meta_key'
  1228. , 'meta_value'
  1229. , 'preview'
  1230. , 's'
  1231. , 'sentence'
  1232. , 'fields'
  1233. );
  1234. foreach ( $keys as $key ) {
  1235. if ( !isset($array[$key]) )
  1236. $array[$key] = '';
  1237. }
  1238. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1239. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1240. foreach ( $array_keys as $key ) {
  1241. if ( !isset($array[$key]) )
  1242. $array[$key] = array();
  1243. }
  1244. return $array;
  1245. }
  1246. /**
  1247. * Parse a query string and set query type booleans.
  1248. *
  1249. * @since 1.5.0
  1250. * @access public
  1251. *
  1252. * @param string|array $query Optional query.
  1253. */
  1254. function parse_query( $query = '' ) {
  1255. if ( ! empty( $query ) ) {
  1256. $this->init();
  1257. $this->query = $this->query_vars = wp_parse_args( $query );
  1258. } elseif ( ! isset( $this->query ) ) {
  1259. $this->query = $this->query_vars;
  1260. }
  1261. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1262. $qv = &$this->query_vars;
  1263. $this->query_vars_changed = true;
  1264. if ( ! empty($qv['robots']) )
  1265. $this->is_robots = true;
  1266. $qv['p'] = absint($qv['p']);
  1267. $qv['page_id'] = absint($qv['page_id']);
  1268. $qv['year'] = absint($qv['year']);
  1269. $qv['monthnum'] = absint($qv['monthnum']);
  1270. $qv['day'] = absint($qv['day']);
  1271. $qv['w'] = absint($qv['w']);
  1272. $qv['m'] = absint($qv['m']);
  1273. $qv['paged'] = absint($qv['paged']);
  1274. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1275. $qv['pagename'] = trim( $qv['pagename'] );
  1276. $qv['name'] = trim( $qv['name'] );
  1277. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1278. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1279. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1280. // Compat. Map subpost to attachment.
  1281. if ( '' != $qv['subpost'] )
  1282. $qv['attachment'] = $qv['subpost'];
  1283. if ( '' != $qv['subpost_id'] )
  1284. $qv['attachment_id'] = $qv['subpost_id'];
  1285. $qv['attachment_id'] = absint($qv['attachment_id']);
  1286. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1287. $this->is_single = true;
  1288. $this->is_attachment = true;
  1289. } elseif ( '' != $qv['name'] ) {
  1290. $this->is_single = true;
  1291. } elseif ( $qv['p'] ) {
  1292. $this->is_single = true;
  1293. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1294. // If year, month, day, hour, minute, and second are set, a single
  1295. // post is being queried.
  1296. $this->is_single = true;
  1297. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1298. $this->is_page = true;
  1299. $this->is_single = false;
  1300. } else {
  1301. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1302. if ( !empty($qv['s']) ) {
  1303. $this->is_search = true;
  1304. }
  1305. if ( '' !== $qv['second'] ) {
  1306. $this->is_time = true;
  1307. $this->is_date = true;
  1308. }
  1309. if ( '' !== $qv['minute'] ) {
  1310. $this->is_time = true;
  1311. $this->is_date = true;
  1312. }
  1313. if ( '' !== $qv['hour'] ) {
  1314. $this->is_time = true;
  1315. $this->is_date = true;
  1316. }
  1317. if ( $qv['day'] ) {
  1318. if ( ! $this->is_date ) {
  1319. $this->is_day = true;
  1320. $this->is_date = true;
  1321. }
  1322. }
  1323. if ( $qv['monthnum'] ) {
  1324. if ( ! $this->is_date ) {
  1325. $this->is_month = true;
  1326. $this->is_date = true;
  1327. }
  1328. }
  1329. if ( $qv['year'] ) {
  1330. if ( ! $this->is_date ) {
  1331. $this->is_year = true;
  1332. $this->is_date = true;
  1333. }
  1334. }
  1335. if ( $qv['m'] ) {
  1336. $this->is_date = true;
  1337. if ( strlen($qv['m']) > 9 ) {
  1338. $this->is_time = true;
  1339. } else if ( strlen($qv['m']) > 7 ) {
  1340. $this->is_day = true;
  1341. } else if ( strlen($qv['m']) > 5 ) {
  1342. $this->is_month = true;
  1343. } else {
  1344. $this->is_year = true;
  1345. }
  1346. }
  1347. if ( '' != $qv['w'] ) {
  1348. $this->is_date = true;
  1349. }
  1350. $this->query_vars_hash = false;
  1351. $this->parse_tax_query( $qv );
  1352. foreach ( $this->tax_query->queries as $tax_query ) {
  1353. if ( 'NOT IN' != $tax_query['operator'] ) {
  1354. switch ( $tax_query['taxonomy'] ) {
  1355. case 'category':
  1356. $this->is_category = true;
  1357. break;
  1358. case 'post_tag':
  1359. $this->is_tag = true;
  1360. break;
  1361. default:
  1362. $this->is_tax = true;
  1363. }
  1364. }
  1365. }
  1366. unset( $tax_query );
  1367. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1368. $this->is_author = false;
  1369. } else {
  1370. $this->is_author = true;
  1371. }
  1372. if ( '' != $qv['author_name'] )
  1373. $this->is_author = true;
  1374. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1375. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1376. if ( ! empty( $post_type_obj->has_archive ) )
  1377. $this->is_post_type_archive = true;
  1378. }
  1379. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1380. $this->is_archive = true;
  1381. }
  1382. if ( '' != $qv['feed'] )
  1383. $this->is_feed = true;
  1384. if ( '' != $qv['tb'] )
  1385. $this->is_trackback = true;
  1386. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1387. $this->is_paged = true;
  1388. if ( '' != $qv['comments_popup'] )
  1389. $this->is_comments_popup = true;
  1390. // if we're previewing inside the write screen
  1391. if ( '' != $qv['preview'] )
  1392. $this->is_preview = true;
  1393. if ( is_admin() )
  1394. $this->is_admin = true;
  1395. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1396. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1397. $qv['withcomments'] = 1;
  1398. }
  1399. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1400. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1401. $this->is_comment_feed = true;
  1402. 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 ) )
  1403. $this->is_home = true;
  1404. // Correct is_* for page_on_front and page_for_posts
  1405. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1406. $_query = wp_parse_args($this->query);
  1407. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1408. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1409. unset($_query['pagename']);
  1410. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1411. $this->is_page = true;
  1412. $this->is_home = false;
  1413. $qv['page_id'] = get_option('page_on_front');
  1414. // Correct <!--nextpage--> for page_on_front
  1415. if ( !empty($qv['paged']) ) {
  1416. $qv['page'] = $qv['paged'];
  1417. unset($qv['paged']);
  1418. }
  1419. }
  1420. }
  1421. if ( '' != $qv['pagename'] ) {
  1422. $this->queried_object =& get_page_by_path($qv['pagename']);
  1423. if ( !empty($this->queried_object) )
  1424. $this->queried_object_id = (int) $this->queried_object->ID;
  1425. else
  1426. unset($this->queried_object);
  1427. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1428. $this->is_page = false;
  1429. $this->is_home = true;
  1430. $this->is_posts_page = true;
  1431. }
  1432. }
  1433. if ( $qv['page_id'] ) {
  1434. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1435. $this->is_page = false;
  1436. $this->is_home = true;
  1437. $this->is_posts_page = true;
  1438. }
  1439. }
  1440. if ( !empty($qv['post_type']) ) {
  1441. if ( is_array($qv['post_type']) )
  1442. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1443. else
  1444. $qv['post_type'] = sanitize_key($qv['post_type']);
  1445. }
  1446. if ( ! empty( $qv['post_status'] ) ) {
  1447. if ( is_array( $qv['post_status'] ) )
  1448. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1449. else
  1450. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1451. }
  1452. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1453. $this->is_comment_feed = false;
  1454. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1455. // Done correcting is_* for page_on_front and page_for_posts
  1456. if ( '404' == $qv['error'] )
  1457. $this->set_404();
  1458. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1459. $this->query_vars_changed = false;
  1460. do_action_ref_array('parse_query', array(&$this));
  1461. }
  1462. /*
  1463. * Parses various taxonomy related query vars.
  1464. *
  1465. * @access protected
  1466. * @since 3.1.0
  1467. *
  1468. * @param array &$q The query variables
  1469. */
  1470. function parse_tax_query( &$q ) {
  1471. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1472. $tax_query = $q['tax_query'];
  1473. } else {
  1474. $tax_query = array();
  1475. }
  1476. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1477. $tax_query[] = array(
  1478. 'taxonomy' => $q['taxonomy'],
  1479. 'terms' => array( $q['term'] ),
  1480. 'field' => 'slug',
  1481. );
  1482. }
  1483. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1484. if ( 'post_tag' == $taxonomy )
  1485. continue; // Handled further down in the $q['tag'] block
  1486. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1487. $tax_query_defaults = array(
  1488. 'taxonomy' => $taxonomy,
  1489. 'field' => 'slug',
  1490. );
  1491. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1492. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1493. }
  1494. $term = $q[$t->query_var];
  1495. if ( strpos($term, '+') !== false ) {
  1496. $terms = preg_split( '/[+]+/', $term );
  1497. foreach ( $terms as $term ) {
  1498. $tax_query[] = array_merge( $tax_query_defaults, array(
  1499. 'terms' => array( $term )
  1500. ) );
  1501. }
  1502. } else {
  1503. $tax_query[] = array_merge( $tax_query_defaults, array(
  1504. 'terms' => preg_split( '/[,]+/', $term )
  1505. ) );
  1506. }
  1507. }
  1508. }
  1509. // Category stuff
  1510. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1511. $q['cat'] = ''.urldecode($q['cat']).'';
  1512. $q['cat'] = addslashes_gpc($q['cat']);
  1513. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1514. $q['cat'] = '';
  1515. $req_cats = array();
  1516. foreach ( (array) $cat_array as $cat ) {
  1517. $cat = intval($cat);
  1518. $req_cats[] = $cat;
  1519. $in = ($cat > 0);
  1520. $cat = abs($cat);
  1521. if ( $in ) {
  1522. $q['category__in'][] = $cat;
  1523. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1524. } else {
  1525. $q['category__not_in'][] = $cat;
  1526. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1527. }
  1528. }
  1529. $q['cat'] = implode(',', $req_cats);
  1530. }
  1531. if ( !empty($q['category__in']) ) {
  1532. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1533. $tax_query[] = array(
  1534. 'taxonomy' => 'category',
  1535. 'terms' => $q['category__in'],
  1536. 'field' => 'term_id',
  1537. 'include_children' => false
  1538. );
  1539. }
  1540. if ( !empty($q['category__not_in']) ) {
  1541. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1542. $tax_query[] = array(
  1543. 'taxonomy' => 'category',
  1544. 'terms' => $q['category__not_in'],
  1545. 'operator' => 'NOT IN',
  1546. 'include_children' => false
  1547. );
  1548. }
  1549. if ( !empty($q['category__and']) ) {
  1550. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1551. $tax_query[] = array(
  1552. 'taxonomy' => 'category',
  1553. 'terms' => $q['category__and'],
  1554. 'field' => 'term_id',
  1555. 'operator' => 'AND',
  1556. 'include_children' => false
  1557. );
  1558. }
  1559. // Tag stuff
  1560. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1561. if ( strpos($q['tag'], ',') !== false ) {
  1562. $tags = preg_split('/[,\s]+/', $q['tag']);
  1563. foreach ( (array) $tags as $tag ) {
  1564. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1565. $q['tag_slug__in'][] = $tag;
  1566. }
  1567. } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  1568. $tags = preg_split('/[+\s]+/', $q['tag']);
  1569. foreach ( (array) $tags as $tag ) {
  1570. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1571. $q['tag_slug__and'][] = $tag;
  1572. }
  1573. } else {
  1574. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1575. $q['tag_slug__in'][] = $q['tag'];
  1576. }
  1577. }
  1578. if ( !empty($q['tag_id']) ) {
  1579. $q['tag_id'] = absint( $q['tag_id'] );
  1580. $tax_query[] = array(
  1581. 'taxonomy' => 'post_tag',
  1582. 'terms' => $q['tag_id']
  1583. );
  1584. }
  1585. if ( !empty($q['tag__in']) ) {
  1586. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1587. $tax_query[] = array(
  1588. 'taxonomy' => 'post_tag',
  1589. 'terms' => $q['tag__in']
  1590. );
  1591. }
  1592. if ( !empty($q['tag__not_in']) ) {
  1593. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1594. $tax_query[] = array(
  1595. 'taxonomy' => 'post_tag',
  1596. 'terms' => $q['tag__not_in'],
  1597. 'operator' => 'NOT IN'
  1598. );
  1599. }
  1600. if ( !empty($q['tag__and']) ) {
  1601. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1602. $tax_query[] = array(
  1603. 'taxonomy' => 'post_tag',
  1604. 'terms' => $q['tag__and'],
  1605. 'operator' => 'AND'
  1606. );
  1607. }
  1608. if ( !empty($q['tag_slug__in']) ) {
  1609. $q['tag_slug__in'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__in'] ) );
  1610. $tax_query[] = array(
  1611. 'taxonomy' => 'post_tag',
  1612. 'terms' => $q['tag_slug__in'],
  1613. 'field' => 'slug'
  1614. );
  1615. }
  1616. if ( !empty($q['tag_slug__and']) ) {
  1617. $q['tag_slug__and'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__and'] ) );
  1618. $tax_query[] = array(
  1619. 'taxonomy' => 'post_tag',
  1620. 'terms' => $q['tag_slug__and'],
  1621. 'field' => 'slug',
  1622. 'operator' => 'AND'
  1623. );
  1624. }
  1625. $this->tax_query = new WP_Tax_Query( $tax_query );
  1626. }
  1627. /**
  1628. * Sets the 404 property and saves whether query is feed.
  1629. *
  1630. * @since 2.0.0
  1631. * @access public
  1632. */
  1633. function set_404() {
  1634. $is_feed = $this->is_feed;
  1635. $this->init_query_flags();
  1636. $this->is_404 = true;
  1637. $this->is_feed = $is_feed;
  1638. }
  1639. /**
  1640. * Retrieve query variable.
  1641. *
  1642. * @since 1.5.0
  1643. * @access public
  1644. *
  1645. * @param string $query_var Query variable key.
  1646. * @return mixed
  1647. */
  1648. function get($query_var) {
  1649. if ( isset($this->query_vars[$query_var]) )
  1650. return $this->query_vars[$query_var];
  1651. return '';
  1652. }
  1653. /**
  1654. * Set query variable.
  1655. *
  1656. * @since 1.5.0
  1657. * @access public
  1658. *
  1659. * @param string $query_var Query variable key.
  1660. * @param mixed $value Query variable value.
  1661. */
  1662. function set($query_var, $value) {
  1663. $this->query_vars[$query_var] = $value;
  1664. }
  1665. /**
  1666. * Retrieve the posts based on query variables.
  1667. *
  1668. * There are a few filters and actions that can be used to modify the post
  1669. * database query.
  1670. *
  1671. * @since 1.5.0
  1672. * @access public
  1673. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1674. *
  1675. * @return array List of posts.
  1676. */
  1677. function &get_posts() {
  1678. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1679. $this->parse_query();
  1680. do_action_ref_array('pre_get_posts', array(&$this));
  1681. // Shorthand.
  1682. $q = &$this->query_vars;
  1683. // Fill again in case pre_get_posts unset some vars.
  1684. $q = $this->fill_query_vars($q);
  1685. // Parse meta query
  1686. $this->meta_query = new WP_Meta_Query();
  1687. $this->meta_query->parse_query_vars( $q );
  1688. // Set a flag if a pre_get_posts hook changed the query vars.
  1689. $hash = md5( serialize( $this->query_vars ) );
  1690. if ( $hash != $this->query_vars_hash ) {
  1691. $this->query_vars_changed = true;
  1692. $this->query_vars_hash = $hash;
  1693. }
  1694. unset($hash);
  1695. // First let's clear some variables
  1696. $distinct = '';
  1697. $whichauthor = '';
  1698. $whichmimetype = '';
  1699. $where = '';
  1700. $limits = '';
  1701. $join = '';
  1702. $search = '';
  1703. $groupby = '';
  1704. $fields = '';
  1705. $post_status_join = false;
  1706. $page = 1;
  1707. if ( isset( $q['caller_get_posts'] ) ) {
  1708. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1709. if ( !isset( $q['ignore_sticky_posts'] ) )
  1710. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1711. }
  1712. if ( !isset( $q['ignore_sticky_posts'] ) )
  1713. $q['ignore_sticky_posts'] = false;
  1714. if ( !isset($q['suppress_filters']) )
  1715. $q['suppress_filters'] = false;
  1716. if ( !isset($q['cache_results']) ) {
  1717. if ( $_wp_using_ext_object_cache )
  1718. $q['cache_results'] = false;
  1719. else
  1720. $q['cache_results'] = true;
  1721. }
  1722. if ( !isset($q['update_post_term_cache']) )
  1723. $q['update_post_term_cache'] = true;
  1724. if ( !isset($q['update_post_meta_cache']) )
  1725. $q['update_post_meta_cache'] = true;
  1726. if ( !isset($q['post_type']) ) {
  1727. if ( $this->is_search )
  1728. $q['post_type'] = 'any';
  1729. else
  1730. $q['post_type'] = '';
  1731. }
  1732. $post_type = $q['post_type'];
  1733. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1734. $q['posts_per_page'] = get_option('posts_per_page');
  1735. if ( isset($q['showposts']) && $q['showposts'] ) {
  1736. $q['showposts'] = (int) $q['showposts'];
  1737. $q['posts_per_page'] = $q['showposts'];
  1738. }
  1739. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1740. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1741. if ( !isset($q['nopaging']) ) {
  1742. if ( $q['posts_per_page'] == -1 ) {
  1743. $q['nopaging'] = true;
  1744. } else {
  1745. $q['nopaging'] = false;
  1746. }
  1747. }
  1748. if ( $this->is_feed ) {
  1749. $q['posts_per_page'] = get_option('posts_per_rss');
  1750. $q['nopaging'] = false;
  1751. }
  1752. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1753. if ( $q['posts_per_page'] < -1 )
  1754. $q['posts_per_page'] = abs($q['posts_per_page']);
  1755. else if ( $q['posts_per_page'] == 0 )
  1756. $q['posts_per_page'] = 1;
  1757. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1758. $q['comments_per_page'] = get_option('comments_per_page');
  1759. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1760. $this->is_page = true;
  1761. $this->is_home = false;
  1762. $q['page_id'] = get_option('page_on_front');
  1763. }
  1764. if ( isset($q['page']) ) {
  1765. $q['page'] = trim($q['page'], '/');
  1766. $q['page'] = absint($q['page']);
  1767. }
  1768. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1769. if ( isset($q['no_found_rows']) )
  1770. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1771. else
  1772. $q['no_found_rows'] = false;
  1773. switch ( $q['fields'] ) {
  1774. case 'ids':
  1775. $fields = "$wpdb->posts.ID";
  1776. break;
  1777. case 'id=>parent':
  1778. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1779. break;
  1780. default:
  1781. $fields = "$wpdb->posts.*";
  1782. }
  1783. // If a month is specified in the querystring, load that month
  1784. if ( $q['m'] ) {
  1785. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1786. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1787. if ( strlen($q['m']) > 5 )
  1788. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1789. if ( strlen($q['m']) > 7 )
  1790. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1791. if ( strlen($q['m']) > 9 )
  1792. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1793. if ( strlen($q['m']) > 11 )
  1794. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1795. if ( strlen($q['m']) > 13 )
  1796. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1797. }
  1798. if ( '' !== $q['hour'] )
  1799. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1800. if ( '' !== $q['minute'] )
  1801. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1802. if ( '' !== $q['second'] )
  1803. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1804. if ( $q['year'] )
  1805. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1806. if ( $q['monthnum'] )
  1807. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1808. if ( $q['day'] )
  1809. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1810. // If we've got a post_type AND its not "any" post_type.
  1811. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1812. foreach ( (array)$q['post_type'] as $_post_type ) {
  1813. $ptype_obj = get_post_type_object($_post_type);
  1814. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1815. continue;
  1816. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1817. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1818. $q['name'] = $q[ $ptype_obj->query_var ];
  1819. } else {
  1820. // Hierarchical post_types will operate through the
  1821. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1822. $q['name'] = '';
  1823. }
  1824. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1825. break;
  1826. } //end foreach
  1827. unset($ptype_obj);
  1828. }
  1829. if ( '' != $q['name'] ) {
  1830. $q['name'] = sanitize_title_for_query( $q['name'] );
  1831. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1832. } elseif ( '' != $q['pagename'] ) {
  1833. if ( isset($this->queried_object_id) ) {
  1834. $reqpage = $this->queried_object_id;
  1835. } else {
  1836. if ( 'page' != $q['post_type'] ) {
  1837. foreach ( (array)$q['post_type'] as $_post_type ) {
  1838. $ptype_obj = get_post_type_object($_post_type);
  1839. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1840. continue;
  1841. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1842. if ( $reqpage )
  1843. break;
  1844. }
  1845. unset($ptype_obj);
  1846. } else {
  1847. $reqpage = get_page_by_path($q['pagename']);
  1848. }
  1849. if ( !empty($reqpage) )
  1850. $reqpage = $reqpage->ID;
  1851. else
  1852. $reqpage = 0;
  1853. }
  1854. $page_for_posts = get_option('page_for_posts');
  1855. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1856. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1857. $q['name'] = $q['pagename'];
  1858. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1859. $reqpage_obj = get_page($reqpage);
  1860. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1861. $this->is_attachment = true;
  1862. $post_type = $q['post_type'] = 'attachment';
  1863. $this->is_page = true;
  1864. $q['attachment_id'] = $reqpage;
  1865. }
  1866. }
  1867. } elseif ( '' != $q['attachment'] ) {
  1868. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1869. $q['name'] = $q['attachment'];
  1870. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1871. }
  1872. if ( $q['w'] )
  1873. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1874. if ( intval($q['comments_popup']) )
  1875. $q['p'] = absint($q['comments_popup']);
  1876. // If an attachment is requested by number, let it supercede any post number.
  1877. if ( $q['attachment_…

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