PageRenderTime 37ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/query.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 3575 lines | 1796 code | 451 blank | 1328 comment | 522 complexity | 15579b8b7d5c120fffe2859c3e071013 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress to the user is on. It
  6. * also provides functionality to getting URL query information.
  7. *
  8. * @link http://codex.wordpress.org/The_Loop More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieve variable in the WP_Query class.
  15. *
  16. * @see WP_Query::get()
  17. * @since 1.5.0
  18. * @uses $wp_query
  19. *
  20. * @param string $var The variable key to retrieve.
  21. * @return mixed
  22. */
  23. function get_query_var($var) {
  24. global $wp_query;
  25. return $wp_query->get($var);
  26. }
  27. /**
  28. * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  29. *
  30. * @uses WP_Query::get_queried_object
  31. *
  32. * @since 3.1.0
  33. * @access public
  34. *
  35. * @return object
  36. */
  37. function get_queried_object() {
  38. global $wp_query;
  39. return $wp_query->get_queried_object();
  40. }
  41. /**
  42. * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
  43. *
  44. * @uses WP_Query::get_queried_object_id()
  45. *
  46. * @since 3.1.0
  47. * @access public
  48. *
  49. * @return int
  50. */
  51. function get_queried_object_id() {
  52. global $wp_query;
  53. return $wp_query->get_queried_object_id();
  54. }
  55. /**
  56. * Set query variable.
  57. *
  58. * @see WP_Query::set()
  59. * @since 2.2.0
  60. * @uses $wp_query
  61. *
  62. * @param string $var Query variable key.
  63. * @param mixed $value
  64. * @return null
  65. */
  66. function set_query_var($var, $value) {
  67. global $wp_query;
  68. return $wp_query->set($var, $value);
  69. }
  70. /**
  71. * Set up The Loop with query parameters.
  72. *
  73. * This will override the current WordPress Loop and shouldn't be used more than
  74. * once. This must not be used within the WordPress Loop.
  75. *
  76. * @since 1.5.0
  77. * @uses $wp_query
  78. *
  79. * @param string $query
  80. * @return array List of posts
  81. */
  82. function &query_posts($query) {
  83. unset($GLOBALS['wp_query']);
  84. $GLOBALS['wp_query'] =& new WP_Query();
  85. return $GLOBALS['wp_query']->query($query);
  86. }
  87. /**
  88. * Destroy the previous query and set up a new query.
  89. *
  90. * This should be used after {@link query_posts()} and before another {@link
  91. * query_posts()}. This will remove obscure bugs that occur when the previous
  92. * wp_query object is not destroyed properly before another is set up.
  93. *
  94. * @since 2.3.0
  95. * @uses $wp_query
  96. */
  97. function wp_reset_query() {
  98. unset($GLOBALS['wp_query']);
  99. $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
  100. wp_reset_postdata();
  101. }
  102. /**
  103. * After looping through a separate query, this function restores
  104. * the $post global to the current post in the main query
  105. *
  106. * @since 3.0.0
  107. * @uses $wp_query
  108. */
  109. function wp_reset_postdata() {
  110. global $wp_query;
  111. if ( !empty($wp_query->post) ) {
  112. $GLOBALS['post'] = $wp_query->post;
  113. setup_postdata($wp_query->post);
  114. }
  115. }
  116. /*
  117. * Query type checks.
  118. */
  119. /**
  120. * Is the query for an archive page?
  121. *
  122. * Month, Year, Category, Author, Post Type archive...
  123. *
  124. * @see WP_Query::is_archive()
  125. * @since 1.5.0
  126. * @uses $wp_query
  127. *
  128. * @return bool
  129. */
  130. function is_archive() {
  131. global $wp_query;
  132. if ( ! isset( $wp_query ) ) {
  133. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  134. return false;
  135. }
  136. return $wp_query->is_archive();
  137. }
  138. /**
  139. * Is the query for a post type archive page?
  140. *
  141. * @see WP_Query::is_post_type_archive()
  142. * @since 3.1.0
  143. * @uses $wp_query
  144. *
  145. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  146. * @return bool
  147. */
  148. function is_post_type_archive( $post_types = '' ) {
  149. global $wp_query;
  150. if ( ! isset( $wp_query ) ) {
  151. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  152. return false;
  153. }
  154. return $wp_query->is_post_type_archive( $post_types );
  155. }
  156. /**
  157. * Is the query for an attachment page?
  158. *
  159. * @see WP_Query::is_attachment()
  160. * @since 2.0.0
  161. * @uses $wp_query
  162. *
  163. * @return bool
  164. */
  165. function is_attachment() {
  166. global $wp_query;
  167. if ( ! isset( $wp_query ) ) {
  168. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  169. return false;
  170. }
  171. return $wp_query->is_attachment();
  172. }
  173. /**
  174. * Is the query for an author archive page?
  175. *
  176. * If the $author parameter is specified, this function will additionally
  177. * check if the query is for one of the authors specified.
  178. *
  179. * @see WP_Query::is_author()
  180. * @since 1.5.0
  181. * @uses $wp_query
  182. *
  183. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  184. * @return bool
  185. */
  186. function is_author( $author = '' ) {
  187. global $wp_query;
  188. if ( ! isset( $wp_query ) ) {
  189. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  190. return false;
  191. }
  192. return $wp_query->is_author( $author );
  193. }
  194. /**
  195. * Is the query for a category archive page?
  196. *
  197. * If the $category parameter is specified, this function will additionally
  198. * check if the query is for one of the categories specified.
  199. *
  200. * @see WP_Query::is_category()
  201. * @since 1.5.0
  202. * @uses $wp_query
  203. *
  204. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  205. * @return bool
  206. */
  207. function is_category( $category = '' ) {
  208. global $wp_query;
  209. if ( ! isset( $wp_query ) ) {
  210. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  211. return false;
  212. }
  213. return $wp_query->is_category( $category );
  214. }
  215. /**
  216. * Is the query for a tag archive page?
  217. *
  218. * If the $tag parameter is specified, this function will additionally
  219. * check if the query is for one of the tags specified.
  220. *
  221. * @see WP_Query::is_tag()
  222. * @since 2.3.0
  223. * @uses $wp_query
  224. *
  225. * @param mixed $slug Optional. Tag slug or array of slugs.
  226. * @return bool
  227. */
  228. function is_tag( $slug = '' ) {
  229. global $wp_query;
  230. if ( ! isset( $wp_query ) ) {
  231. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  232. return false;
  233. }
  234. return $wp_query->is_tag( $slug );
  235. }
  236. /**
  237. * Is the query for a taxonomy archive page?
  238. *
  239. * If the $taxonomy parameter is specified, this function will additionally
  240. * check if the query is for that specific $taxonomy.
  241. *
  242. * If the $term parameter is specified in addition to the $taxonomy parameter,
  243. * this function will additionally check if the query is for one of the terms
  244. * specified.
  245. *
  246. * @see WP_Query::is_tax()
  247. * @since 2.5.0
  248. * @uses $wp_query
  249. *
  250. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  251. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  252. * @return bool
  253. */
  254. function is_tax( $taxonomy = '', $term = '' ) {
  255. global $wp_query;
  256. if ( ! isset( $wp_query ) ) {
  257. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  258. return false;
  259. }
  260. return $wp_query->is_tax( $taxonomy, $term );
  261. }
  262. /**
  263. * Whether the current URL is within the comments popup window.
  264. *
  265. * @see WP_Query::is_comments_popup()
  266. * @since 1.5.0
  267. * @uses $wp_query
  268. *
  269. * @return bool
  270. */
  271. function is_comments_popup() {
  272. global $wp_query;
  273. if ( ! isset( $wp_query ) ) {
  274. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  275. return false;
  276. }
  277. return $wp_query->is_comments_popup();
  278. }
  279. /**
  280. * Is the query for a date archive?
  281. *
  282. * @see WP_Query::is_date()
  283. * @since 1.5.0
  284. * @uses $wp_query
  285. *
  286. * @return bool
  287. */
  288. function is_date() {
  289. global $wp_query;
  290. if ( ! isset( $wp_query ) ) {
  291. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  292. return false;
  293. }
  294. return $wp_query->is_date();
  295. }
  296. /**
  297. * Is the query for a day archive?
  298. *
  299. * @see WP_Query::is_day()
  300. * @since 1.5.0
  301. * @uses $wp_query
  302. *
  303. * @return bool
  304. */
  305. function is_day() {
  306. global $wp_query;
  307. if ( ! isset( $wp_query ) ) {
  308. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  309. return false;
  310. }
  311. return $wp_query->is_day();
  312. }
  313. /**
  314. * Is the query for a feed?
  315. *
  316. * @see WP_Query::is_feed()
  317. * @since 1.5.0
  318. * @uses $wp_query
  319. *
  320. * @param string|array $feeds Optional feed types to check.
  321. * @return bool
  322. */
  323. function is_feed( $feeds = '' ) {
  324. global $wp_query;
  325. if ( ! isset( $wp_query ) ) {
  326. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  327. return false;
  328. }
  329. return $wp_query->is_feed( $feeds );
  330. }
  331. /**
  332. * Is the query for a comments feed?
  333. *
  334. * @see WP_Query::is_comments_feed()
  335. * @since 3.0.0
  336. * @uses $wp_query
  337. *
  338. * @return bool
  339. */
  340. function is_comment_feed() {
  341. global $wp_query;
  342. if ( ! isset( $wp_query ) ) {
  343. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  344. return false;
  345. }
  346. return $wp_query->is_comment_feed();
  347. }
  348. /**
  349. * Is the query for the front page of the site?
  350. *
  351. * This is for what is displayed at your site's main URL.
  352. *
  353. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  354. *
  355. * If you set a static page for the front page of your site, this function will return
  356. * true when viewing that page.
  357. *
  358. * Otherwise the same as @see is_home()
  359. *
  360. * @see WP_Query::is_front_page()
  361. * @since 2.5.0
  362. * @uses is_home()
  363. * @uses get_option()
  364. *
  365. * @return bool True, if front of site.
  366. */
  367. function is_front_page() {
  368. global $wp_query;
  369. if ( ! isset( $wp_query ) ) {
  370. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  371. return false;
  372. }
  373. return $wp_query->is_front_page();
  374. }
  375. /**
  376. * Is the query for the blog homepage?
  377. *
  378. * This is the page which shows the time based blog content of your site.
  379. *
  380. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  381. *
  382. * If you set a static page for the front page of your site, this function will return
  383. * true only on the page you set as the "Posts page".
  384. *
  385. * @see is_front_page()
  386. *
  387. * @see WP_Query::is_home()
  388. * @since 1.5.0
  389. * @uses $wp_query
  390. *
  391. * @return bool True if blog view homepage.
  392. */
  393. function is_home() {
  394. global $wp_query;
  395. if ( ! isset( $wp_query ) ) {
  396. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  397. return false;
  398. }
  399. return $wp_query->is_home();
  400. }
  401. /**
  402. * Is the query for a month archive?
  403. *
  404. * @see WP_Query::is_month()
  405. * @since 1.5.0
  406. * @uses $wp_query
  407. *
  408. * @return bool
  409. */
  410. function is_month() {
  411. global $wp_query;
  412. if ( ! isset( $wp_query ) ) {
  413. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  414. return false;
  415. }
  416. return $wp_query->is_month();
  417. }
  418. /**
  419. * Is the query for a single page?
  420. *
  421. * If the $page parameter is specified, this function will additionally
  422. * check if the query is for one of the pages specified.
  423. *
  424. * @see is_single()
  425. * @see is_singular()
  426. *
  427. * @see WP_Query::is_page()
  428. * @since 1.5.0
  429. * @uses $wp_query
  430. *
  431. * @param mixed $page Page ID, title, slug, or array of such.
  432. * @return bool
  433. */
  434. function is_page( $page = '' ) {
  435. global $wp_query;
  436. if ( ! isset( $wp_query ) ) {
  437. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  438. return false;
  439. }
  440. return $wp_query->is_page( $page );
  441. }
  442. /**
  443. * Is the query for paged result and not for the first page?
  444. *
  445. * @see WP_Query::is_paged()
  446. * @since 1.5.0
  447. * @uses $wp_query
  448. *
  449. * @return bool
  450. */
  451. function is_paged() {
  452. global $wp_query;
  453. if ( ! isset( $wp_query ) ) {
  454. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  455. return false;
  456. }
  457. return $wp_query->is_paged();
  458. }
  459. /**
  460. * Is the query for a post or page preview?
  461. *
  462. * @see WP_Query::is_preview()
  463. * @since 2.0.0
  464. * @uses $wp_query
  465. *
  466. * @return bool
  467. */
  468. function is_preview() {
  469. global $wp_query;
  470. if ( ! isset( $wp_query ) ) {
  471. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  472. return false;
  473. }
  474. return $wp_query->is_preview();
  475. }
  476. /**
  477. * Is the query for the robots file?
  478. *
  479. * @see WP_Query::is_robots()
  480. * @since 2.1.0
  481. * @uses $wp_query
  482. *
  483. * @return bool
  484. */
  485. function is_robots() {
  486. global $wp_query;
  487. if ( ! isset( $wp_query ) ) {
  488. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  489. return false;
  490. }
  491. return $wp_query->is_robots();
  492. }
  493. /**
  494. * Is the query for a search?
  495. *
  496. * @see WP_Query::is_search()
  497. * @since 1.5.0
  498. * @uses $wp_query
  499. *
  500. * @return bool
  501. */
  502. function is_search() {
  503. global $wp_query;
  504. if ( ! isset( $wp_query ) ) {
  505. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  506. return false;
  507. }
  508. return $wp_query->is_search();
  509. }
  510. /**
  511. * Is the query for a single post?
  512. *
  513. * Works for any post type, except attachments and pages
  514. *
  515. * If the $post parameter is specified, this function will additionally
  516. * check if the query is for one of the Posts specified.
  517. *
  518. * @see is_page()
  519. * @see is_singular()
  520. *
  521. * @see WP_Query::is_single()
  522. * @since 1.5.0
  523. * @uses $wp_query
  524. *
  525. * @param mixed $post Post ID, title, slug, or array of such.
  526. * @return bool
  527. */
  528. function is_single( $post = '' ) {
  529. global $wp_query;
  530. if ( ! isset( $wp_query ) ) {
  531. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  532. return false;
  533. }
  534. return $wp_query->is_single( $post );
  535. }
  536. /**
  537. * Is the query for a single post of any post type (post, attachment, page, ... )?
  538. *
  539. * If the $post_types parameter is specified, this function will additionally
  540. * check if the query is for one of the Posts Types specified.
  541. *
  542. * @see is_page()
  543. * @see is_single()
  544. *
  545. * @see WP_Query::is_singular()
  546. * @since 1.5.0
  547. * @uses $wp_query
  548. *
  549. * @param mixed $post_types Optional. Post Type or array of Post Types
  550. * @return bool
  551. */
  552. function is_singular( $post_types = '' ) {
  553. global $wp_query;
  554. if ( ! isset( $wp_query ) ) {
  555. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  556. return false;
  557. }
  558. return $wp_query->is_singular( $post_types );
  559. }
  560. /**
  561. * Is the query for a specific time?
  562. *
  563. * @see WP_Query::is_time()
  564. * @since 1.5.0
  565. * @uses $wp_query
  566. *
  567. * @return bool
  568. */
  569. function is_time() {
  570. global $wp_query;
  571. if ( ! isset( $wp_query ) ) {
  572. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  573. return false;
  574. }
  575. return $wp_query->is_time();
  576. }
  577. /**
  578. * Is the query for a trackback endpoint call?
  579. *
  580. * @see WP_Query::is_trackback()
  581. * @since 1.5.0
  582. * @uses $wp_query
  583. *
  584. * @return bool
  585. */
  586. function is_trackback() {
  587. global $wp_query;
  588. if ( ! isset( $wp_query ) ) {
  589. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  590. return false;
  591. }
  592. return $wp_query->is_trackback();
  593. }
  594. /**
  595. * Is the query for a specific year?
  596. *
  597. * @see WP_Query::is_year()
  598. * @since 1.5.0
  599. * @uses $wp_query
  600. *
  601. * @return bool
  602. */
  603. function is_year() {
  604. global $wp_query;
  605. if ( ! isset( $wp_query ) ) {
  606. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  607. return false;
  608. }
  609. return $wp_query->is_year();
  610. }
  611. /**
  612. * Is the query a 404 (returns no results)?
  613. *
  614. * @see WP_Query::is_404()
  615. * @since 1.5.0
  616. * @uses $wp_query
  617. *
  618. * @return bool
  619. */
  620. function is_404() {
  621. global $wp_query;
  622. if ( ! isset( $wp_query ) ) {
  623. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  624. return false;
  625. }
  626. return $wp_query->is_404();
  627. }
  628. /*
  629. * The Loop. Post loop control.
  630. */
  631. /**
  632. * Whether current WordPress query has results to loop over.
  633. *
  634. * @see WP_Query::have_posts()
  635. * @since 1.5.0
  636. * @uses $wp_query
  637. *
  638. * @return bool
  639. */
  640. function have_posts() {
  641. global $wp_query;
  642. return $wp_query->have_posts();
  643. }
  644. /**
  645. * Whether the caller is in the Loop.
  646. *
  647. * @since 2.0.0
  648. * @uses $wp_query
  649. *
  650. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  651. */
  652. function in_the_loop() {
  653. global $wp_query;
  654. return $wp_query->in_the_loop;
  655. }
  656. /**
  657. * Rewind the loop posts.
  658. *
  659. * @see WP_Query::rewind_posts()
  660. * @since 1.5.0
  661. * @uses $wp_query
  662. *
  663. * @return null
  664. */
  665. function rewind_posts() {
  666. global $wp_query;
  667. return $wp_query->rewind_posts();
  668. }
  669. /**
  670. * Iterate the post index in the loop.
  671. *
  672. * @see WP_Query::the_post()
  673. * @since 1.5.0
  674. * @uses $wp_query
  675. */
  676. function the_post() {
  677. global $wp_query;
  678. $wp_query->the_post();
  679. }
  680. /*
  681. * Comments loop.
  682. */
  683. /**
  684. * Whether there are comments to loop over.
  685. *
  686. * @see WP_Query::have_comments()
  687. * @since 2.2.0
  688. * @uses $wp_query
  689. *
  690. * @return bool
  691. */
  692. function have_comments() {
  693. global $wp_query;
  694. return $wp_query->have_comments();
  695. }
  696. /**
  697. * Iterate comment index in the comment loop.
  698. *
  699. * @see WP_Query::the_comment()
  700. * @since 2.2.0
  701. * @uses $wp_query
  702. *
  703. * @return object
  704. */
  705. function the_comment() {
  706. global $wp_query;
  707. return $wp_query->the_comment();
  708. }
  709. /*
  710. * WP_Query
  711. */
  712. /**
  713. * The WordPress Query class.
  714. *
  715. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  716. *
  717. * @since 1.5.0
  718. */
  719. class WP_Query {
  720. /**
  721. * Query vars set by the user
  722. *
  723. * @since 1.5.0
  724. * @access public
  725. * @var array
  726. */
  727. var $query;
  728. /**
  729. * Query vars, after parsing
  730. *
  731. * @since 1.5.0
  732. * @access public
  733. * @var array
  734. */
  735. var $query_vars = array();
  736. /**
  737. * Taxonomy query, as passed to get_tax_sql()
  738. *
  739. * @since 3.1.0
  740. * @access public
  741. * @var object WP_Tax_Query
  742. */
  743. var $tax_query;
  744. /**
  745. * Metadata query container
  746. *
  747. * @since 3.2.0
  748. * @access public
  749. * @var object WP_Meta_Query
  750. */
  751. var $meta_query = false;
  752. /**
  753. * Holds the data for a single object that is queried.
  754. *
  755. * Holds the contents of a post, page, category, attachment.
  756. *
  757. * @since 1.5.0
  758. * @access public
  759. * @var object|array
  760. */
  761. var $queried_object;
  762. /**
  763. * The ID of the queried object.
  764. *
  765. * @since 1.5.0
  766. * @access public
  767. * @var int
  768. */
  769. var $queried_object_id;
  770. /**
  771. * Get post database query.
  772. *
  773. * @since 2.0.1
  774. * @access public
  775. * @var string
  776. */
  777. var $request;
  778. /**
  779. * List of posts.
  780. *
  781. * @since 1.5.0
  782. * @access public
  783. * @var array
  784. */
  785. var $posts;
  786. /**
  787. * The amount of posts for the current query.
  788. *
  789. * @since 1.5.0
  790. * @access public
  791. * @var int
  792. */
  793. var $post_count = 0;
  794. /**
  795. * Index of the current item in the loop.
  796. *
  797. * @since 1.5.0
  798. * @access public
  799. * @var int
  800. */
  801. var $current_post = -1;
  802. /**
  803. * Whether the loop has started and the caller is in the loop.
  804. *
  805. * @since 2.0.0
  806. * @access public
  807. * @var bool
  808. */
  809. var $in_the_loop = false;
  810. /**
  811. * The current post ID.
  812. *
  813. * @since 1.5.0
  814. * @access public
  815. * @var object
  816. */
  817. var $post;
  818. /**
  819. * The list of comments for current post.
  820. *
  821. * @since 2.2.0
  822. * @access public
  823. * @var array
  824. */
  825. var $comments;
  826. /**
  827. * The amount of comments for the posts.
  828. *
  829. * @since 2.2.0
  830. * @access public
  831. * @var int
  832. */
  833. var $comment_count = 0;
  834. /**
  835. * The index of the comment in the comment loop.
  836. *
  837. * @since 2.2.0
  838. * @access public
  839. * @var int
  840. */
  841. var $current_comment = -1;
  842. /**
  843. * Current comment ID.
  844. *
  845. * @since 2.2.0
  846. * @access public
  847. * @var int
  848. */
  849. var $comment;
  850. /**
  851. * Amount of posts if limit clause was not used.
  852. *
  853. * @since 2.1.0
  854. * @access public
  855. * @var int
  856. */
  857. var $found_posts = 0;
  858. /**
  859. * The amount of pages.
  860. *
  861. * @since 2.1.0
  862. * @access public
  863. * @var int
  864. */
  865. var $max_num_pages = 0;
  866. /**
  867. * The amount of comment pages.
  868. *
  869. * @since 2.7.0
  870. * @access public
  871. * @var int
  872. */
  873. var $max_num_comment_pages = 0;
  874. /**
  875. * Set if query is single post.
  876. *
  877. * @since 1.5.0
  878. * @access public
  879. * @var bool
  880. */
  881. var $is_single = false;
  882. /**
  883. * Set if query is preview of blog.
  884. *
  885. * @since 2.0.0
  886. * @access public
  887. * @var bool
  888. */
  889. var $is_preview = false;
  890. /**
  891. * Set if query returns a page.
  892. *
  893. * @since 1.5.0
  894. * @access public
  895. * @var bool
  896. */
  897. var $is_page = false;
  898. /**
  899. * Set if query is an archive list.
  900. *
  901. * @since 1.5.0
  902. * @access public
  903. * @var bool
  904. */
  905. var $is_archive = false;
  906. /**
  907. * Set if query is part of a date.
  908. *
  909. * @since 1.5.0
  910. * @access public
  911. * @var bool
  912. */
  913. var $is_date = false;
  914. /**
  915. * Set if query contains a year.
  916. *
  917. * @since 1.5.0
  918. * @access public
  919. * @var bool
  920. */
  921. var $is_year = false;
  922. /**
  923. * Set if query contains a month.
  924. *
  925. * @since 1.5.0
  926. * @access public
  927. * @var bool
  928. */
  929. var $is_month = false;
  930. /**
  931. * Set if query contains a day.
  932. *
  933. * @since 1.5.0
  934. * @access public
  935. * @var bool
  936. */
  937. var $is_day = false;
  938. /**
  939. * Set if query contains time.
  940. *
  941. * @since 1.5.0
  942. * @access public
  943. * @var bool
  944. */
  945. var $is_time = false;
  946. /**
  947. * Set if query contains an author.
  948. *
  949. * @since 1.5.0
  950. * @access public
  951. * @var bool
  952. */
  953. var $is_author = false;
  954. /**
  955. * Set if query contains category.
  956. *
  957. * @since 1.5.0
  958. * @access public
  959. * @var bool
  960. */
  961. var $is_category = false;
  962. /**
  963. * Set if query contains tag.
  964. *
  965. * @since 2.3.0
  966. * @access public
  967. * @var bool
  968. */
  969. var $is_tag = false;
  970. /**
  971. * Set if query contains taxonomy.
  972. *
  973. * @since 2.5.0
  974. * @access public
  975. * @var bool
  976. */
  977. var $is_tax = false;
  978. /**
  979. * Set if query was part of a search result.
  980. *
  981. * @since 1.5.0
  982. * @access public
  983. * @var bool
  984. */
  985. var $is_search = false;
  986. /**
  987. * Set if query is feed display.
  988. *
  989. * @since 1.5.0
  990. * @access public
  991. * @var bool
  992. */
  993. var $is_feed = false;
  994. /**
  995. * Set if query is comment feed display.
  996. *
  997. * @since 2.2.0
  998. * @access public
  999. * @var bool
  1000. */
  1001. var $is_comment_feed = false;
  1002. /**
  1003. * Set if query is trackback.
  1004. *
  1005. * @since 1.5.0
  1006. * @access public
  1007. * @var bool
  1008. */
  1009. var $is_trackback = false;
  1010. /**
  1011. * Set if query is blog homepage.
  1012. *
  1013. * @since 1.5.0
  1014. * @access public
  1015. * @var bool
  1016. */
  1017. var $is_home = false;
  1018. /**
  1019. * Set if query couldn't found anything.
  1020. *
  1021. * @since 1.5.0
  1022. * @access public
  1023. * @var bool
  1024. */
  1025. var $is_404 = false;
  1026. /**
  1027. * Set if query is within comments popup window.
  1028. *
  1029. * @since 1.5.0
  1030. * @access public
  1031. * @var bool
  1032. */
  1033. var $is_comments_popup = false;
  1034. /**
  1035. * Set if query is paged
  1036. *
  1037. * @since 1.5.0
  1038. * @access public
  1039. * @var bool
  1040. */
  1041. var $is_paged = false;
  1042. /**
  1043. * Set if query is part of administration page.
  1044. *
  1045. * @since 1.5.0
  1046. * @access public
  1047. * @var bool
  1048. */
  1049. var $is_admin = false;
  1050. /**
  1051. * Set if query is an attachment.
  1052. *
  1053. * @since 2.0.0
  1054. * @access public
  1055. * @var bool
  1056. */
  1057. var $is_attachment = false;
  1058. /**
  1059. * Set if is single, is a page, or is an attachment.
  1060. *
  1061. * @since 2.1.0
  1062. * @access public
  1063. * @var bool
  1064. */
  1065. var $is_singular = false;
  1066. /**
  1067. * Set if query is for robots.
  1068. *
  1069. * @since 2.1.0
  1070. * @access public
  1071. * @var bool
  1072. */
  1073. var $is_robots = false;
  1074. /**
  1075. * Set if query contains posts.
  1076. *
  1077. * Basically, the homepage if the option isn't set for the static homepage.
  1078. *
  1079. * @since 2.1.0
  1080. * @access public
  1081. * @var bool
  1082. */
  1083. var $is_posts_page = false;
  1084. /**
  1085. * Set if query is for a post type archive.
  1086. *
  1087. * @since 3.1.0
  1088. * @access public
  1089. * @var bool
  1090. */
  1091. var $is_post_type_archive = false;
  1092. /**
  1093. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1094. * whether we have to re-parse because something has changed
  1095. *
  1096. * @since 3.1.0
  1097. * @access private
  1098. */
  1099. var $query_vars_hash = false;
  1100. /**
  1101. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1102. * via pre_get_posts hooks.
  1103. *
  1104. * @since 3.1.1
  1105. * @access private
  1106. */
  1107. var $query_vars_changed = true;
  1108. /**
  1109. * Set if post thumbnails are cached
  1110. *
  1111. * @since 3.2.0
  1112. * @access public
  1113. * @var bool
  1114. */
  1115. var $thumbnails_cached = false;
  1116. /**
  1117. * Resets query flags to false.
  1118. *
  1119. * The query flags are what page info WordPress was able to figure out.
  1120. *
  1121. * @since 2.0.0
  1122. * @access private
  1123. */
  1124. function init_query_flags() {
  1125. $this->is_single = false;
  1126. $this->is_preview = false;
  1127. $this->is_page = false;
  1128. $this->is_archive = false;
  1129. $this->is_date = false;
  1130. $this->is_year = false;
  1131. $this->is_month = false;
  1132. $this->is_day = false;
  1133. $this->is_time = false;
  1134. $this->is_author = false;
  1135. $this->is_category = false;
  1136. $this->is_tag = false;
  1137. $this->is_tax = false;
  1138. $this->is_search = false;
  1139. $this->is_feed = false;
  1140. $this->is_comment_feed = false;
  1141. $this->is_trackback = false;
  1142. $this->is_home = false;
  1143. $this->is_404 = false;
  1144. $this->is_comments_popup = false;
  1145. $this->is_paged = false;
  1146. $this->is_admin = false;
  1147. $this->is_attachment = false;
  1148. $this->is_singular = false;
  1149. $this->is_robots = false;
  1150. $this->is_posts_page = false;
  1151. $this->is_post_type_archive = false;
  1152. }
  1153. /**
  1154. * Initiates object properties and sets default values.
  1155. *
  1156. * @since 1.5.0
  1157. * @access public
  1158. */
  1159. function init() {
  1160. unset($this->posts);
  1161. unset($this->query);
  1162. $this->query_vars = array();
  1163. unset($this->queried_object);
  1164. unset($this->queried_object_id);
  1165. $this->post_count = 0;
  1166. $this->current_post = -1;
  1167. $this->in_the_loop = false;
  1168. unset( $this->request );
  1169. unset( $this->post );
  1170. unset( $this->comments );
  1171. unset( $this->comment );
  1172. $this->comment_count = 0;
  1173. $this->current_comment = -1;
  1174. $this->found_posts = 0;
  1175. $this->max_num_pages = 0;
  1176. $this->max_num_comment_pages = 0;
  1177. $this->init_query_flags();
  1178. }
  1179. /**
  1180. * Reparse the query vars.
  1181. *
  1182. * @since 1.5.0
  1183. * @access public
  1184. */
  1185. function parse_query_vars() {
  1186. $this->parse_query();
  1187. }
  1188. /**
  1189. * Fills in the query variables, which do not exist within the parameter.
  1190. *
  1191. * @since 2.1.0
  1192. * @access public
  1193. *
  1194. * @param array $array Defined query variables.
  1195. * @return array Complete query variables with undefined ones filled in empty.
  1196. */
  1197. function fill_query_vars($array) {
  1198. $keys = array(
  1199. 'error'
  1200. , 'm'
  1201. , 'p'
  1202. , 'post_parent'
  1203. , 'subpost'
  1204. , 'subpost_id'
  1205. , 'attachment'
  1206. , 'attachment_id'
  1207. , 'name'
  1208. , 'static'
  1209. , 'pagename'
  1210. , 'page_id'
  1211. , 'second'
  1212. , 'minute'
  1213. , 'hour'
  1214. , 'day'
  1215. , 'monthnum'
  1216. , 'year'
  1217. , 'w'
  1218. , 'category_name'
  1219. , 'tag'
  1220. , 'cat'
  1221. , 'tag_id'
  1222. , 'author_name'
  1223. , 'feed'
  1224. , 'tb'
  1225. , 'paged'
  1226. , 'comments_popup'
  1227. , 'meta_key'
  1228. , 'meta_value'
  1229. , 'preview'
  1230. , 's'
  1231. , 'sentence'
  1232. , 'fields'
  1233. );
  1234. foreach ( $keys as $key ) {
  1235. if ( !isset($array[$key]) )
  1236. $array[$key] = '';
  1237. }
  1238. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1239. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1240. foreach ( $array_keys as $key ) {
  1241. if ( !isset($array[$key]) )
  1242. $array[$key] = array();
  1243. }
  1244. return $array;
  1245. }
  1246. /**
  1247. * Parse a query string and set query type booleans.
  1248. *
  1249. * @since 1.5.0
  1250. * @access public
  1251. *
  1252. * @param string|array $query Optional query.
  1253. */
  1254. function parse_query( $query = '' ) {
  1255. if ( ! empty( $query ) ) {
  1256. $this->init();
  1257. $this->query = $this->query_vars = wp_parse_args( $query );
  1258. } elseif ( ! isset( $this->query ) ) {
  1259. $this->query = $this->query_vars;
  1260. }
  1261. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1262. $qv = &$this->query_vars;
  1263. $this->query_vars_changed = true;
  1264. if ( ! empty($qv['robots']) )
  1265. $this->is_robots = true;
  1266. $qv['p'] = absint($qv['p']);
  1267. $qv['page_id'] = absint($qv['page_id']);
  1268. $qv['year'] = absint($qv['year']);
  1269. $qv['monthnum'] = absint($qv['monthnum']);
  1270. $qv['day'] = absint($qv['day']);
  1271. $qv['w'] = absint($qv['w']);
  1272. $qv['m'] = absint($qv['m']);
  1273. $qv['paged'] = absint($qv['paged']);
  1274. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1275. $qv['pagename'] = trim( $qv['pagename'] );
  1276. $qv['name'] = trim( $qv['name'] );
  1277. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1278. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1279. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1280. // Compat. Map subpost to attachment.
  1281. if ( '' != $qv['subpost'] )
  1282. $qv['attachment'] = $qv['subpost'];
  1283. if ( '' != $qv['subpost_id'] )
  1284. $qv['attachment_id'] = $qv['subpost_id'];
  1285. $qv['attachment_id'] = absint($qv['attachment_id']);
  1286. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1287. $this->is_single = true;
  1288. $this->is_attachment = true;
  1289. } elseif ( '' != $qv['name'] ) {
  1290. $this->is_single = true;
  1291. } elseif ( $qv['p'] ) {
  1292. $this->is_single = true;
  1293. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1294. // If year, month, day, hour, minute, and second are set, a single
  1295. // post is being queried.
  1296. $this->is_single = true;
  1297. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1298. $this->is_page = true;
  1299. $this->is_single = false;
  1300. } else {
  1301. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1302. if ( !empty($qv['s']) ) {
  1303. $this->is_search = true;
  1304. }
  1305. if ( '' !== $qv['second'] ) {
  1306. $this->is_time = true;
  1307. $this->is_date = true;
  1308. }
  1309. if ( '' !== $qv['minute'] ) {
  1310. $this->is_time = true;
  1311. $this->is_date = true;
  1312. }
  1313. if ( '' !== $qv['hour'] ) {
  1314. $this->is_time = true;
  1315. $this->is_date = true;
  1316. }
  1317. if ( $qv['day'] ) {
  1318. if ( ! $this->is_date ) {
  1319. $this->is_day = true;
  1320. $this->is_date = true;
  1321. }
  1322. }
  1323. if ( $qv['monthnum'] ) {
  1324. if ( ! $this->is_date ) {
  1325. $this->is_month = true;
  1326. $this->is_date = true;
  1327. }
  1328. }
  1329. if ( $qv['year'] ) {
  1330. if ( ! $this->is_date ) {
  1331. $this->is_year = true;
  1332. $this->is_date = true;
  1333. }
  1334. }
  1335. if ( $qv['m'] ) {
  1336. $this->is_date = true;
  1337. if ( strlen($qv['m']) > 9 ) {
  1338. $this->is_time = true;
  1339. } else if ( strlen($qv['m']) > 7 ) {
  1340. $this->is_day = true;
  1341. } else if ( strlen($qv['m']) > 5 ) {
  1342. $this->is_month = true;
  1343. } else {
  1344. $this->is_year = true;
  1345. }
  1346. }
  1347. if ( '' != $qv['w'] ) {
  1348. $this->is_date = true;
  1349. }
  1350. $this->query_vars_hash = false;
  1351. $this->parse_tax_query( $qv );
  1352. foreach ( $this->tax_query->queries as $tax_query ) {
  1353. if ( 'NOT IN' != $tax_query['operator'] ) {
  1354. switch ( $tax_query['taxonomy'] ) {
  1355. case 'category':
  1356. $this->is_category = true;
  1357. break;
  1358. case 'post_tag':
  1359. $this->is_tag = true;
  1360. break;
  1361. default:
  1362. $this->is_tax = true;
  1363. }
  1364. }
  1365. }
  1366. unset( $tax_query );
  1367. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1368. $this->is_author = false;
  1369. } else {
  1370. $this->is_author = true;
  1371. }
  1372. if ( '' != $qv['author_name'] )
  1373. $this->is_author = true;
  1374. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1375. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1376. if ( ! empty( $post_type_obj->has_archive ) )
  1377. $this->is_post_type_archive = true;
  1378. }
  1379. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1380. $this->is_archive = true;
  1381. }
  1382. if ( '' != $qv['feed'] )
  1383. $this->is_feed = true;
  1384. if ( '' != $qv['tb'] )
  1385. $this->is_trackback = true;
  1386. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1387. $this->is_paged = true;
  1388. if ( '' != $qv['comments_popup'] )
  1389. $this->is_comments_popup = true;
  1390. // if we're previewing inside the write screen
  1391. if ( '' != $qv['preview'] )
  1392. $this->is_preview = true;
  1393. if ( is_admin() )
  1394. $this->is_admin = true;
  1395. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1396. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1397. $qv['withcomments'] = 1;
  1398. }
  1399. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1400. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1401. $this->is_comment_feed = true;
  1402. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
  1403. $this->is_home = true;
  1404. // Correct is_* for page_on_front and page_for_posts
  1405. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1406. $_query = wp_parse_args($this->query);
  1407. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1408. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1409. unset($_query['pagename']);
  1410. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1411. $this->is_page = true;
  1412. $this->is_home = false;
  1413. $qv['page_id'] = get_option('page_on_front');
  1414. // Correct <!--nextpage--> for page_on_front
  1415. if ( !empty($qv['paged']) ) {
  1416. $qv['page'] = $qv['paged'];
  1417. unset($qv['paged']);
  1418. }
  1419. }
  1420. }
  1421. if ( '' != $qv['pagename'] ) {
  1422. $this->queried_object =& get_page_by_path($qv['pagename']);
  1423. if ( !empty($this->queried_object) )
  1424. $this->queried_object_id = (int) $this->queried_object->ID;
  1425. else
  1426. unset($this->queried_object);
  1427. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1428. $this->is_page = false;
  1429. $this->is_home = true;
  1430. $this->is_posts_page = true;
  1431. }
  1432. }
  1433. if ( $qv['page_id'] ) {
  1434. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1435. $this->is_page = false;
  1436. $this->is_home = true;
  1437. $this->is_posts_page = true;
  1438. }
  1439. }
  1440. if ( !empty($qv['post_type']) ) {
  1441. if ( is_array($qv['post_type']) )
  1442. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1443. else
  1444. $qv['post_type'] = sanitize_key($qv['post_type']);
  1445. }
  1446. if ( ! empty( $qv['post_status'] ) ) {
  1447. if ( is_array( $qv['post_status'] ) )
  1448. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1449. else
  1450. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1451. }
  1452. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1453. $this->is_comment_feed = false;
  1454. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1455. // Done correcting is_* for page_on_front and page_for_posts
  1456. if ( '404' == $qv['error'] )
  1457. $this->set_404();
  1458. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1459. $this->query_vars_changed = false;
  1460. do_action_ref_array('parse_query', array(&$this));
  1461. }
  1462. /*
  1463. * Parses various taxonomy related query vars.
  1464. *
  1465. * @access protected
  1466. * @since 3.1.0
  1467. *
  1468. * @param array &$q The query variables
  1469. */
  1470. function parse_tax_query( &$q ) {
  1471. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1472. $tax_query = $q['tax_query'];
  1473. } else {
  1474. $tax_query = array();
  1475. }
  1476. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1477. $tax_query[] = array(
  1478. 'taxonomy' => $q['taxonomy'],
  1479. 'terms' => array( $q['term'] ),
  1480. 'field' => 'slug',
  1481. );
  1482. }
  1483. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1484. if ( 'post_tag' == $taxonomy )
  1485. continue; // Handled further down in the $q['tag'] block
  1486. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1487. $tax_query_defaults = array(
  1488. 'taxonomy' => $taxonomy,
  1489. 'field' => 'slug',
  1490. );
  1491. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1492. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1493. }
  1494. $term = $q[$t->query_var];
  1495. if ( strpos($term, '+') !== false ) {
  1496. $terms = preg_split( '/[+]+/', $term );
  1497. foreach ( $terms as $term ) {
  1498. $tax_query[] = array_merge( $tax_query_defaults, array(
  1499. 'terms' => array( $term )
  1500. ) );
  1501. }
  1502. } else {
  1503. $tax_query[] = array_merge( $tax_query_defaults, array(
  1504. 'terms' => preg_split( '/[,]+/', $term )
  1505. ) );
  1506. }
  1507. }
  1508. }
  1509. // Category stuff
  1510. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1511. $q['cat'] = ''.urldecode($q['cat']).'';
  1512. $q['cat'] = addslashes_gpc($q['cat']);
  1513. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1514. $q['cat'] = '';
  1515. $req_cats = array();
  1516. foreach ( (array) $cat_array as $cat ) {
  1517. $cat = intval($cat);
  1518. $req_cats[] = $cat;
  1519. $in = ($cat > 0);
  1520. $cat = abs($cat);
  1521. if ( $in ) {
  1522. $q['category__in'][] = $cat;
  1523. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1524. } else {
  1525. $q['category__not_in'][] = $cat;
  1526. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1527. }
  1528. }
  1529. $q['cat'] = implode(',', $req_cats);
  1530. }
  1531. if ( !empty($q['category__in']) ) {
  1532. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1533. $tax_query[] = array(
  1534. 'taxonomy' => 'category',
  1535. 'terms' => $q['category__in'],
  1536. 'field' => 'term_id',
  1537. 'include_children' => false
  1538. );
  1539. }
  1540. if ( !empty($q['category__not_in']) ) {
  1541. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1542. $tax_query[] = array(
  1543. 'taxonomy' => 'category',
  1544. 'terms' => $q['category__not_in'],
  1545. 'operator' => 'NOT IN',
  1546. 'include_children' => false
  1547. );
  1548. }
  1549. if ( !empty($q['category__and']) ) {
  1550. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1551. $tax_query[] = array(
  1552. 'taxonomy' => 'category',
  1553. 'terms' => $q['category__and'],
  1554. 'field' => 'term_id',
  1555. 'operator' => 'AND',
  1556. 'include_children' => false
  1557. );
  1558. }
  1559. // Tag stuff
  1560. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1561. if ( strpos($q['tag'], ',') !== false ) {
  1562. $tags = preg_split('/[,\s]+/', $q['tag']);
  1563. foreach ( (array) $tags as $tag ) {
  1564. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1565. $q['tag_slug__in'][] = $tag;
  1566. }
  1567. } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  1568. $tags = preg_split('/[+\s]+/', $q['tag']);
  1569. foreach ( (array) $tags as $tag ) {
  1570. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1571. $q['tag_slug__and'][] = $tag;
  1572. }
  1573. } else {
  1574. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1575. $q['tag_slug__in'][] = $q['tag'];
  1576. }
  1577. }
  1578. if ( !empty($q['tag_id']) ) {
  1579. $q['tag_id'] = absint( $q['tag_id'] );
  1580. $tax_query[] = array(
  1581. 'taxonomy' => 'post_tag',
  1582. 'terms' => $q['tag_id']
  1583. );
  1584. }
  1585. if ( !empty($q['tag__in']) ) {
  1586. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1587. $tax_query[] = array(
  1588. 'taxonomy' => 'post_tag',
  1589. 'terms' => $q['tag__in']
  1590. );
  1591. }
  1592. if ( !empty($q['tag__not_in']) ) {
  1593. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1594. $tax_query[] = array(
  1595. 'taxonomy' => 'post_tag',
  1596. 'terms' => $q['tag__not_in'],
  1597. 'operator' => 'NOT IN'
  1598. );
  1599. }
  1600. if ( !empty($q['tag__and']) ) {
  1601. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1602. $tax_query[] = array(
  1603. 'taxonomy' => 'post_tag',
  1604. 'terms' => $q['tag__and'],
  1605. 'operator' => 'AND'
  1606. );
  1607. }
  1608. if ( !empty($q['tag_slug__in']) ) {
  1609. $q['tag_slug__in'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__in'] ) );
  1610. $tax_query[] = array(
  1611. 'taxonomy' => 'post_tag',
  1612. 'terms' => $q['tag_slug__in'],
  1613. 'field' => 'slug'
  1614. );
  1615. }
  1616. if ( !empty($q['tag_slug__and']) ) {
  1617. $q['tag_slug__and'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__and'] ) );
  1618. $tax_query[] = array(
  1619. 'taxonomy' => 'post_tag',
  1620. 'terms' => $q['tag_slug__and'],
  1621. 'field' => 'slug',
  1622. 'operator' => 'AND'
  1623. );
  1624. }
  1625. $this->tax_query = new WP_Tax_Query( $tax_query );
  1626. }
  1627. /**
  1628. * Sets the 404 property and saves whether query is feed.
  1629. *
  1630. * @since 2.0.0
  1631. * @access public
  1632. */
  1633. function set_404() {
  1634. $is_feed = $this->is_feed;
  1635. $this->init_query_flags();
  1636. $this->is_404 = true;
  1637. $this->is_feed = $is_feed;
  1638. }
  1639. /**
  1640. * Retrieve query variable.
  1641. *
  1642. * @since 1.5.0
  1643. * @access public
  1644. *
  1645. * @param string $query_var Query variable key.
  1646. * @return mixed
  1647. */
  1648. function get($query_var) {
  1649. if ( isset($this->query_vars[$query_var]) )
  1650. return $this->query_vars[$query_var];
  1651. return '';
  1652. }
  1653. /**
  1654. * Set query variable.
  1655. *
  1656. * @since 1.5.0
  1657. * @access public
  1658. *
  1659. * @param string $query_var Query variable key.
  1660. * @param mixed $value Query variable value.
  1661. */
  1662. function set($query_var, $value) {
  1663. $this->query_vars[$query_var] = $value;
  1664. }
  1665. /**
  1666. * Retrieve the posts based on query variables.
  1667. *
  1668. * There are a few filters and actions that can be used to modify the post
  1669. * database query.
  1670. *
  1671. * @since 1.5.0
  1672. * @access public
  1673. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1674. *
  1675. * @return array List of posts.
  1676. */
  1677. function &get_posts() {
  1678. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1679. $this->parse_query();
  1680. do_action_ref_array('pre_get_posts', array(&$this));
  1681. // Shorthand.
  1682. $q = &$this->query_vars;
  1683. // Fill again in case pre_get_posts unset some vars.
  1684. $q = $this->fill_query_vars($q);
  1685. // Parse meta query
  1686. $this->meta_query = new WP_Meta_Query();
  1687. $this->meta_query->parse_query_vars( $q );
  1688. // Set a flag if a pre_get_posts hook changed the query vars.
  1689. $hash = md5( serialize( $this->query_vars ) );
  1690. if ( $hash != $this->query_vars_hash ) {
  1691. $this->query_vars_changed = true;
  1692. $this->query_vars_hash = $hash;
  1693. }
  1694. unset($hash);
  1695. // First let's clear some variables
  1696. $distinct = '';
  1697. $whichauthor = '';
  1698. $whichmimetype = '';
  1699. $where = '';
  1700. $limits = '';
  1701. $join = '';
  1702. $search = '';
  1703. $groupby = '';
  1704. $fields = '';
  1705. $post_status_join = false;
  1706. $page = 1;
  1707. if ( isset( $q['caller_get_posts'] ) ) {
  1708. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1709. if ( !isset( $q['ignore_sticky_posts'] ) )
  1710. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1711. }
  1712. if ( !isset( $q['ignore_sticky_posts'] ) )
  1713. $q['ignore_sticky_posts'] = false;
  1714. if ( !isset($q['suppress_filters']) )
  1715. $q['suppress_filters'] = false;
  1716. if ( !isset($q['cache_results']) ) {
  1717. if ( $_wp_using_ext_object_cache )
  1718. $q['cache_results'] = false;
  1719. else
  1720. $q['cache_results'] = true;
  1721. }
  1722. if ( !isset($q['update_post_term_cache']) )
  1723. $q['update_post_term_cache'] = true;
  1724. if ( !isset($q['update_post_meta_cache']) )
  1725. $q['update_post_meta_cache'] = true;
  1726. if ( !isset($q['post_type']) ) {
  1727. if ( $this->is_search )
  1728. $q['post_type'] = 'any';
  1729. else
  1730. $q['post_type'] = '';
  1731. }
  1732. $post_type = $q['post_type'];
  1733. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1734. $q['posts_per_page'] = get_option('posts_per_page');
  1735. if ( isset($q['showposts']) && $q['showposts'] ) {
  1736. $q['showposts'] = (int) $q['showposts'];
  1737. $q['posts_per_page'] = $q['showposts'];
  1738. }
  1739. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1740. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1741. if ( !isset($q['nopaging']) ) {
  1742. if ( $q['posts_per_page'] == -1 ) {
  1743. $q['nopaging'] = true;
  1744. } else {
  1745. $q['nopaging'] = false;
  1746. }
  1747. }
  1748. if ( $this->is_feed ) {
  1749. $q['posts_per_page'] = get_option('posts_per_rss');
  1750. $q['nopaging'] = false;
  1751. }
  1752. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1753. if ( $q['posts_per_page'] < -1 )
  1754. $q['posts_per_page'] = abs($q['posts_per_page']);
  1755. else if ( $q['posts_per_page'] == 0 )
  1756. $q['posts_per_page'] = 1;
  1757. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1758. $q['comments_per_page'] = get_option('comments_per_page');
  1759. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1760. $this->is_page = true;
  1761. $this->is_home = false;
  1762. $q['page_id'] = get_option('page_on_front');
  1763. }
  1764. if ( isset($q['page']) ) {
  1765. $q['page'] = trim($q['page'], '/');
  1766. $q['page'] = absint($q['page']);
  1767. }
  1768. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1769. if ( isset($q['no_found_rows']) )
  1770. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1771. else
  1772. $q['no_found_rows'] = false;
  1773. switch ( $q['fields'] ) {
  1774. case 'ids':
  1775. $fields = "$wpdb->posts.ID";
  1776. break;
  1777. case 'id=>parent':
  1778. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1779. break;
  1780. default:
  1781. $fields = "$wpdb->posts.*";
  1782. }
  1783. // If a month is specified in the querystring, load that month
  1784. if ( $q['m'] ) {
  1785. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1786. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1787. if ( strlen($q['m']) > 5 )
  1788. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1789. if ( strlen($q['m']) > 7 )
  1790. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1791. if ( strlen($q['m']) > 9 )
  1792. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1793. if ( strlen($q['m']) > 11 )
  1794. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1795. if ( strlen($q['m']) > 13 )
  1796. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1797. }
  1798. if ( '' !== $q['hour'] )
  1799. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1800. if ( '' !== $q['minute'] )
  1801. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1802. if ( '' !== $q['second'] )
  1803. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1804. if ( $q['year'] )
  1805. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1806. if ( $q['monthnum'] )
  1807. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1808. if ( $q['day'] )
  1809. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1810. // If we've got a post_type AND its not "any" post_type.
  1811. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1812. foreach ( (array)$q['post_type'] as $_post_type ) {
  1813. $ptype_obj = get_post_type_object($_post_type);
  1814. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1815. continue;
  1816. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1817. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1818. $q['name'] = $q[ $ptype_obj->query_var ];
  1819. } else {
  1820. // Hierarchical post_types will operate through the
  1821. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1822. $q['name'] = '';
  1823. }
  1824. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1825. break;
  1826. } //end foreach
  1827. unset($ptype_obj);
  1828. }
  1829. if ( '' != $q['name'] ) {
  1830. $q['name'] = sanitize_title_for_query( $q['name'] );
  1831. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1832. } elseif ( '' != $q['pagename'] ) {
  1833. if ( isset($this->queried_object_id) ) {
  1834. $reqpage = $this->queried_object_id;
  1835. } else {
  1836. if ( 'page' != $q['post_type'] ) {
  1837. foreach ( (array)$q['post_type'] as $_post_type ) {
  1838. $ptype_obj = get_post_type_object($_post_type);
  1839. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1840. continue;
  1841. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1842. if ( $reqpage )
  1843. break;
  1844. }
  1845. unset($ptype_obj);
  1846. } else {
  1847. $reqpage = get_page_by_path($q['pagename']);
  1848. }
  1849. if ( !empty($reqpage) )
  1850. $reqpage = $reqpage->ID;
  1851. else
  1852. $reqpage = 0;
  1853. }
  1854. $page_for_posts = get_option('page_for_posts');
  1855. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1856. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1857. $q['name'] = $q['pagename'];
  1858. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1859. $reqpage_obj = get_page($reqpage);
  1860. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1861. $this->is_attachment = true;
  1862. $post_type = $q['post_type'] = 'attachment';
  1863. $this->is_page = true;
  1864. $q['attachment_id'] = $reqpage;
  1865. }
  1866. }
  1867. } elseif ( '' != $q['attachment'] ) {
  1868. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1869. $q['name'] = $q['attachment'];
  1870. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1871. }
  1872. if ( $q['w'] )
  1873. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1874. if ( intval($q['comments_popup']) )
  1875. $q['p'] = absint($q['comments_popup']);
  1876. // If an attachment is requested by number, let it supercede any post number.
  1877. if ( $q['attachment_id'] )
  1878. $q['p'] = absint($q['attachment_id']);
  1879. // If a post number is specified, load that post
  1880. if ( $q['p'] ) {
  1881. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1882. } elseif ( $q['post__in'] ) {
  1883. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1884. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1885. } elseif ( $q['post__not_in'] ) {
  1886. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1887. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1888. }
  1889. if ( is_numeric($q['post_parent']) )
  1890. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  1891. if ( $q['page_id'] ) {
  1892. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1893. $q['p'] = $q['page_id'];
  1894. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1895. }
  1896. }
  1897. // If a search pattern is specified, load the posts that match
  1898. if ( !empty($q['s']) ) {
  1899. // added slashes screw with quote grouping when done early, so done later
  1900. $q['s'] = stripslashes($q['s']);
  1901. if ( !empty($q['sentence']) ) {
  1902. $q['search_terms'] = array($q['s']);
  1903. } else {
  1904. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
  1905. $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1906. }
  1907. $n = !empty($q['exact']) ? '' : '%';
  1908. $searchand = '';
  1909. foreach( (array) $q['search_terms'] as $term ) {
  1910. $term = esc_sql( like_escape( $term ) );
  1911. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1912. $searchand = ' AND ';
  1913. }
  1914. if ( !empty($search) ) {
  1915. $search = " AND ({$search}) ";
  1916. if ( !is_user_logged_in() )
  1917. $search .= " AND ($wpdb->posts.post_password = '') ";
  1918. }
  1919. }
  1920. // Allow plugins to contextually add/remove/modify the search section of the database query
  1921. $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  1922. // Taxonomies
  1923. if ( !$this->is_singular ) {
  1924. $this->parse_tax_query( $q );
  1925. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  1926. $join .= $clauses['join'];
  1927. $where .= $clauses['where'];
  1928. }
  1929. if ( $this->is_tax ) {
  1930. if ( empty($post_type) ) {
  1931. $post_type = 'any';
  1932. $post_status_join = true;
  1933. } elseif ( in_array('attachment', (array) $post_type) ) {
  1934. $post_status_join = true;
  1935. }
  1936. }
  1937. // Back-compat
  1938. if ( !empty($this->tax_query->queries) ) {
  1939. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  1940. if ( !empty( $tax_query_in_and ) ) {
  1941. if ( !isset( $q['taxonomy'] ) ) {
  1942. foreach ( $tax_query_in_and as $a_tax_query ) {
  1943. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  1944. $q['taxonomy'] = $a_tax_query['taxonomy'];
  1945. if ( 'slug' == $a_tax_query['field'] )
  1946. $q['term'] = $a_tax_query['terms'][0];
  1947. else
  1948. $q['term_id'] = $a_tax_query['terms'][0];
  1949. break;
  1950. }
  1951. }
  1952. }
  1953. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  1954. if ( !empty( $cat_query ) ) {
  1955. $cat_query = reset( $cat_query );
  1956. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  1957. if ( $the_cat ) {
  1958. $this->set( 'cat', $the_cat->term_id );
  1959. $this->set( 'category_name', $the_cat->slug );
  1960. }
  1961. unset( $the_cat );
  1962. }
  1963. unset( $cat_query );
  1964. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  1965. if ( !empty( $tag_query ) ) {
  1966. $tag_query = reset( $tag_query );
  1967. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  1968. if ( $the_tag ) {
  1969. $this->set( 'tag_id', $the_tag->term_id );
  1970. }
  1971. unset( $the_tag );
  1972. }
  1973. unset( $tag_query );
  1974. }
  1975. }
  1976. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  1977. $groupby = "{$wpdb->posts}.ID";
  1978. }
  1979. // Author/user stuff
  1980. if ( empty($q['author']) || ($q['author'] == '0') ) {
  1981. $whichauthor = '';
  1982. } else {
  1983. $q['author'] = (string)urldecode($q['author']);
  1984. $q['author'] = addslashes_gpc($q['author']);
  1985. if ( strpos($q['author'], '-') !== false ) {
  1986. $eq = '!=';
  1987. $andor = 'AND';
  1988. $q['author'] = explode('-', $q['author']);
  1989. $q['author'] = (string)absint($q['author'][1]);
  1990. } else {
  1991. $eq = '=';
  1992. $andor = 'OR';
  1993. }
  1994. $author_array = preg_split('/[,\s]+/', $q['author']);
  1995. $_author_array = array();
  1996. foreach ( $author_array as $key => $_author )
  1997. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  1998. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  1999. unset($author_array, $_author_array);
  2000. }
  2001. // Author stuff for nice URLs
  2002. if ( '' != $q['author_name'] ) {
  2003. if ( strpos($q['author_name'], '/') !== false ) {
  2004. $q['author_name'] = explode('/', $q['author_name']);
  2005. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2006. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2007. } else {
  2008. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
  2009. }
  2010. }
  2011. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2012. $q['author'] = get_user_by('slug', $q['author_name']);
  2013. if ( $q['author'] )
  2014. $q['author'] = $q['author']->ID;
  2015. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2016. }
  2017. // MIME-Type stuff for attachment browsing
  2018. if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
  2019. $table_alias = $post_status_join ? $wpdb->posts : '';
  2020. $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
  2021. }
  2022. $where .= $search . $whichauthor . $whichmimetype;
  2023. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2024. $q['order'] = 'DESC';
  2025. // Order by
  2026. if ( empty($q['orderby']) ) {
  2027. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2028. } elseif ( 'none' == $q['orderby'] ) {
  2029. $orderby = '';
  2030. } else {
  2031. // Used to filter values
  2032. $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2033. if ( !empty($q['meta_key']) ) {
  2034. $allowed_keys[] = $q['meta_key'];
  2035. $allowed_keys[] = 'meta_value';
  2036. $allowed_keys[] = 'meta_value_num';
  2037. }
  2038. $q['orderby'] = urldecode($q['orderby']);
  2039. $q['orderby'] = addslashes_gpc($q['orderby']);
  2040. $orderby_array = array();
  2041. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2042. // Only allow certain values for safety
  2043. if ( ! in_array($orderby, $allowed_keys) )
  2044. continue;
  2045. switch ( $orderby ) {
  2046. case 'menu_order':
  2047. break;
  2048. case 'ID':
  2049. $orderby = "$wpdb->posts.ID";
  2050. break;
  2051. case 'rand':
  2052. $orderby = 'RAND()';
  2053. break;
  2054. case $q['meta_key']:
  2055. case 'meta_value':
  2056. $orderby = "$wpdb->postmeta.meta_value";
  2057. break;
  2058. case 'meta_value_num':
  2059. $orderby = "$wpdb->postmeta.meta_value+0";
  2060. break;
  2061. case 'comment_count':
  2062. $orderby = "$wpdb->posts.comment_count";
  2063. break;
  2064. default:
  2065. $orderby = "$wpdb->posts.post_" . $orderby;
  2066. }
  2067. $orderby_array[] = $orderby;
  2068. }
  2069. $orderby = implode( ',', $orderby_array );
  2070. if ( empty( $orderby ) )
  2071. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2072. else
  2073. $orderby .= " {$q['order']}";
  2074. }
  2075. if ( is_array( $post_type ) ) {
  2076. $post_type_cap = 'multiple_post_type';
  2077. } else {
  2078. $post_type_object = get_post_type_object( $post_type );
  2079. if ( empty( $post_type_object ) )
  2080. $post_type_cap = $post_type;
  2081. }
  2082. $exclude_post_types = '';
  2083. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2084. if ( ! empty( $in_search_post_types ) )
  2085. $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  2086. if ( 'any' == $post_type ) {
  2087. $where .= $exclude_post_types;
  2088. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2089. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2090. } elseif ( ! empty( $post_type ) ) {
  2091. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2092. $post_type_object = get_post_type_object ( $post_type );
  2093. } elseif ( $this->is_attachment ) {
  2094. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2095. $post_type_object = get_post_type_object ( 'attachment' );
  2096. } elseif ( $this->is_page ) {
  2097. $where .= " AND $wpdb->posts.post_type = 'page'";
  2098. $post_type_object = get_post_type_object ( 'page' );
  2099. } else {
  2100. $where .= " AND $wpdb->posts.post_type = 'post'";
  2101. $post_type_object = get_post_type_object ( 'post' );
  2102. }
  2103. if ( ! empty( $post_type_object ) ) {
  2104. $edit_cap = $post_type_object->cap->edit_post;
  2105. $read_cap = $post_type_object->cap->read_post;
  2106. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2107. $read_private_cap = $post_type_object->cap->read_private_posts;
  2108. } else {
  2109. $edit_cap = 'edit_' . $post_type_cap;
  2110. $read_cap = 'read_' . $post_type_cap;
  2111. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2112. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2113. }
  2114. if ( ! empty( $q['post_status'] ) ) {
  2115. $statuswheres = array();
  2116. $q_status = $q['post_status'];
  2117. if ( ! is_array( $q_status ) )
  2118. $q_status = explode(',', $q_status);
  2119. $r_status = array();
  2120. $p_status = array();
  2121. $e_status = array();
  2122. if ( in_array('any', $q_status) ) {
  2123. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2124. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2125. } else {
  2126. foreach ( get_post_stati() as $status ) {
  2127. if ( in_array( $status, $q_status ) ) {
  2128. if ( 'private' == $status )
  2129. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2130. else
  2131. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2132. }
  2133. }
  2134. }
  2135. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2136. $r_status = array_merge($r_status, $p_status);
  2137. unset($p_status);
  2138. }
  2139. if ( !empty($e_status) ) {
  2140. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2141. }
  2142. if ( !empty($r_status) ) {
  2143. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2144. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2145. else
  2146. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2147. }
  2148. if ( !empty($p_status) ) {
  2149. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2150. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2151. else
  2152. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2153. }
  2154. if ( $post_status_join ) {
  2155. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2156. foreach ( $statuswheres as $index => $statuswhere )
  2157. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2158. }
  2159. foreach ( $statuswheres as $statuswhere )
  2160. $where .= " AND $statuswhere";
  2161. } elseif ( !$this->is_singular ) {
  2162. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2163. // Add public states.
  2164. $public_states = get_post_stati( array('public' => true) );
  2165. foreach ( (array) $public_states as $state ) {
  2166. if ( 'publish' == $state ) // Publish is hard-coded above.
  2167. continue;
  2168. $where .= " OR $wpdb->posts.post_status = '$state'";
  2169. }
  2170. if ( is_admin() ) {
  2171. // Add protected states that should show in the admin all list.
  2172. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2173. foreach ( (array) $admin_all_states as $state )
  2174. $where .= " OR $wpdb->posts.post_status = '$state'";
  2175. }
  2176. if ( is_user_logged_in() ) {
  2177. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2178. $private_states = get_post_stati( array('private' => true) );
  2179. foreach ( (array) $private_states as $state )
  2180. $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = '$state'";
  2181. }
  2182. $where .= ')';
  2183. }
  2184. if ( !empty( $this->meta_query->queries ) ) {
  2185. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2186. $join .= $clauses['join'];
  2187. $where .= $clauses['where'];
  2188. }
  2189. // Apply filters on where and join prior to paging so that any
  2190. // manipulations to them are reflected in the paging by day queries.
  2191. if ( !$q['suppress_filters'] ) {
  2192. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2193. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2194. }
  2195. // Paging
  2196. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2197. $page = absint($q['paged']);
  2198. if ( empty($page) )
  2199. $page = 1;
  2200. if ( empty($q['offset']) ) {
  2201. $pgstrt = '';
  2202. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2203. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2204. } else { // we're ignoring $page and using 'offset'
  2205. $q['offset'] = absint($q['offset']);
  2206. $pgstrt = $q['offset'] . ', ';
  2207. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2208. }
  2209. }
  2210. // Comments feeds
  2211. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2212. if ( $this->is_archive || $this->is_search ) {
  2213. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2214. $cwhere = "WHERE comment_approved = '1' $where";
  2215. $cgroupby = "$wpdb->comments.comment_id";
  2216. } else { // Other non singular e.g. front
  2217. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2218. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2219. $cgroupby = '';
  2220. }
  2221. if ( !$q['suppress_filters'] ) {
  2222. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2223. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2224. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2225. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2226. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2227. }
  2228. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2229. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2230. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2231. $this->comment_count = count($this->comments);
  2232. $post_ids = array();
  2233. foreach ( $this->comments as $comment )
  2234. $post_ids[] = (int) $comment->comment_post_ID;
  2235. $post_ids = join(',', $post_ids);
  2236. $join = '';
  2237. if ( $post_ids )
  2238. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2239. else
  2240. $where = "AND 0";
  2241. }
  2242. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2243. // Apply post-paging filters on where and join. Only plugins that
  2244. // manipulate paging queries should use these hooks.
  2245. if ( !$q['suppress_filters'] ) {
  2246. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2247. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2248. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2249. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2250. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2251. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2252. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2253. // Filter all clauses at once, for convenience
  2254. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2255. foreach ( $pieces as $piece )
  2256. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2257. }
  2258. // Announce current selection parameters. For use by caching plugins.
  2259. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2260. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2261. if ( !$q['suppress_filters'] ) {
  2262. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2263. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2264. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2265. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2266. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2267. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2268. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2269. // Filter all clauses at once, for convenience
  2270. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2271. foreach ( $pieces as $piece )
  2272. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2273. }
  2274. if ( ! empty($groupby) )
  2275. $groupby = 'GROUP BY ' . $groupby;
  2276. if ( !empty( $orderby ) )
  2277. $orderby = 'ORDER BY ' . $orderby;
  2278. $found_rows = '';
  2279. if ( !$q['no_found_rows'] && !empty($limits) )
  2280. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2281. $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2282. if ( !$q['suppress_filters'] )
  2283. $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
  2284. if ( 'ids' == $q['fields'] ) {
  2285. $this->posts = $wpdb->get_col($this->request);
  2286. return $this->posts;
  2287. }
  2288. if ( 'id=>parent' == $q['fields'] ) {
  2289. $this->posts = $wpdb->get_results($this->request);
  2290. $r = array();
  2291. foreach ( $this->posts as $post )
  2292. $r[ $post->ID ] = $post->post_parent;
  2293. return $r;
  2294. }
  2295. $this->posts = $wpdb->get_results($this->request);
  2296. // Raw results filter. Prior to status checks.
  2297. if ( !$q['suppress_filters'] )
  2298. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2299. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2300. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2301. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2302. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2303. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2304. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2305. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2306. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2307. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2308. $this->comments = $wpdb->get_results($comments_request);
  2309. $this->comment_count = count($this->comments);
  2310. }
  2311. if ( !$q['no_found_rows'] && !empty($limits) ) {
  2312. $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
  2313. $this->found_posts = $wpdb->get_var( $found_posts_query );
  2314. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2315. $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
  2316. }
  2317. // Check post status to determine if post should be displayed.
  2318. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2319. $status = get_post_status($this->posts[0]);
  2320. $post_status_obj = get_post_status_object($status);
  2321. //$type = get_post_type($this->posts[0]);
  2322. if ( !$post_status_obj->public ) {
  2323. if ( ! is_user_logged_in() ) {
  2324. // User must be logged in to view unpublished posts.
  2325. $this->posts = array();
  2326. } else {
  2327. if ( $post_status_obj->protected ) {
  2328. // User must have edit permissions on the draft to preview.
  2329. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2330. $this->posts = array();
  2331. } else {
  2332. $this->is_preview = true;
  2333. if ( 'future' != $status )
  2334. $this->posts[0]->post_date = current_time('mysql');
  2335. }
  2336. } elseif ( $post_status_obj->private ) {
  2337. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2338. $this->posts = array();
  2339. } else {
  2340. $this->posts = array();
  2341. }
  2342. }
  2343. }
  2344. if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2345. $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
  2346. }
  2347. // Put sticky posts at the top of the posts array
  2348. $sticky_posts = get_option('sticky_posts');
  2349. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2350. $num_posts = count($this->posts);
  2351. $sticky_offset = 0;
  2352. // Loop over posts and relocate stickies to the front.
  2353. for ( $i = 0; $i < $num_posts; $i++ ) {
  2354. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2355. $sticky_post = $this->posts[$i];
  2356. // Remove sticky from current position
  2357. array_splice($this->posts, $i, 1);
  2358. // Move to front, after other stickies
  2359. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2360. // Increment the sticky offset. The next sticky will be placed at this offset.
  2361. $sticky_offset++;
  2362. // Remove post from sticky posts array
  2363. $offset = array_search($sticky_post->ID, $sticky_posts);
  2364. unset( $sticky_posts[$offset] );
  2365. }
  2366. }
  2367. // If any posts have been excluded specifically, Ignore those that are sticky.
  2368. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2369. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2370. // Fetch sticky posts that weren't in the query results
  2371. if ( !empty($sticky_posts) ) {
  2372. $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  2373. // honor post type(s) if not set to any
  2374. $stickies_where = '';
  2375. if ( 'any' != $post_type && '' != $post_type ) {
  2376. if ( is_array( $post_type ) ) {
  2377. $post_types = join( "', '", $post_type );
  2378. } else {
  2379. $post_types = $post_type;
  2380. }
  2381. $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  2382. }
  2383. $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  2384. foreach ( $stickies as $sticky_post ) {
  2385. // Ignore sticky posts the current user cannot read or are not published.
  2386. if ( 'publish' != $sticky_post->post_status )
  2387. continue;
  2388. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2389. $sticky_offset++;
  2390. }
  2391. }
  2392. }
  2393. if ( !$q['suppress_filters'] )
  2394. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2395. $this->post_count = count($this->posts);
  2396. // Sanitize before caching so it'll only get done once
  2397. for ( $i = 0; $i < $this->post_count; $i++ ) {
  2398. $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
  2399. }
  2400. if ( $q['cache_results'] )
  2401. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2402. if ( $this->post_count > 0 ) {
  2403. $this->post = $this->posts[0];
  2404. }
  2405. return $this->posts;
  2406. }
  2407. /**
  2408. * Set up the next post and iterate current post index.
  2409. *
  2410. * @since 1.5.0
  2411. * @access public
  2412. *
  2413. * @return object Next post.
  2414. */
  2415. function next_post() {
  2416. $this->current_post++;
  2417. $this->post = $this->posts[$this->current_post];
  2418. return $this->post;
  2419. }
  2420. /**
  2421. * Sets up the current post.
  2422. *
  2423. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2424. * property to true.
  2425. *
  2426. * @since 1.5.0
  2427. * @access public
  2428. * @uses $post
  2429. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2430. */
  2431. function the_post() {
  2432. global $post;
  2433. $this->in_the_loop = true;
  2434. if ( $this->current_post == -1 ) // loop has just started
  2435. do_action_ref_array('loop_start', array(&$this));
  2436. $post = $this->next_post();
  2437. setup_postdata($post);
  2438. }
  2439. /**
  2440. * Whether there are more posts available in the loop.
  2441. *
  2442. * Calls action 'loop_end', when the loop is complete.
  2443. *
  2444. * @since 1.5.0
  2445. * @access public
  2446. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2447. *
  2448. * @return bool True if posts are available, false if end of loop.
  2449. */
  2450. function have_posts() {
  2451. if ( $this->current_post + 1 < $this->post_count ) {
  2452. return true;
  2453. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2454. do_action_ref_array('loop_end', array(&$this));
  2455. // Do some cleaning up after the loop
  2456. $this->rewind_posts();
  2457. }
  2458. $this->in_the_loop = false;
  2459. return false;
  2460. }
  2461. /**
  2462. * Rewind the posts and reset post index.
  2463. *
  2464. * @since 1.5.0
  2465. * @access public
  2466. */
  2467. function rewind_posts() {
  2468. $this->current_post = -1;
  2469. if ( $this->post_count > 0 ) {
  2470. $this->post = $this->posts[0];
  2471. }
  2472. }
  2473. /**
  2474. * Iterate current comment index and return comment object.
  2475. *
  2476. * @since 2.2.0
  2477. * @access public
  2478. *
  2479. * @return object Comment object.
  2480. */
  2481. function next_comment() {
  2482. $this->current_comment++;
  2483. $this->comment = $this->comments[$this->current_comment];
  2484. return $this->comment;
  2485. }
  2486. /**
  2487. * Sets up the current comment.
  2488. *
  2489. * @since 2.2.0
  2490. * @access public
  2491. * @global object $comment Current comment.
  2492. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2493. */
  2494. function the_comment() {
  2495. global $comment;
  2496. $comment = $this->next_comment();
  2497. if ( $this->current_comment == 0 ) {
  2498. do_action('comment_loop_start');
  2499. }
  2500. }
  2501. /**
  2502. * Whether there are more comments available.
  2503. *
  2504. * Automatically rewinds comments when finished.
  2505. *
  2506. * @since 2.2.0
  2507. * @access public
  2508. *
  2509. * @return bool True, if more comments. False, if no more posts.
  2510. */
  2511. function have_comments() {
  2512. if ( $this->current_comment + 1 < $this->comment_count ) {
  2513. return true;
  2514. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2515. $this->rewind_comments();
  2516. }
  2517. return false;
  2518. }
  2519. /**
  2520. * Rewind the comments, resets the comment index and comment to first.
  2521. *
  2522. * @since 2.2.0
  2523. * @access public
  2524. */
  2525. function rewind_comments() {
  2526. $this->current_comment = -1;
  2527. if ( $this->comment_count > 0 ) {
  2528. $this->comment = $this->comments[0];
  2529. }
  2530. }
  2531. /**
  2532. * Sets up the WordPress query by parsing query string.
  2533. *
  2534. * @since 1.5.0
  2535. * @access public
  2536. *
  2537. * @param string $query URL query string.
  2538. * @return array List of posts.
  2539. */
  2540. function &query( $query ) {
  2541. $this->init();
  2542. $this->query = $this->query_vars = wp_parse_args( $query );
  2543. return $this->get_posts();
  2544. }
  2545. /**
  2546. * Retrieve queried object.
  2547. *
  2548. * If queried object is not set, then the queried object will be set from
  2549. * the category, tag, taxonomy, posts page, single post, page, or author
  2550. * query variable. After it is set up, it will be returned.
  2551. *
  2552. * @since 1.5.0
  2553. * @access public
  2554. *
  2555. * @return object
  2556. */
  2557. function get_queried_object() {
  2558. if ( isset($this->queried_object) )
  2559. return $this->queried_object;
  2560. $this->queried_object = NULL;
  2561. $this->queried_object_id = 0;
  2562. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2563. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2564. $query = reset( $tax_query_in_and );
  2565. if ( 'term_id' == $query['field'] )
  2566. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2567. else
  2568. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2569. if ( $term && ! is_wp_error($term) ) {
  2570. $this->queried_object = $term;
  2571. $this->queried_object_id = (int) $term->term_id;
  2572. if ( $this->is_category )
  2573. _make_cat_compat( $this->queried_object );
  2574. }
  2575. } elseif ( $this->is_post_type_archive ) {
  2576. $this->queried_object = get_post_type_object( $this->get('post_type') );
  2577. } elseif ( $this->is_posts_page ) {
  2578. $page_for_posts = get_option('page_for_posts');
  2579. $this->queried_object = & get_page( $page_for_posts );
  2580. $this->queried_object_id = (int) $this->queried_object->ID;
  2581. } elseif ( $this->is_singular && !is_null($this->post) ) {
  2582. $this->queried_object = $this->post;
  2583. $this->queried_object_id = (int) $this->post->ID;
  2584. } elseif ( $this->is_author ) {
  2585. $this->queried_object_id = (int) $this->get('author');
  2586. $this->queried_object = get_userdata( $this->queried_object_id );
  2587. }
  2588. return $this->queried_object;
  2589. }
  2590. /**
  2591. * Retrieve ID of the current queried object.
  2592. *
  2593. * @since 1.5.0
  2594. * @access public
  2595. *
  2596. * @return int
  2597. */
  2598. function get_queried_object_id() {
  2599. $this->get_queried_object();
  2600. if ( isset($this->queried_object_id) ) {
  2601. return $this->queried_object_id;
  2602. }
  2603. return 0;
  2604. }
  2605. /**
  2606. * Constructor.
  2607. *
  2608. * Sets up the WordPress query, if parameter is not empty.
  2609. *
  2610. * @since 1.5.0
  2611. * @access public
  2612. *
  2613. * @param string $query URL query string.
  2614. * @return WP_Query
  2615. */
  2616. function __construct($query = '') {
  2617. if ( ! empty($query) ) {
  2618. $this->query($query);
  2619. }
  2620. }
  2621. /**
  2622. * Is the query for an archive page?
  2623. *
  2624. * Month, Year, Category, Author, Post Type archive...
  2625. *
  2626. * @since 3.1.0
  2627. *
  2628. * @return bool
  2629. */
  2630. function is_archive() {
  2631. return (bool) $this->is_archive;
  2632. }
  2633. /**
  2634. * Is the query for a post type archive page?
  2635. *
  2636. * @since 3.1.0
  2637. *
  2638. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2639. * @return bool
  2640. */
  2641. function is_post_type_archive( $post_types = '' ) {
  2642. if ( empty( $post_types ) || !$this->is_post_type_archive )
  2643. return (bool) $this->is_post_type_archive;
  2644. $post_type_object = $this->get_queried_object();
  2645. return in_array( $post_type_object->name, (array) $post_types );
  2646. }
  2647. /**
  2648. * Is the query for an attachment page?
  2649. *
  2650. * @since 3.1.0
  2651. *
  2652. * @return bool
  2653. */
  2654. function is_attachment() {
  2655. return (bool) $this->is_attachment;
  2656. }
  2657. /**
  2658. * Is the query for an author archive page?
  2659. *
  2660. * If the $author parameter is specified, this function will additionally
  2661. * check if the query is for one of the authors specified.
  2662. *
  2663. * @since 3.1.0
  2664. *
  2665. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  2666. * @return bool
  2667. */
  2668. function is_author( $author = '' ) {
  2669. if ( !$this->is_author )
  2670. return false;
  2671. if ( empty($author) )
  2672. return true;
  2673. $author_obj = $this->get_queried_object();
  2674. $author = (array) $author;
  2675. if ( in_array( $author_obj->ID, $author ) )
  2676. return true;
  2677. elseif ( in_array( $author_obj->nickname, $author ) )
  2678. return true;
  2679. elseif ( in_array( $author_obj->user_nicename, $author ) )
  2680. return true;
  2681. return false;
  2682. }
  2683. /**
  2684. * Is the query for a category archive page?
  2685. *
  2686. * If the $category parameter is specified, this function will additionally
  2687. * check if the query is for one of the categories specified.
  2688. *
  2689. * @since 3.1.0
  2690. *
  2691. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  2692. * @return bool
  2693. */
  2694. function is_category( $category = '' ) {
  2695. if ( !$this->is_category )
  2696. return false;
  2697. if ( empty($category) )
  2698. return true;
  2699. $cat_obj = $this->get_queried_object();
  2700. $category = (array) $category;
  2701. if ( in_array( $cat_obj->term_id, $category ) )
  2702. return true;
  2703. elseif ( in_array( $cat_obj->name, $category ) )
  2704. return true;
  2705. elseif ( in_array( $cat_obj->slug, $category ) )
  2706. return true;
  2707. return false;
  2708. }
  2709. /**
  2710. * Is the query for a tag archive page?
  2711. *
  2712. * If the $tag parameter is specified, this function will additionally
  2713. * check if the query is for one of the tags specified.
  2714. *
  2715. * @since 3.1.0
  2716. *
  2717. * @param mixed $slug Optional. Tag slug or array of slugs.
  2718. * @return bool
  2719. */
  2720. function is_tag( $slug = '' ) {
  2721. if ( !$this->is_tag )
  2722. return false;
  2723. if ( empty( $slug ) )
  2724. return true;
  2725. $tag_obj = $this->get_queried_object();
  2726. $slug = (array) $slug;
  2727. if ( in_array( $tag_obj->slug, $slug ) )
  2728. return true;
  2729. return false;
  2730. }
  2731. /**
  2732. * Is the query for a taxonomy archive page?
  2733. *
  2734. * If the $taxonomy parameter is specified, this function will additionally
  2735. * check if the query is for that specific $taxonomy.
  2736. *
  2737. * If the $term parameter is specified in addition to the $taxonomy parameter,
  2738. * this function will additionally check if the query is for one of the terms
  2739. * specified.
  2740. *
  2741. * @since 3.1.0
  2742. *
  2743. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  2744. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  2745. * @return bool
  2746. */
  2747. function is_tax( $taxonomy = '', $term = '' ) {
  2748. global $wp_taxonomies;
  2749. if ( !$this->is_tax )
  2750. return false;
  2751. if ( empty( $taxonomy ) )
  2752. return true;
  2753. $queried_object = $this->get_queried_object();
  2754. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  2755. $term_array = (array) $term;
  2756. if ( empty( $term ) ) // Only a Taxonomy provided
  2757. return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
  2758. return isset( $queried_object->term_id ) &&
  2759. count( array_intersect(
  2760. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  2761. $term_array
  2762. ) );
  2763. }
  2764. /**
  2765. * Whether the current URL is within the comments popup window.
  2766. *
  2767. * @since 3.1.0
  2768. *
  2769. * @return bool
  2770. */
  2771. function is_comments_popup() {
  2772. return (bool) $this->is_comments_popup;
  2773. }
  2774. /**
  2775. * Is the query for a date archive?
  2776. *
  2777. * @since 3.1.0
  2778. *
  2779. * @return bool
  2780. */
  2781. function is_date() {
  2782. return (bool) $this->is_date;
  2783. }
  2784. /**
  2785. * Is the query for a day archive?
  2786. *
  2787. * @since 3.1.0
  2788. *
  2789. * @return bool
  2790. */
  2791. function is_day() {
  2792. return (bool) $this->is_day;
  2793. }
  2794. /**
  2795. * Is the query for a feed?
  2796. *
  2797. * @since 3.1.0
  2798. *
  2799. * @param string|array $feeds Optional feed types to check.
  2800. * @return bool
  2801. */
  2802. function is_feed( $feeds = '' ) {
  2803. if ( empty( $feeds ) || ! $this->is_feed )
  2804. return (bool) $this->is_feed;
  2805. $qv = $this->get( 'feed' );
  2806. if ( 'feed' == $qv )
  2807. $qv = get_default_feed();
  2808. return in_array( $qv, (array) $feeds );
  2809. }
  2810. /**
  2811. * Is the query for a comments feed?
  2812. *
  2813. * @since 3.1.0
  2814. *
  2815. * @return bool
  2816. */
  2817. function is_comment_feed() {
  2818. return (bool) $this->is_comment_feed;
  2819. }
  2820. /**
  2821. * Is the query for the front page of the site?
  2822. *
  2823. * This is for what is displayed at your site's main URL.
  2824. *
  2825. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  2826. *
  2827. * If you set a static page for the front page of your site, this function will return
  2828. * true when viewing that page.
  2829. *
  2830. * Otherwise the same as @see WP_Query::is_home()
  2831. *
  2832. * @since 3.1.0
  2833. * @uses is_home()
  2834. * @uses get_option()
  2835. *
  2836. * @return bool True, if front of site.
  2837. */
  2838. function is_front_page() {
  2839. // most likely case
  2840. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  2841. return true;
  2842. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  2843. return true;
  2844. else
  2845. return false;
  2846. }
  2847. /**
  2848. * Is the query for the blog homepage?
  2849. *
  2850. * This is the page which shows the time based blog content of your site.
  2851. *
  2852. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  2853. *
  2854. * If you set a static page for the front page of your site, this function will return
  2855. * true only on the page you set as the "Posts page".
  2856. *
  2857. * @see WP_Query::is_front_page()
  2858. *
  2859. * @since 3.1.0
  2860. *
  2861. * @return bool True if blog view homepage.
  2862. */
  2863. function is_home() {
  2864. return (bool) $this->is_home;
  2865. }
  2866. /**
  2867. * Is the query for a month archive?
  2868. *
  2869. * @since 3.1.0
  2870. *
  2871. * @return bool
  2872. */
  2873. function is_month() {
  2874. return (bool) $this->is_month;
  2875. }
  2876. /**
  2877. * Is the query for a single page?
  2878. *
  2879. * If the $page parameter is specified, this function will additionally
  2880. * check if the query is for one of the pages specified.
  2881. *
  2882. * @see WP_Query::is_single()
  2883. * @see WP_Query::is_singular()
  2884. *
  2885. * @since 3.1.0
  2886. *
  2887. * @param mixed $page Page ID, title, slug, or array of such.
  2888. * @return bool
  2889. */
  2890. function is_page( $page = '' ) {
  2891. if ( !$this->is_page )
  2892. return false;
  2893. if ( empty( $page ) )
  2894. return true;
  2895. $page_obj = $this->get_queried_object();
  2896. $page = (array) $page;
  2897. if ( in_array( $page_obj->ID, $page ) )
  2898. return true;
  2899. elseif ( in_array( $page_obj->post_title, $page ) )
  2900. return true;
  2901. else if ( in_array( $page_obj->post_name, $page ) )
  2902. return true;
  2903. return false;
  2904. }
  2905. /**
  2906. * Is the query for paged result and not for the first page?
  2907. *
  2908. * @since 3.1.0
  2909. *
  2910. * @return bool
  2911. */
  2912. function is_paged() {
  2913. return (bool) $this->is_paged;
  2914. }
  2915. /**
  2916. * Is the query for a post or page preview?
  2917. *
  2918. * @since 3.1.0
  2919. *
  2920. * @return bool
  2921. */
  2922. function is_preview() {
  2923. return (bool) $this->is_preview;
  2924. }
  2925. /**
  2926. * Is the query for the robots file?
  2927. *
  2928. * @since 3.1.0
  2929. *
  2930. * @return bool
  2931. */
  2932. function is_robots() {
  2933. return (bool) $this->is_robots;
  2934. }
  2935. /**
  2936. * Is the query for a search?
  2937. *
  2938. * @since 3.1.0
  2939. *
  2940. * @return bool
  2941. */
  2942. function is_search() {
  2943. return (bool) $this->is_search;
  2944. }
  2945. /**
  2946. * Is the query for a single post?
  2947. *
  2948. * Works for any post type, except attachments and pages
  2949. *
  2950. * If the $post parameter is specified, this function will additionally
  2951. * check if the query is for one of the Posts specified.
  2952. *
  2953. * @see WP_Query::is_page()
  2954. * @see WP_Query::is_singular()
  2955. *
  2956. * @since 3.1.0
  2957. *
  2958. * @param mixed $post Post ID, title, slug, or array of such.
  2959. * @return bool
  2960. */
  2961. function is_single( $post = '' ) {
  2962. if ( !$this->is_single )
  2963. return false;
  2964. if ( empty($post) )
  2965. return true;
  2966. $post_obj = $this->get_queried_object();
  2967. $post = (array) $post;
  2968. if ( in_array( $post_obj->ID, $post ) )
  2969. return true;
  2970. elseif ( in_array( $post_obj->post_title, $post ) )
  2971. return true;
  2972. elseif ( in_array( $post_obj->post_name, $post ) )
  2973. return true;
  2974. return false;
  2975. }
  2976. /**
  2977. * Is the query for a single post of any post type (post, attachment, page, ... )?
  2978. *
  2979. * If the $post_types parameter is specified, this function will additionally
  2980. * check if the query is for one of the Posts Types specified.
  2981. *
  2982. * @see WP_Query::is_page()
  2983. * @see WP_Query::is_single()
  2984. *
  2985. * @since 3.1.0
  2986. *
  2987. * @param mixed $post_types Optional. Post Type or array of Post Types
  2988. * @return bool
  2989. */
  2990. function is_singular( $post_types = '' ) {
  2991. if ( empty( $post_types ) || !$this->is_singular )
  2992. return (bool) $this->is_singular;
  2993. $post_obj = $this->get_queried_object();
  2994. return in_array( $post_obj->post_type, (array) $post_types );
  2995. }
  2996. /**
  2997. * Is the query for a specific time?
  2998. *
  2999. * @since 3.1.0
  3000. *
  3001. * @return bool
  3002. */
  3003. function is_time() {
  3004. return (bool) $this->is_time;
  3005. }
  3006. /**
  3007. * Is the query for a trackback endpoint call?
  3008. *
  3009. * @since 3.1.0
  3010. *
  3011. * @return bool
  3012. */
  3013. function is_trackback() {
  3014. return (bool) $this->is_trackback;
  3015. }
  3016. /**
  3017. * Is the query for a specific year?
  3018. *
  3019. * @since 3.1.0
  3020. *
  3021. * @return bool
  3022. */
  3023. function is_year() {
  3024. return (bool) $this->is_year;
  3025. }
  3026. /**
  3027. * Is the query a 404 (returns no results)?
  3028. *
  3029. * @since 3.1.0
  3030. *
  3031. * @return bool
  3032. */
  3033. function is_404() {
  3034. return (bool) $this->is_404;
  3035. }
  3036. }
  3037. /**
  3038. * Redirect old slugs to the correct permalink.
  3039. *
  3040. * Attempts to find the current slug from the past slugs.
  3041. *
  3042. * @since 2.1.0
  3043. * @uses $wp_query
  3044. * @uses $wpdb
  3045. *
  3046. * @return null If no link is found, null is returned.
  3047. */
  3048. function wp_old_slug_redirect() {
  3049. global $wp_query;
  3050. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3051. global $wpdb;
  3052. // Guess the current post_type based on the query vars.
  3053. if ( get_query_var('post_type') )
  3054. $post_type = get_query_var('post_type');
  3055. elseif ( !empty($wp_query->query_vars['pagename']) )
  3056. $post_type = 'page';
  3057. else
  3058. $post_type = 'post';
  3059. if ( is_array( $post_type ) ) {
  3060. if ( count( $post_type ) > 1 )
  3061. return;
  3062. $post_type = array_shift( $post_type );
  3063. }
  3064. // Do not attempt redirect for hierarchical post types
  3065. if ( is_post_type_hierarchical( $post_type ) )
  3066. return;
  3067. $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
  3068. // if year, monthnum, or day have been specified, make our query more precise
  3069. // just in case there are multiple identical _wp_old_slug values
  3070. if ( '' != $wp_query->query_vars['year'] )
  3071. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3072. if ( '' != $wp_query->query_vars['monthnum'] )
  3073. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3074. if ( '' != $wp_query->query_vars['day'] )
  3075. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3076. $id = (int) $wpdb->get_var($query);
  3077. if ( ! $id )
  3078. return;
  3079. $link = get_permalink($id);
  3080. if ( !$link )
  3081. return;
  3082. wp_redirect( $link, 301 ); // Permanent redirect
  3083. exit;
  3084. endif;
  3085. }
  3086. /**
  3087. * Set up global post data.
  3088. *
  3089. * @since 1.5.0
  3090. *
  3091. * @param object $post Post data.
  3092. * @uses do_action_ref_array() Calls 'the_post'
  3093. * @return bool True when finished.
  3094. */
  3095. function setup_postdata($post) {
  3096. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3097. $id = (int) $post->ID;
  3098. $authordata = get_userdata($post->post_author);
  3099. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3100. $currentmonth = mysql2date('m', $post->post_date, false);
  3101. $numpages = 1;
  3102. $page = get_query_var('page');
  3103. if ( !$page )
  3104. $page = 1;
  3105. if ( is_single() || is_page() || is_feed() )
  3106. $more = 1;
  3107. $content = $post->post_content;
  3108. if ( strpos( $content, '<!--nextpage-->' ) ) {
  3109. if ( $page > 1 )
  3110. $more = 1;
  3111. $multipage = 1;
  3112. $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  3113. $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  3114. $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  3115. $pages = explode('<!--nextpage-->', $content);
  3116. $numpages = count($pages);
  3117. } else {
  3118. $pages = array( $post->post_content );
  3119. $multipage = 0;
  3120. }
  3121. do_action_ref_array('the_post', array(&$post));
  3122. return true;
  3123. }
  3124. ?>