PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/サイト/wp/wp-includes/query.php

https://bitbucket.org/recotana/staff-marche
PHP | 3658 lines | 1837 code | 464 blank | 1357 comment | 537 complexity | 35b078a3bfe2e99adb91674477ab3c59 MD5 | raw file

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. $GLOBALS['wp_query'] = new WP_Query();
  84. return $GLOBALS['wp_query']->query($query);
  85. }
  86. /**
  87. * Destroy the previous query and set up a new query.
  88. *
  89. * This should be used after {@link query_posts()} and before another {@link
  90. * query_posts()}. This will remove obscure bugs that occur when the previous
  91. * wp_query object is not destroyed properly before another is set up.
  92. *
  93. * @since 2.3.0
  94. * @uses $wp_query
  95. */
  96. function wp_reset_query() {
  97. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  98. wp_reset_postdata();
  99. }
  100. /**
  101. * After looping through a separate query, this function restores
  102. * the $post global to the current post in the main query
  103. *
  104. * @since 3.0.0
  105. * @uses $wp_query
  106. */
  107. function wp_reset_postdata() {
  108. global $wp_query;
  109. if ( !empty($wp_query->post) ) {
  110. $GLOBALS['post'] = $wp_query->post;
  111. setup_postdata($wp_query->post);
  112. }
  113. }
  114. /*
  115. * Query type checks.
  116. */
  117. /**
  118. * Is the query for an existing archive page?
  119. *
  120. * Month, Year, Category, Author, Post Type archive...
  121. *
  122. * @see WP_Query::is_archive()
  123. * @since 1.5.0
  124. * @uses $wp_query
  125. *
  126. * @return bool
  127. */
  128. function is_archive() {
  129. global $wp_query;
  130. if ( ! isset( $wp_query ) ) {
  131. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  132. return false;
  133. }
  134. return $wp_query->is_archive();
  135. }
  136. /**
  137. * Is the query for an existing post type archive page?
  138. *
  139. * @see WP_Query::is_post_type_archive()
  140. * @since 3.1.0
  141. * @uses $wp_query
  142. *
  143. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  144. * @return bool
  145. */
  146. function is_post_type_archive( $post_types = '' ) {
  147. global $wp_query;
  148. if ( ! isset( $wp_query ) ) {
  149. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  150. return false;
  151. }
  152. return $wp_query->is_post_type_archive( $post_types );
  153. }
  154. /**
  155. * Is the query for an existing attachment page?
  156. *
  157. * @see WP_Query::is_attachment()
  158. * @since 2.0.0
  159. * @uses $wp_query
  160. *
  161. * @return bool
  162. */
  163. function is_attachment() {
  164. global $wp_query;
  165. if ( ! isset( $wp_query ) ) {
  166. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  167. return false;
  168. }
  169. return $wp_query->is_attachment();
  170. }
  171. /**
  172. * Is the query for an existing author archive page?
  173. *
  174. * If the $author parameter is specified, this function will additionally
  175. * check if the query is for one of the authors specified.
  176. *
  177. * @see WP_Query::is_author()
  178. * @since 1.5.0
  179. * @uses $wp_query
  180. *
  181. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  182. * @return bool
  183. */
  184. function is_author( $author = '' ) {
  185. global $wp_query;
  186. if ( ! isset( $wp_query ) ) {
  187. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  188. return false;
  189. }
  190. return $wp_query->is_author( $author );
  191. }
  192. /**
  193. * Is the query for an existing category archive page?
  194. *
  195. * If the $category parameter is specified, this function will additionally
  196. * check if the query is for one of the categories specified.
  197. *
  198. * @see WP_Query::is_category()
  199. * @since 1.5.0
  200. * @uses $wp_query
  201. *
  202. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  203. * @return bool
  204. */
  205. function is_category( $category = '' ) {
  206. global $wp_query;
  207. if ( ! isset( $wp_query ) ) {
  208. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  209. return false;
  210. }
  211. return $wp_query->is_category( $category );
  212. }
  213. /**
  214. * Is the query for an existing tag archive page?
  215. *
  216. * If the $tag parameter is specified, this function will additionally
  217. * check if the query is for one of the tags specified.
  218. *
  219. * @see WP_Query::is_tag()
  220. * @since 2.3.0
  221. * @uses $wp_query
  222. *
  223. * @param mixed $slug Optional. Tag slug or array of slugs.
  224. * @return bool
  225. */
  226. function is_tag( $slug = '' ) {
  227. global $wp_query;
  228. if ( ! isset( $wp_query ) ) {
  229. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  230. return false;
  231. }
  232. return $wp_query->is_tag( $slug );
  233. }
  234. /**
  235. * Is the query for an existing taxonomy archive page?
  236. *
  237. * If the $taxonomy parameter is specified, this function will additionally
  238. * check if the query is for that specific $taxonomy.
  239. *
  240. * If the $term parameter is specified in addition to the $taxonomy parameter,
  241. * this function will additionally check if the query is for one of the terms
  242. * specified.
  243. *
  244. * @see WP_Query::is_tax()
  245. * @since 2.5.0
  246. * @uses $wp_query
  247. *
  248. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  249. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  250. * @return bool
  251. */
  252. function is_tax( $taxonomy = '', $term = '' ) {
  253. global $wp_query;
  254. if ( ! isset( $wp_query ) ) {
  255. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  256. return false;
  257. }
  258. return $wp_query->is_tax( $taxonomy, $term );
  259. }
  260. /**
  261. * Whether the current URL is within the comments popup window.
  262. *
  263. * @see WP_Query::is_comments_popup()
  264. * @since 1.5.0
  265. * @uses $wp_query
  266. *
  267. * @return bool
  268. */
  269. function is_comments_popup() {
  270. global $wp_query;
  271. if ( ! isset( $wp_query ) ) {
  272. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  273. return false;
  274. }
  275. return $wp_query->is_comments_popup();
  276. }
  277. /**
  278. * Is the query for an existing date archive?
  279. *
  280. * @see WP_Query::is_date()
  281. * @since 1.5.0
  282. * @uses $wp_query
  283. *
  284. * @return bool
  285. */
  286. function is_date() {
  287. global $wp_query;
  288. if ( ! isset( $wp_query ) ) {
  289. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  290. return false;
  291. }
  292. return $wp_query->is_date();
  293. }
  294. /**
  295. * Is the query for an existing day archive?
  296. *
  297. * @see WP_Query::is_day()
  298. * @since 1.5.0
  299. * @uses $wp_query
  300. *
  301. * @return bool
  302. */
  303. function is_day() {
  304. global $wp_query;
  305. if ( ! isset( $wp_query ) ) {
  306. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  307. return false;
  308. }
  309. return $wp_query->is_day();
  310. }
  311. /**
  312. * Is the query for a feed?
  313. *
  314. * @see WP_Query::is_feed()
  315. * @since 1.5.0
  316. * @uses $wp_query
  317. *
  318. * @param string|array $feeds Optional feed types to check.
  319. * @return bool
  320. */
  321. function is_feed( $feeds = '' ) {
  322. global $wp_query;
  323. if ( ! isset( $wp_query ) ) {
  324. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  325. return false;
  326. }
  327. return $wp_query->is_feed( $feeds );
  328. }
  329. /**
  330. * Is the query for a comments feed?
  331. *
  332. * @see WP_Query::is_comments_feed()
  333. * @since 3.0.0
  334. * @uses $wp_query
  335. *
  336. * @return bool
  337. */
  338. function is_comment_feed() {
  339. global $wp_query;
  340. if ( ! isset( $wp_query ) ) {
  341. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  342. return false;
  343. }
  344. return $wp_query->is_comment_feed();
  345. }
  346. /**
  347. * Is the query for the front page of the site?
  348. *
  349. * This is for what is displayed at your site's main URL.
  350. *
  351. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  352. *
  353. * If you set a static page for the front page of your site, this function will return
  354. * true when viewing that page.
  355. *
  356. * Otherwise the same as @see is_home()
  357. *
  358. * @see WP_Query::is_front_page()
  359. * @since 2.5.0
  360. * @uses is_home()
  361. * @uses get_option()
  362. *
  363. * @return bool True, if front of site.
  364. */
  365. function is_front_page() {
  366. global $wp_query;
  367. if ( ! isset( $wp_query ) ) {
  368. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  369. return false;
  370. }
  371. return $wp_query->is_front_page();
  372. }
  373. /**
  374. * Is the query for the blog homepage?
  375. *
  376. * This is the page which shows the time based blog content of your site.
  377. *
  378. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  379. *
  380. * If you set a static page for the front page of your site, this function will return
  381. * true only on the page you set as the "Posts page".
  382. *
  383. * @see is_front_page()
  384. *
  385. * @see WP_Query::is_home()
  386. * @since 1.5.0
  387. * @uses $wp_query
  388. *
  389. * @return bool True if blog view homepage.
  390. */
  391. function is_home() {
  392. global $wp_query;
  393. if ( ! isset( $wp_query ) ) {
  394. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  395. return false;
  396. }
  397. return $wp_query->is_home();
  398. }
  399. /**
  400. * Is the query for an existing month archive?
  401. *
  402. * @see WP_Query::is_month()
  403. * @since 1.5.0
  404. * @uses $wp_query
  405. *
  406. * @return bool
  407. */
  408. function is_month() {
  409. global $wp_query;
  410. if ( ! isset( $wp_query ) ) {
  411. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  412. return false;
  413. }
  414. return $wp_query->is_month();
  415. }
  416. /**
  417. * Is the query for an existing single page?
  418. *
  419. * If the $page parameter is specified, this function will additionally
  420. * check if the query is for one of the pages specified.
  421. *
  422. * @see is_single()
  423. * @see is_singular()
  424. *
  425. * @see WP_Query::is_page()
  426. * @since 1.5.0
  427. * @uses $wp_query
  428. *
  429. * @param mixed $page Page ID, title, slug, or array of such.
  430. * @return bool
  431. */
  432. function is_page( $page = '' ) {
  433. global $wp_query;
  434. if ( ! isset( $wp_query ) ) {
  435. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  436. return false;
  437. }
  438. return $wp_query->is_page( $page );
  439. }
  440. /**
  441. * Is the query for paged result and not for the first page?
  442. *
  443. * @see WP_Query::is_paged()
  444. * @since 1.5.0
  445. * @uses $wp_query
  446. *
  447. * @return bool
  448. */
  449. function is_paged() {
  450. global $wp_query;
  451. if ( ! isset( $wp_query ) ) {
  452. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  453. return false;
  454. }
  455. return $wp_query->is_paged();
  456. }
  457. /**
  458. * Is the query for a post or page preview?
  459. *
  460. * @see WP_Query::is_preview()
  461. * @since 2.0.0
  462. * @uses $wp_query
  463. *
  464. * @return bool
  465. */
  466. function is_preview() {
  467. global $wp_query;
  468. if ( ! isset( $wp_query ) ) {
  469. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  470. return false;
  471. }
  472. return $wp_query->is_preview();
  473. }
  474. /**
  475. * Is the query for the robots file?
  476. *
  477. * @see WP_Query::is_robots()
  478. * @since 2.1.0
  479. * @uses $wp_query
  480. *
  481. * @return bool
  482. */
  483. function is_robots() {
  484. global $wp_query;
  485. if ( ! isset( $wp_query ) ) {
  486. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  487. return false;
  488. }
  489. return $wp_query->is_robots();
  490. }
  491. /**
  492. * Is the query for a search?
  493. *
  494. * @see WP_Query::is_search()
  495. * @since 1.5.0
  496. * @uses $wp_query
  497. *
  498. * @return bool
  499. */
  500. function is_search() {
  501. global $wp_query;
  502. if ( ! isset( $wp_query ) ) {
  503. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  504. return false;
  505. }
  506. return $wp_query->is_search();
  507. }
  508. /**
  509. * Is the query for an existing single post?
  510. *
  511. * Works for any post type, except attachments and pages
  512. *
  513. * If the $post parameter is specified, this function will additionally
  514. * check if the query is for one of the Posts specified.
  515. *
  516. * @see is_page()
  517. * @see is_singular()
  518. *
  519. * @see WP_Query::is_single()
  520. * @since 1.5.0
  521. * @uses $wp_query
  522. *
  523. * @param mixed $post Post ID, title, slug, or array of such.
  524. * @return bool
  525. */
  526. function is_single( $post = '' ) {
  527. global $wp_query;
  528. if ( ! isset( $wp_query ) ) {
  529. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  530. return false;
  531. }
  532. return $wp_query->is_single( $post );
  533. }
  534. /**
  535. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  536. *
  537. * If the $post_types parameter is specified, this function will additionally
  538. * check if the query is for one of the Posts Types specified.
  539. *
  540. * @see is_page()
  541. * @see is_single()
  542. *
  543. * @see WP_Query::is_singular()
  544. * @since 1.5.0
  545. * @uses $wp_query
  546. *
  547. * @param mixed $post_types Optional. Post Type or array of Post Types
  548. * @return bool
  549. */
  550. function is_singular( $post_types = '' ) {
  551. global $wp_query;
  552. if ( ! isset( $wp_query ) ) {
  553. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  554. return false;
  555. }
  556. return $wp_query->is_singular( $post_types );
  557. }
  558. /**
  559. * Is the query for a specific time?
  560. *
  561. * @see WP_Query::is_time()
  562. * @since 1.5.0
  563. * @uses $wp_query
  564. *
  565. * @return bool
  566. */
  567. function is_time() {
  568. global $wp_query;
  569. if ( ! isset( $wp_query ) ) {
  570. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  571. return false;
  572. }
  573. return $wp_query->is_time();
  574. }
  575. /**
  576. * Is the query for a trackback endpoint call?
  577. *
  578. * @see WP_Query::is_trackback()
  579. * @since 1.5.0
  580. * @uses $wp_query
  581. *
  582. * @return bool
  583. */
  584. function is_trackback() {
  585. global $wp_query;
  586. if ( ! isset( $wp_query ) ) {
  587. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  588. return false;
  589. }
  590. return $wp_query->is_trackback();
  591. }
  592. /**
  593. * Is the query for an existing year archive?
  594. *
  595. * @see WP_Query::is_year()
  596. * @since 1.5.0
  597. * @uses $wp_query
  598. *
  599. * @return bool
  600. */
  601. function is_year() {
  602. global $wp_query;
  603. if ( ! isset( $wp_query ) ) {
  604. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  605. return false;
  606. }
  607. return $wp_query->is_year();
  608. }
  609. /**
  610. * Is the query a 404 (returns no results)?
  611. *
  612. * @see WP_Query::is_404()
  613. * @since 1.5.0
  614. * @uses $wp_query
  615. *
  616. * @return bool
  617. */
  618. function is_404() {
  619. global $wp_query;
  620. if ( ! isset( $wp_query ) ) {
  621. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  622. return false;
  623. }
  624. return $wp_query->is_404();
  625. }
  626. /**
  627. * Is the query the main query?
  628. *
  629. * @since 3.3.0
  630. *
  631. * @return bool
  632. */
  633. function is_main_query() {
  634. global $wp_query;
  635. return $wp_query->is_main_query();
  636. }
  637. /*
  638. * The Loop. Post loop control.
  639. */
  640. /**
  641. * Whether current WordPress query has results to loop over.
  642. *
  643. * @see WP_Query::have_posts()
  644. * @since 1.5.0
  645. * @uses $wp_query
  646. *
  647. * @return bool
  648. */
  649. function have_posts() {
  650. global $wp_query;
  651. return $wp_query->have_posts();
  652. }
  653. /**
  654. * Whether the caller is in the Loop.
  655. *
  656. * @since 2.0.0
  657. * @uses $wp_query
  658. *
  659. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  660. */
  661. function in_the_loop() {
  662. global $wp_query;
  663. return $wp_query->in_the_loop;
  664. }
  665. /**
  666. * Rewind the loop posts.
  667. *
  668. * @see WP_Query::rewind_posts()
  669. * @since 1.5.0
  670. * @uses $wp_query
  671. *
  672. * @return null
  673. */
  674. function rewind_posts() {
  675. global $wp_query;
  676. return $wp_query->rewind_posts();
  677. }
  678. /**
  679. * Iterate the post index in the loop.
  680. *
  681. * @see WP_Query::the_post()
  682. * @since 1.5.0
  683. * @uses $wp_query
  684. */
  685. function the_post() {
  686. global $wp_query;
  687. $wp_query->the_post();
  688. }
  689. /*
  690. * Comments loop.
  691. */
  692. /**
  693. * Whether there are comments to loop over.
  694. *
  695. * @see WP_Query::have_comments()
  696. * @since 2.2.0
  697. * @uses $wp_query
  698. *
  699. * @return bool
  700. */
  701. function have_comments() {
  702. global $wp_query;
  703. return $wp_query->have_comments();
  704. }
  705. /**
  706. * Iterate comment index in the comment loop.
  707. *
  708. * @see WP_Query::the_comment()
  709. * @since 2.2.0
  710. * @uses $wp_query
  711. *
  712. * @return object
  713. */
  714. function the_comment() {
  715. global $wp_query;
  716. return $wp_query->the_comment();
  717. }
  718. /*
  719. * WP_Query
  720. */
  721. /**
  722. * The WordPress Query class.
  723. *
  724. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  725. *
  726. * @since 1.5.0
  727. */
  728. class WP_Query {
  729. /**
  730. * Query vars set by the user
  731. *
  732. * @since 1.5.0
  733. * @access public
  734. * @var array
  735. */
  736. var $query;
  737. /**
  738. * Query vars, after parsing
  739. *
  740. * @since 1.5.0
  741. * @access public
  742. * @var array
  743. */
  744. var $query_vars = array();
  745. /**
  746. * Taxonomy query, as passed to get_tax_sql()
  747. *
  748. * @since 3.1.0
  749. * @access public
  750. * @var object WP_Tax_Query
  751. */
  752. var $tax_query;
  753. /**
  754. * Metadata query container
  755. *
  756. * @since 3.2.0
  757. * @access public
  758. * @var object WP_Meta_Query
  759. */
  760. var $meta_query = false;
  761. /**
  762. * Holds the data for a single object that is queried.
  763. *
  764. * Holds the contents of a post, page, category, attachment.
  765. *
  766. * @since 1.5.0
  767. * @access public
  768. * @var object|array
  769. */
  770. var $queried_object;
  771. /**
  772. * The ID of the queried object.
  773. *
  774. * @since 1.5.0
  775. * @access public
  776. * @var int
  777. */
  778. var $queried_object_id;
  779. /**
  780. * Get post database query.
  781. *
  782. * @since 2.0.1
  783. * @access public
  784. * @var string
  785. */
  786. var $request;
  787. /**
  788. * List of posts.
  789. *
  790. * @since 1.5.0
  791. * @access public
  792. * @var array
  793. */
  794. var $posts;
  795. /**
  796. * The amount of posts for the current query.
  797. *
  798. * @since 1.5.0
  799. * @access public
  800. * @var int
  801. */
  802. var $post_count = 0;
  803. /**
  804. * Index of the current item in the loop.
  805. *
  806. * @since 1.5.0
  807. * @access public
  808. * @var int
  809. */
  810. var $current_post = -1;
  811. /**
  812. * Whether the loop has started and the caller is in the loop.
  813. *
  814. * @since 2.0.0
  815. * @access public
  816. * @var bool
  817. */
  818. var $in_the_loop = false;
  819. /**
  820. * The current post ID.
  821. *
  822. * @since 1.5.0
  823. * @access public
  824. * @var object
  825. */
  826. var $post;
  827. /**
  828. * The list of comments for current post.
  829. *
  830. * @since 2.2.0
  831. * @access public
  832. * @var array
  833. */
  834. var $comments;
  835. /**
  836. * The amount of comments for the posts.
  837. *
  838. * @since 2.2.0
  839. * @access public
  840. * @var int
  841. */
  842. var $comment_count = 0;
  843. /**
  844. * The index of the comment in the comment loop.
  845. *
  846. * @since 2.2.0
  847. * @access public
  848. * @var int
  849. */
  850. var $current_comment = -1;
  851. /**
  852. * Current comment ID.
  853. *
  854. * @since 2.2.0
  855. * @access public
  856. * @var int
  857. */
  858. var $comment;
  859. /**
  860. * The amount of found posts for the current query.
  861. *
  862. * If limit clause was not used, equals $post_count.
  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. , 'menu_order'
  1245. );
  1246. foreach ( $keys as $key ) {
  1247. if ( !isset($array[$key]) )
  1248. $array[$key] = '';
  1249. }
  1250. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1251. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1252. foreach ( $array_keys as $key ) {
  1253. if ( !isset($array[$key]) )
  1254. $array[$key] = array();
  1255. }
  1256. return $array;
  1257. }
  1258. /**
  1259. * Parse a query string and set query type booleans.
  1260. *
  1261. * @since 1.5.0
  1262. * @access public
  1263. *
  1264. * @param string|array $query Optional query.
  1265. */
  1266. function parse_query( $query = '' ) {
  1267. if ( ! empty( $query ) ) {
  1268. $this->init();
  1269. $this->query = $this->query_vars = wp_parse_args( $query );
  1270. } elseif ( ! isset( $this->query ) ) {
  1271. $this->query = $this->query_vars;
  1272. }
  1273. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1274. $qv = &$this->query_vars;
  1275. $this->query_vars_changed = true;
  1276. if ( ! empty($qv['robots']) )
  1277. $this->is_robots = true;
  1278. $qv['p'] = absint($qv['p']);
  1279. $qv['page_id'] = absint($qv['page_id']);
  1280. $qv['year'] = absint($qv['year']);
  1281. $qv['monthnum'] = absint($qv['monthnum']);
  1282. $qv['day'] = absint($qv['day']);
  1283. $qv['w'] = absint($qv['w']);
  1284. $qv['m'] = absint($qv['m']);
  1285. $qv['paged'] = absint($qv['paged']);
  1286. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1287. $qv['pagename'] = trim( $qv['pagename'] );
  1288. $qv['name'] = trim( $qv['name'] );
  1289. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1290. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1291. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1292. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1293. // Compat. Map subpost to attachment.
  1294. if ( '' != $qv['subpost'] )
  1295. $qv['attachment'] = $qv['subpost'];
  1296. if ( '' != $qv['subpost_id'] )
  1297. $qv['attachment_id'] = $qv['subpost_id'];
  1298. $qv['attachment_id'] = absint($qv['attachment_id']);
  1299. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1300. $this->is_single = true;
  1301. $this->is_attachment = true;
  1302. } elseif ( '' != $qv['name'] ) {
  1303. $this->is_single = true;
  1304. } elseif ( $qv['p'] ) {
  1305. $this->is_single = true;
  1306. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1307. // If year, month, day, hour, minute, and second are set, a single
  1308. // post is being queried.
  1309. $this->is_single = true;
  1310. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1311. $this->is_page = true;
  1312. $this->is_single = false;
  1313. } else {
  1314. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1315. if ( !empty($qv['s']) ) {
  1316. $this->is_search = true;
  1317. }
  1318. if ( '' !== $qv['second'] ) {
  1319. $this->is_time = true;
  1320. $this->is_date = true;
  1321. }
  1322. if ( '' !== $qv['minute'] ) {
  1323. $this->is_time = true;
  1324. $this->is_date = true;
  1325. }
  1326. if ( '' !== $qv['hour'] ) {
  1327. $this->is_time = true;
  1328. $this->is_date = true;
  1329. }
  1330. if ( $qv['day'] ) {
  1331. if ( ! $this->is_date ) {
  1332. $this->is_day = true;
  1333. $this->is_date = true;
  1334. }
  1335. }
  1336. if ( $qv['monthnum'] ) {
  1337. if ( ! $this->is_date ) {
  1338. $this->is_month = true;
  1339. $this->is_date = true;
  1340. }
  1341. }
  1342. if ( $qv['year'] ) {
  1343. if ( ! $this->is_date ) {
  1344. $this->is_year = true;
  1345. $this->is_date = true;
  1346. }
  1347. }
  1348. if ( $qv['m'] ) {
  1349. $this->is_date = true;
  1350. if ( strlen($qv['m']) > 9 ) {
  1351. $this->is_time = true;
  1352. } else if ( strlen($qv['m']) > 7 ) {
  1353. $this->is_day = true;
  1354. } else if ( strlen($qv['m']) > 5 ) {
  1355. $this->is_month = true;
  1356. } else {
  1357. $this->is_year = true;
  1358. }
  1359. }
  1360. if ( '' != $qv['w'] ) {
  1361. $this->is_date = true;
  1362. }
  1363. $this->query_vars_hash = false;
  1364. $this->parse_tax_query( $qv );
  1365. foreach ( $this->tax_query->queries as $tax_query ) {
  1366. if ( 'NOT IN' != $tax_query['operator'] ) {
  1367. switch ( $tax_query['taxonomy'] ) {
  1368. case 'category':
  1369. $this->is_category = true;
  1370. break;
  1371. case 'post_tag':
  1372. $this->is_tag = true;
  1373. break;
  1374. default:
  1375. $this->is_tax = true;
  1376. }
  1377. }
  1378. }
  1379. unset( $tax_query );
  1380. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1381. $this->is_author = false;
  1382. } else {
  1383. $this->is_author = true;
  1384. }
  1385. if ( '' != $qv['author_name'] )
  1386. $this->is_author = true;
  1387. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1388. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1389. if ( ! empty( $post_type_obj->has_archive ) )
  1390. $this->is_post_type_archive = true;
  1391. }
  1392. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1393. $this->is_archive = true;
  1394. }
  1395. if ( '' != $qv['feed'] )
  1396. $this->is_feed = true;
  1397. if ( '' != $qv['tb'] )
  1398. $this->is_trackback = true;
  1399. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1400. $this->is_paged = true;
  1401. if ( '' != $qv['comments_popup'] )
  1402. $this->is_comments_popup = true;
  1403. // if we're previewing inside the write screen
  1404. if ( '' != $qv['preview'] )
  1405. $this->is_preview = true;
  1406. if ( is_admin() )
  1407. $this->is_admin = true;
  1408. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1409. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1410. $qv['withcomments'] = 1;
  1411. }
  1412. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1413. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1414. $this->is_comment_feed = true;
  1415. 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 ) )
  1416. $this->is_home = true;
  1417. // Correct is_* for page_on_front and page_for_posts
  1418. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1419. $_query = wp_parse_args($this->query);
  1420. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1421. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1422. unset($_query['pagename']);
  1423. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1424. $this->is_page = true;
  1425. $this->is_home = false;
  1426. $qv['page_id'] = get_option('page_on_front');
  1427. // Correct <!--nextpage--> for page_on_front
  1428. if ( !empty($qv['paged']) ) {
  1429. $qv['page'] = $qv['paged'];
  1430. unset($qv['paged']);
  1431. }
  1432. }
  1433. }
  1434. if ( '' != $qv['pagename'] ) {
  1435. $this->queried_object = get_page_by_path($qv['pagename']);
  1436. if ( !empty($this->queried_object) )
  1437. $this->queried_object_id = (int) $this->queried_object->ID;
  1438. else
  1439. unset($this->queried_object);
  1440. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1441. $this->is_page = false;
  1442. $this->is_home = true;
  1443. $this->is_posts_page = true;
  1444. }
  1445. }
  1446. if ( $qv['page_id'] ) {
  1447. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1448. $this->is_page = false;
  1449. $this->is_home = true;
  1450. $this->is_posts_page = true;
  1451. }
  1452. }
  1453. if ( !empty($qv['post_type']) ) {
  1454. if ( is_array($qv['post_type']) )
  1455. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1456. else
  1457. $qv['post_type'] = sanitize_key($qv['post_type']);
  1458. }
  1459. if ( ! empty( $qv['post_status'] ) ) {
  1460. if ( is_array( $qv['post_status'] ) )
  1461. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1462. else
  1463. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1464. }
  1465. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1466. $this->is_comment_feed = false;
  1467. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1468. // Done correcting is_* for page_on_front and page_for_posts
  1469. if ( '404' == $qv['error'] )
  1470. $this->set_404();
  1471. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1472. $this->query_vars_changed = false;
  1473. do_action_ref_array('parse_query', array(&$this));
  1474. }
  1475. /*
  1476. * Parses various taxonomy related query vars.
  1477. *
  1478. * @access protected
  1479. * @since 3.1.0
  1480. *
  1481. * @param array &$q The query variables
  1482. */
  1483. function parse_tax_query( &$q ) {
  1484. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1485. $tax_query = $q['tax_query'];
  1486. } else {
  1487. $tax_query = array();
  1488. }
  1489. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1490. $tax_query[] = array(
  1491. 'taxonomy' => $q['taxonomy'],
  1492. 'terms' => array( $q['term'] ),
  1493. 'field' => 'slug',
  1494. );
  1495. }
  1496. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1497. if ( 'post_tag' == $taxonomy )
  1498. continue; // Handled further down in the $q['tag'] block
  1499. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1500. $tax_query_defaults = array(
  1501. 'taxonomy' => $taxonomy,
  1502. 'field' => 'slug',
  1503. );
  1504. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1505. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1506. }
  1507. $term = $q[$t->query_var];
  1508. if ( strpos($term, '+') !== false ) {
  1509. $terms = preg_split( '/[+]+/', $term );
  1510. foreach ( $terms as $term ) {
  1511. $tax_query[] = array_merge( $tax_query_defaults, array(
  1512. 'terms' => array( $term )
  1513. ) );
  1514. }
  1515. } else {
  1516. $tax_query[] = array_merge( $tax_query_defaults, array(
  1517. 'terms' => preg_split( '/[,]+/', $term )
  1518. ) );
  1519. }
  1520. }
  1521. }
  1522. // Category stuff
  1523. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1524. $q['cat'] = ''.urldecode($q['cat']).'';
  1525. $q['cat'] = addslashes_gpc($q['cat']);
  1526. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1527. $q['cat'] = '';
  1528. $req_cats = array();
  1529. foreach ( (array) $cat_array as $cat ) {
  1530. $cat = intval($cat);
  1531. $req_cats[] = $cat;
  1532. $in = ($cat > 0);
  1533. $cat = abs($cat);
  1534. if ( $in ) {
  1535. $q['category__in'][] = $cat;
  1536. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1537. } else {
  1538. $q['category__not_in'][] = $cat;
  1539. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1540. }
  1541. }
  1542. $q['cat'] = implode(',', $req_cats);
  1543. }
  1544. if ( !empty($q['category__in']) ) {
  1545. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1546. $tax_query[] = array(
  1547. 'taxonomy' => 'category',
  1548. 'terms' => $q['category__in'],
  1549. 'field' => 'term_id',
  1550. 'include_children' => false
  1551. );
  1552. }
  1553. if ( !empty($q['category__not_in']) ) {
  1554. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1555. $tax_query[] = array(
  1556. 'taxonomy' => 'category',
  1557. 'terms' => $q['category__not_in'],
  1558. 'operator' => 'NOT IN',
  1559. 'include_children' => false
  1560. );
  1561. }
  1562. if ( !empty($q['category__and']) ) {
  1563. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1564. $tax_query[] = array(
  1565. 'taxonomy' => 'category',
  1566. 'terms' => $q['category__and'],
  1567. 'field' => 'term_id',
  1568. 'operator' => 'AND',
  1569. 'include_children' => false
  1570. );
  1571. }
  1572. // Tag stuff
  1573. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1574. if ( strpos($q['tag'], ',') !== false ) {
  1575. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1576. foreach ( (array) $tags as $tag ) {
  1577. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1578. $q['tag_slug__in'][] = $tag;
  1579. }
  1580. } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
  1581. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1582. foreach ( (array) $tags as $tag ) {
  1583. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1584. $q['tag_slug__and'][] = $tag;
  1585. }
  1586. } else {
  1587. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1588. $q['tag_slug__in'][] = $q['tag'];
  1589. }
  1590. }
  1591. if ( !empty($q['tag_id']) ) {
  1592. $q['tag_id'] = absint( $q['tag_id'] );
  1593. $tax_query[] = array(
  1594. 'taxonomy' => 'post_tag',
  1595. 'terms' => $q['tag_id']
  1596. );
  1597. }
  1598. if ( !empty($q['tag__in']) ) {
  1599. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1600. $tax_query[] = array(
  1601. 'taxonomy' => 'post_tag',
  1602. 'terms' => $q['tag__in']
  1603. );
  1604. }
  1605. if ( !empty($q['tag__not_in']) ) {
  1606. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1607. $tax_query[] = array(
  1608. 'taxonomy' => 'post_tag',
  1609. 'terms' => $q['tag__not_in'],
  1610. 'operator' => 'NOT IN'
  1611. );
  1612. }
  1613. if ( !empty($q['tag__and']) ) {
  1614. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1615. $tax_query[] = array(
  1616. 'taxonomy' => 'post_tag',
  1617. 'terms' => $q['tag__and'],
  1618. 'operator' => 'AND'
  1619. );
  1620. }
  1621. if ( !empty($q['tag_slug__in']) ) {
  1622. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1623. $tax_query[] = array(
  1624. 'taxonomy' => 'post_tag',
  1625. 'terms' => $q['tag_slug__in'],
  1626. 'field' => 'slug'
  1627. );
  1628. }
  1629. if ( !empty($q['tag_slug__and']) ) {
  1630. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1631. $tax_query[] = array(
  1632. 'taxonomy' => 'post_tag',
  1633. 'terms' => $q['tag_slug__and'],
  1634. 'field' => 'slug',
  1635. 'operator' => 'AND'
  1636. );
  1637. }
  1638. $this->tax_query = new WP_Tax_Query( $tax_query );
  1639. }
  1640. /**
  1641. * Sets the 404 property and saves whether query is feed.
  1642. *
  1643. * @since 2.0.0
  1644. * @access public
  1645. */
  1646. function set_404() {
  1647. $is_feed = $this->is_feed;
  1648. $this->init_query_flags();
  1649. $this->is_404 = true;
  1650. $this->is_feed = $is_feed;
  1651. }
  1652. /**
  1653. * Retrieve query variable.
  1654. *
  1655. * @since 1.5.0
  1656. * @access public
  1657. *
  1658. * @param string $query_var Query variable key.
  1659. * @return mixed
  1660. */
  1661. function get($query_var) {
  1662. if ( isset($this->query_vars[$query_var]) )
  1663. return $this->query_vars[$query_var];
  1664. return '';
  1665. }
  1666. /**
  1667. * Set query variable.
  1668. *
  1669. * @since 1.5.0
  1670. * @access public
  1671. *
  1672. * @param string $query_var Query variable key.
  1673. * @param mixed $value Query variable value.
  1674. */
  1675. function set($query_var, $value) {
  1676. $this->query_vars[$query_var] = $value;
  1677. }
  1678. /**
  1679. * Retrieve the posts based on query variables.
  1680. *
  1681. * There are a few filters and actions that can be used to modify the post
  1682. * database query.
  1683. *
  1684. * @since 1.5.0
  1685. * @access public
  1686. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1687. *
  1688. * @return array List of posts.
  1689. */
  1690. function get_posts() {
  1691. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1692. $this->parse_query();
  1693. do_action_ref_array('pre_get_posts', array(&$this));
  1694. // Shorthand.
  1695. $q = &$this->query_vars;
  1696. // Fill again in case pre_get_posts unset some vars.
  1697. $q = $this->fill_query_vars($q);
  1698. // Parse meta query
  1699. $this->meta_query = new WP_Meta_Query();
  1700. $this->meta_query->parse_query_vars( $q );
  1701. // Set a flag if a pre_get_posts hook changed the query vars.
  1702. $hash = md5( serialize( $this->query_vars ) );
  1703. if ( $hash != $this->query_vars_hash ) {
  1704. $this->query_vars_changed = true;
  1705. $this->query_vars_hash = $hash;
  1706. }
  1707. unset($hash);
  1708. // First let's clear some variables
  1709. $distinct = '';
  1710. $whichauthor = '';
  1711. $whichmimetype = '';
  1712. $where = '';
  1713. $limits = '';
  1714. $join = '';
  1715. $search = '';
  1716. $groupby = '';
  1717. $fields = '';
  1718. $post_status_join = false;
  1719. $page = 1;
  1720. if ( isset( $q['caller_get_posts'] ) ) {
  1721. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1722. if ( !isset( $q['ignore_sticky_posts'] ) )
  1723. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1724. }
  1725. if ( !isset( $q['ignore_sticky_posts'] ) )
  1726. $q['ignore_sticky_posts'] = false;
  1727. if ( !isset($q['suppress_filters']) )
  1728. $q['suppress_filters'] = false;
  1729. if ( !isset($q['cache_results']) ) {
  1730. if ( $_wp_using_ext_object_cache )
  1731. $q['cache_results'] = false;
  1732. else
  1733. $q['cache_results'] = true;
  1734. }
  1735. if ( !isset($q['update_post_term_cache']) )
  1736. $q['update_post_term_cache'] = true;
  1737. if ( !isset($q['update_post_meta_cache']) )
  1738. $q['update_post_meta_cache'] = true;
  1739. if ( !isset($q['post_type']) ) {
  1740. if ( $this->is_search )
  1741. $q['post_type'] = 'any';
  1742. else
  1743. $q['post_type'] = '';
  1744. }
  1745. $post_type = $q['post_type'];
  1746. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1747. $q['posts_per_page'] = get_option('posts_per_page');
  1748. if ( isset($q['showposts']) && $q['showposts'] ) {
  1749. $q['showposts'] = (int) $q['showposts'];
  1750. $q['posts_per_page'] = $q['showposts'];
  1751. }
  1752. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1753. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1754. if ( !isset($q['nopaging']) ) {
  1755. if ( $q['posts_per_page'] == -1 ) {
  1756. $q['nopaging'] = true;
  1757. } else {
  1758. $q['nopaging'] = false;
  1759. }
  1760. }
  1761. if ( $this->is_feed ) {
  1762. $q['posts_per_page'] = get_option('posts_per_rss');
  1763. $q['nopaging'] = false;
  1764. }
  1765. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1766. if ( $q['posts_per_page'] < -1 )
  1767. $q['posts_per_page'] = abs($q['posts_per_page']);
  1768. else if ( $q['posts_per_page'] == 0 )
  1769. $q['posts_per_page'] = 1;
  1770. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1771. $q['comments_per_page'] = get_option('comments_per_page');
  1772. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1773. $this->is_page = true;
  1774. $this->is_home = false;
  1775. $q['page_id'] = get_option('page_on_front');
  1776. }
  1777. if ( isset($q['page']) ) {
  1778. $q['page'] = trim($q['page'], '/');
  1779. $q['page'] = absint($q['page']);
  1780. }
  1781. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1782. if ( isset($q['no_found_rows']) )
  1783. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1784. else
  1785. $q['no_found_rows'] = false;
  1786. switch ( $q['fields'] ) {
  1787. case 'ids':
  1788. $fields = "$wpdb->posts.ID";
  1789. break;
  1790. case 'id=>parent':
  1791. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1792. break;
  1793. default:
  1794. $fields = "$wpdb->posts.*";
  1795. }
  1796. if ( '' !== $q['menu_order'] )
  1797. $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
  1798. // If a month is specified in the querystring, load that month
  1799. if ( $q['m'] ) {
  1800. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1801. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1802. if ( strlen($q['m']) > 5 )
  1803. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1804. if ( strlen($q['m']) > 7 )
  1805. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1806. if ( strlen($q['m']) > 9 )
  1807. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1808. if ( strlen($q['m']) > 11 )
  1809. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1810. if ( strlen($q['m']) > 13 )
  1811. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1812. }
  1813. if ( '' !== $q['hour'] )
  1814. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1815. if ( '' !== $q['minute'] )
  1816. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1817. if ( '' !== $q['second'] )
  1818. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1819. if ( $q['year'] )
  1820. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1821. if ( $q['monthnum'] )
  1822. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1823. if ( $q['day'] )
  1824. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1825. // If we've got a post_type AND its not "any" post_type.
  1826. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1827. foreach ( (array)$q['post_type'] as $_post_type ) {
  1828. $ptype_obj = get_post_type_object($_post_type);
  1829. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1830. continue;
  1831. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1832. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1833. $q['name'] = $q[ $ptype_obj->query_var ];
  1834. } else {
  1835. // Hierarchical post_types will operate through the
  1836. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1837. $q['name'] = '';
  1838. }
  1839. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1840. break;
  1841. } //end foreach
  1842. unset($ptype_obj);
  1843. }
  1844. if ( '' != $q['name'] ) {
  1845. $q['name'] = sanitize_title_for_query( $q['name'] );
  1846. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1847. } elseif ( '' != $q['pagename'] ) {
  1848. if ( isset($this->queried_object_id) ) {
  1849. $reqpage = $this->queried_object_id;
  1850. } else {
  1851. if ( 'page' != $q['post_type'] ) {
  1852. foreach ( (array)$q['post_type'] as $_post_type ) {
  1853. $ptype_obj = get_post_type_object($_post_type);
  1854. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1855. continue;
  1856. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1857. if ( $reqpage )
  1858. break;
  1859. }
  1860. unset($ptype_obj);
  1861. } else {
  1862. $reqpage = get_page_by_path($q['pagename']);
  1863. }
  1864. if ( !empty($reqpage) )
  1865. $reqpage = $reqpage->ID;
  1866. else
  1867. $reqpage = 0;
  1868. }
  1869. $page_for_posts = get_option('page_for_posts');
  1870. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1871. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1872. $q['name'] = $q['pagename'];
  1873. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1874. $reqpage_obj = get_post( $reqpage );
  1875. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1876. $this->is_attachment = true;
  1877. $post_type = $q['post_type'] = 'attachment';
  1878. $this->is_page = true;
  1879. $q['attachment_id'] = $reqpage;
  1880. }
  1881. }
  1882. } els

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