PageRenderTime 76ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-includes/query.php

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