PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/query.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 3622 lines | 1818 code | 461 blank | 1343 comment | 531 complexity | 4c222d38ae1af23156b0289e7e254a58 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1

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

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